啓停JavaScript函數我有我的網站下面的代碼....由定時器
首先,我將如何開始通過單擊鏈接而動畫/功能?
然後,我將如何「凍結」動畫中的最後一幀? (可能由定時器?)
謝謝。
HTML標記:
<div id="anim"></div>
CSS:
#anim {
width: 14px; height: 14px;
background-image: url(http://mail.google.com/mail/im/emotisprites/wink2.png);
background-repeat: no-repeat;
}
的Javascript:
var scrollUp = (function() {
var timerId; // stored timer in case you want to use clearInterval later
return function (height, times, element) {
var i = 0; // a simple counter
timerId = setInterval(function() {
if (i > times) // if the last frame is reached, set counter to zero
i = 0;
element.style.backgroundPosition = "0px -" + i * height + 'px'; //scroll up
i++;
}, 100); // every 100 milliseconds
};
})();
// start animation:
scrollUp(14, 42, document.getElementById('anim'))
這裏有一個小提琴:http://jsfiddle.net/ctF4t/