为什么我的if语句继续运行?

时间:2014-03-27 04:29:19

标签: javascript jquery html mousewheel jquery-waypoints

如果页面向下滚动,我正在尝试使浏览器滚动到页面上的某个点。我正在使用jQuery .bind()html绑定到mousewheel。这是我的代码:

"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

上找到

1 个答案:

答案 0 :(得分:2)

我认为必须是

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

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

    if(flag) {

否则,每次偶数触发时,您都会将flag设置为true,并且始终会满足以下if condition