【发布时间】:2013-12-17 16:49:48
【问题描述】:
我需要使用 Javascript 启动、停止和重新启动横向动画,通过一些搜索我发现 Jquery animate() 需要一个插件来执行启动-停止-重新启动,但我不会使用插件来执行此操作(没有时间问“为什么不呢?”)。
所以,我问我:“为什么你不使用setInterval() 来做这个横向动画并用clearInterval() 停止它?
这是我的代码,是正确的还是浏览器的疯狂工作?我需要构建一个“有效”(就浏览器工作而言)脚本,但是,我不是最好的。请原谅。
window.newsflash.interval = setInterval(function() {
if(liWidthTot + $('.fn_container ul').css("left") <= 0)
$('.fn_container ul').css('left', containerWidth);
$('.fn_container ul').css("left", "-=1px");
},10);
//handler
$("#xml").on({
mouseenter: function(){
clearInterval(window.newsflash.interval);
},
mouseleave: function(){
window.newsflash.interval = setInterval(function() {
if($('.fn_container ul').css("left") - liWidthTot <= 0)
$('.fn_container ul').css('left', containerWidth);
$('.fn_container ul').css("left", "-=1px");
},10);
}
});
这只是我的 Js 代码的一种安宁...我不需要知道如何...但是如果使用 setInterval() 是个坏主意。
----- 编辑----
现在:
var newsList = document.getElementById("newsList");
window.newsflash.interval = setInterval(function() {
if(liWidthTot + parseInt(newsList.style.left) <= 0) newsList.style.left = containerWidth +"px";
ewsList.style.left = (parseInt(newsList.style.left) -1) + "px";
},10);
//handler
$("#xml").on({
mouseenter: function(){
clearInterval(window.newsflash.interval);
},
mouseleave: function(){
window.newsflash.interval = setInterval(function() {
if(liWidthTot + parseInt(newsList.style.left) <= 0) newsList.style.left = containerWidth +"px";
newsList.style.left = (parseInt(newsList.style.left) -1) + "px";
},10);
}
})
谢谢队友。
【问题讨论】:
-
查看这个性能测试(比较 js 原生 setInterval 和 jQuery animate):jsperf.com/jquery-vs-javascript-animation
-
谢谢...是我想知道的。
标签: javascript performance animation setinterval