[英]Why does my if statement keep running?
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
. 我正在使用jQuery .bind()
将html
绑定到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;
发生的事情是我的if语句在第一次被调用之后,即使是flag = false;
. 。 What am I doing wrong? 我究竟做错了什么? The site can be found live at http://wilsonbiggs.com/sandy 该网站可以在http://wilsonbiggs.com/sandy上找到
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. 否则,每次偶数触发时,将flag
设置为true
并始终满足以下if condition
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.