2014-03-27 76 views
0

我想让浏览器滚动到页面上的一个点,如果页面向下滚动。我正在使用jQuery .bind()html绑定到mousewheel。这里是我的代码:为什么我的if语句继续运行?

"use strict"; 
var scrollpoint = 1; 
$(document).ready(function(){ 
    console.log(scrollpoint); 
    $("#divacon").hide(); 
    $("#divbcon").hide(); 
    $('#div-a').waypoint(function() { 
      $("#divacon").fadeIn(); 
      var scrollpoint = 2; 
      console.log("waypoint a reached"); 
    },{context:"#container"}); 
    $('#div-b').waypoint(function() { 
      $("#divbcon").fadeIn(); 
      console.log("waypoint b reached"); 
    },{context:"#container"}); 

$('html').bind('mousewheel', function(e){ 
    var flag = true; 
    if(flag) { 
     if(e.originalEvent.wheelDelta < 0) { 
      if((scrollpoint == 1)) { 
       var target = $('#div-a'); 
       target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
       var targetoffset = target.offset().top; 
       console.log(scrollpoint); 
       $('#container').animate(
        {scrollTop: targetoffset}, 
        400, 
        function(){ 
         var scrollpoint = 2; 
         console.log(scrollpoint); 
        } 
       ); 
      } 
      else if((scrollpoint = 2)){ 
       //scroll down 
       console.log('2'); 
      } 
      else{ 
       //scroll down 
       console.log('Down'); 
      } 
     }else { 
      //scroll up 
      console.log('Up'); 
     } 
    //prevent page fom scrolling 
    flag = false; 
    } 
}); 

发生了什么事是我的if语句被调用后的第一时间,即使flag = false;。我究竟做错了什么?该网站可以在现场http://wilsonbiggs.com/sandy

回答

2

找到我认为这是

var flag = true; //put the flag out side mouse wheel bind. 

$('html').bind('mousewheel', function(e){ 

    if(flag) { 

否则每个甚至触发时间设置flagtrue及以下if condition会满意永远。

+0

它不让我接受答案,但是,这是做到了!我是一个傻子,这很明显。哈哈 –

+0

@WilsonB​​iggs:没问题。 – mithunsatheesh

相关问题