简体   繁体   中英

I would like to move images in a div so the containing div displays them exactly their size

function mov_left() {
    if (parseInt(document.getElementById('fold').style.left, 10) == 430) {
        clearInterval(animel);
    }
    document.getElementById('fold').style.left = (parseInt(document.getElementById('fold').style.left, 10) - 10) + 'px';
}

animel = setInterval('mov_left()', 100);

This is the code I'm trying to use to move a <div> containing 2 images so they can display the two images one at a time. However, the image just keeps moving to the left ignoring the if statement in the function.

Help please.

well I've fiddled with it, not really much change but I know this works:

function mov_left() {
    var el = document.getElementById('fold');
    if (parseInt(el.style.left, 10) == 430) {
        clearInterval(animel);
    }
    el.style.left = (parseInt(el.style.left, 10) - 10) + 'px';
}
animel = setInterval(function () {mov_left(); }, 100);

JSfiddle: http://jsfiddle.net/m7q3H/42/

如果未设置element的左css属性,则element.style.left将返回一个空字符串,而parseInt将返回NAN。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM