2017-04-20 97 views
0

我使用的是滾動功能,以及最近增加了一個刷新它網頁不更新,直到我滾動

(因爲我有一個郵件的形式,這是獲得?resp=0/事情最簡單的方法了。是的,我知道有其他的方法,但這次是最好的一個用我,所以請不要推薦其他的方式來做到這一點的過程)

莫名其妙有時當我刷新頁面,它不不知道它是什麼位置。我有開始display:none的東西,但是當它在頁面上的某個地方時,它是display:block。當我滾動頁面突然更新(如出現圖像,導致他們得到display:block,並且url從index.php#this更改爲index.php#that)。我希望它在刷新時立即更改,因此不需要首先滾動。

大多數時候它工作正常,但有時它會像這樣,我不知道是問題所在。下面


代碼:

$(function() { 
    $(window).bind('scroll', function() { 
     if ($(window).scrollTop() > ($(document).height()/4.65) * 3) { 
      $('.Attribute').removeClass('that').addClass('this'); (etc..) 
      if (performance.navigation.type == 1) { 
       window.location.replace("index.php#this"); 
      } 
     } 

     else if ($(window).scrollTop() > ($(document).height()/4.65) * 1.3) { 
      $('.Attribute').removeClass('that').addClass('this'); (etc..) 
      if (performance.navigation.type == 1) { 
       window.location.replace("index.php#that"); 
      } 
     } 

     else if ($(window).scrollTop() > $(document).height()/4.65 * 0.3) { 
      $('.Attribute').removeClass('that').addClass('this'); (etc..) 
      if (performance.navigation.type == 1) { 
       window.location.replace("index.php#things"); 
      } 
     } 
     else if ($(window).scrollTop() < $(document).height()/4.65 * 0.3){ 
      $('.Attribute').removeClass('that').addClass('this'); (etc..) 
      if (performance.navigation.type == 1) { 
       window.location.replace("index.php#something"); 
      } 
     } 
    }); 
}); 

回答

1

你可以考慮做:

$(window).bind('scroll', function() { 
    else if ($(window).scrollTop() > ($(document).height()/4.65) * 3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#this"); 
     } 
    } 

    else if ($(window).scrollTop() > ($(document).height()/4.65) * 1.3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#that"); 
     } 
    } 

    else if ($(window).scrollTop() > $(document).height()/4.65 * 0.3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#things"); 
     } 
    } 
    else if ($(window).scrollTop() < $(document).height()/4.65 * 0.3){ 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#something"); 
     } 
    } 
}).scroll() 

添加.scroll()將調用事件


或者你可以把事件中的函數,調用它:

function onScroll() { 
    else if ($(window).scrollTop() > ($(document).height()/4.65) * 3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#this"); 
     } 
    } 

    else if ($(window).scrollTop() > ($(document).height()/4.65) * 1.3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#that"); 
     } 
    } 

    else if ($(window).scrollTop() > $(document).height()/4.65 * 0.3) { 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#things"); 
     } 
    } 
    else if ($(window).scrollTop() < $(document).height()/4.65 * 0.3){ 
     $('.Attribute').removeClass('that').addClass('this'); (etc..) 
     if (performance.navigation.type == 1) { 
      window.location.replace("index.php#something"); 
     } 
    } 
} 

$(window).bind('scroll', onScroll); 
onScroll(); 

PS:那是正常的,第一if是否則,如果?

+0

我剛纔編輯的第一個if語句,它有一個像9,如果在我的正常的代碼,所以我碰巧部分:),我會嘗試你的代碼 – Minegolfer

+0

由於它的工作,我做了第一個。 (他們中的一個比另一個更好呢?就像其中一個給予更少的問題一樣?)也不能接受答案,但需要等5分鐘纔會讓你快速:) – Minegolfer

+0

好吧,我不知道我經常使用第一個,因爲它是更少的代碼(看起來很清晰),但第二個也很好,@Sandwell也可以工作 –

1

你可以試試這個

$(window).on("load scroll",function(e) { 
+0

對不起別人給了一個工作代碼allready – Minegolfer