從您通過sam
的位置刪除()
作爲回調。你想傳遞函數,而不是它的結果。
var sam = function() { // Changed this to show functions are variables
$("#animate").children("#box")
.animate({
left: '+=1100'},8000, 'linear', function() { // Not sure about +=
$("#animate").children("#box").css("left", "0");
sam; // Pass the variable, not the result of the function
});
};
sam(); // Call the function the first time.
然而,我注意到,#box
是一個id。所以,我推測什麼,我認爲你想做的事,並想出這個演示:
var sam = function() { // Changed this to show functions are variables
$("#box").animate({left: $("#animate").width() + 'px'}, 1000, 'linear')
.animate({left: '-' + $("#box").width() + 'px'}, 0, 'linear', sam);
};
sam(); // Call the function the first time.
#animate {
position: relative;
width: 200px;
height: 200px;
background-color: red;
overflow: hidden;
}
#box {
position: absolute;
width: 50px;
height: 50px;
left: 0;
top: 0;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="animate">
<div id="box"></div>
</div>
或者的jsfiddle如果你喜歡:http://jsfiddle.net/Gisleburt/tn8j7w8p/1/
什麼是「打破的其餘部分我代碼「的意思? – JJJ
它只是停止我的代碼的其他部分在Firefox中工作? –