0
我想運行一個函數(scrollSprite),加載時通過for循環,用一堆if語句「填充」自己。然後在一個滾動事件後,我想調用這個現在特定的「填充」或「內置」函數。我知道如何調用滾動功能,我的問題是,我不知道如何調用該函數的具體實例。如何在JavaScript中調用函數,然後在滾動事件中調用該函數的特定實例?
見下文代碼:
var scrollSprite = function(distance, frames, frameSize, scrollInterval){
minScroll = 0;
maxScroll = -scrollInterval;
for (i = 0; i < frames; i++){
if(distance < minScroll && distance >= maxScroll){
$('#sprite').css("top", -scrollInterval);
}
minScroll = maxScroll;
maxScroll -= scrollInterval;
}
}
window.addEventListener("scroll", function(e){
if($('#finding-finders').hasClass('active')){
var scrollTop = $(window).scrollTop(),
elementOffset = $('#finding-finders').offset().top;
distance = (elementOffset - scrollTop);
//if the the element has reached the window then run instance of scrollSprite
if(distance < 0){
scrollSprite(distance, 10, 300, 20);
}
}
}
任何方向將不勝感激!謝謝