Javascript after 7 seconds slide image

非 Y 不嫁゛ 提交于 2019-12-11 12:23:33

问题


http://jsfiddle.net/UWLsB/189/

I am trying to make the image slide to the left after 7 seconds, how come it's not working?

Html:

<div id="container">
    <div id="pack">
        <img id="pack" src="http://imgur.com/uGMzOGM.png">
    </div>

Javascript:

function FetchData() {
    $("#pack").css('margin-left', 0);
    $("#pack").css('margin-right', 0);
    $("#pack").animate({
        left: '-1000px'
    }, 'slow');
});
}
setTimeout(FetchData, 7000);

CSS:

#pack {
    margin: 5px auto;
    position:fixed;
}
#container {
    overflow:hidden;
}

回答1:


You just have a syntax error. The }); line should be removed entirely:

http://jsfiddle.net/UWLsB/190/

I think you meant to use syntax that would have } and ) on the same line or got it from some tutorial like:

setTimeout(function () {
    // .animate call stuff here
}, 7000);

By the way, you could do this purely with CSS using animations:

http://jsfiddle.net/UWLsB/192/




回答2:


http://jsfiddle.net/UWLsB/191/

function FetchData() {
    $("#pack").css('margin-left', 0);
    $("#pack").css('margin-right', 0);
    $("#pack").animate({
        left: '-1000px'
    }, 'slow');
}
setTimeout(FetchData, 7000);

you can also make the first two lines of the function into one like this

$("#pack").css({"margin-left": "0", "margin-right": "0"});


来源:https://stackoverflow.com/questions/20926650/javascript-after-7-seconds-slide-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!