Question

I'm trying to make a browser scroll to a point on a page if the page is scrolled down. I'm using jQuery .bind() to bind html to mousewheel. Here's my code:

"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"});

and

$('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;
    }
});

What's happening is that my if statement is being called after the first time, even when flag = false;. What am I doing wrong? The site can be found live at http://wilsonbiggs.com/sandy

Was it helpful?

Solution

I think it has to be

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

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

    if(flag) {

Otherwise each time your even triggers you set flag to true and the following if condition will be satisfied always.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top