2017-04-20 96 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