【问题标题】:Replace value by variable, page load slowly jquery用变量替换值,页面加载缓慢jquery
【发布时间】:2013-06-13 22:33:18
【问题描述】:

我有一个完美运行的脚本:

<script>
function aller(){
$('#container').animate({marginLeft : '0px'}, {duration:1});
retour();
}

function boucle(){
$('#container').animate({marginLeft : '0px'}, {duration:4000});
retour();
}

function retour(){
//Modify marginLeft value if needed
$('#container').animate({marginLeft : -400}, {duration:4000});
boucle();
}
</script>

但是当我通过我需要的变量更改左边距“-400”时:

$('#container').animate({marginLeft : $('.Image img:last').position().left}, {duration:4000});

网页加载很慢,我真的不明白,因为当我检查他是否找到值时,它直接找到它(通过写

alert($('.Image img:last').position().left);

例如)

我不知道它在加载什么,因为他知道价值!

html部分:

<body onLoad="aller();">
<div id="cont">
<div id="container">
    <div id="images" >
        <div class="Image"><img src="./Images/richard.jpg"/><div>
        <div class="Image"><img src="./Images/ivanovic.jpg"/><div>
    </div>
</div>

谢谢!

【问题讨论】:

  • 你知道调用这些函数会迫使它们一遍又一遍地来回走动吗?
  • 是的,我知道,这不是这里的问题 =)

标签: jquery variables replace load


【解决方案1】:

您的问题在这里...您正在调用boucle(), 中的retour() 函数,该函数又再次调用retour(),以此类推无限......

function boucle(){
$('#container').animate({marginLeft : '0px'}, {duration:4000});
retour();
}

function retour(){
//Modify marginLeft value if needed
$('#container').animate({marginLeft : -400}, {duration:4000});
boucle();
}

这会导致无限循环.....从而减慢您的页面爬行速度

不确定这是否是您所说的问题,但这绝对是您需要解决的问题.. :)

【讨论】:

  • 是的,我知道这不是问题所在,因为当我不将变量放入循环时它根本不会滞后!
【解决方案2】:

看起来你想调用retour(),然后让它在动画完成后调用boucle(),以此类推。但是对animate() 的调用在安排动画之后立即返回。您要做的是将另一个函数注册为完成时的回调:

$('#container').animate({marginLeft : -400}, {duration:4000, complete:retour});

【讨论】:

  • 是的!我想你明白了。这是问题所在,但现在动画只进行一次迭代并停止。编辑:不,很好,只是在每次迭代下加载一点=)非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-03-15
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
相关资源
最近更新 更多