2014-08-28 116 views
1

我試圖得到一段代碼只執行一次,當滾動到:上滾動更改代碼永久

$('#main_nav').addClass('past-scrollpoint'); 
$('#content-block').addClass('past-scrollpoint-content'); 
$('#header').addClass('hidden'); 

但是,代碼處於非活動狀態,一旦點小於740

這是我最好的嘗試:

$(window).on('scroll', function() 
{ 
    var initial = 0; 
    stop = Math.round($(window).scrollTop()); 
    if (initial < 1) 
    { 
    if (stop > 740) 
    { 
     initial = initial + 1; 
     $('#main_nav').addClass('past-scrollpoint'); 
     $('#content-block').addClass('past-scrollpoint-content'); 
     $('#header').addClass('hidden'); 
    } 
    } 
); 

回答

1

你想要那段代碼只執行一次滾動到740或更多?爲此,我認爲你的代碼很好。你只需要稍作修改: -

$(window).on('scroll', function(){ 

var initial = 0; 
while(initial<1) 
{ 
stop = Math.round($(window).scrollTop()); 

if (stop >= 740) 
{  
    $('#main_nav').addClass('past-scrollpoint'); 
    $('#content-block').addClass('past-scrollpoint-content'); 
    $('#header').addClass('hidden'); 
    initial = initial + 1; 
} 
} 
}); 

希望這會有所幫助!

4

不知道,但我覺得每次要覆蓋初始變量。使用全局變量。

var initial = 0; 
$(window).on('scroll', function() 
{ 
    stop = Math.round($(window).scrollTop()); 
    if (initial < 1) 
    { 
    if (stop > 740) 
    { 
     initial = initial + 1; 
     $('#main_nav').addClass('past-scrollpoint'); 
     $('#content-block').addClass('past-scrollpoint-content'); 
     $('#header').addClass('hidden'); 
    } 
    } 
);