简体   繁体   中英

Why is .scrollTop(); not working well?

I'm trying to make a sample alert if you scroll after a certain height, but the code does not work. Have a look:

$(window).scroll(function(e){
     var scrolled = $(window).scrollTop();
     if (scrolled = 18) {
        alert("ciao");
     }
});

The Google Chrome's console does not report me any bugs and jQuery is linked properly. Do you know which is the problem?

Your assignment operator = should be a comparison operator == .

if (scrolled == 18) {// ==
               alert("ciao");
}

Update:

Don't know why you want alert() at exactly 18 . But you should better use this:

if (scrolled >= 18) {// ==
               alert("ciao");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM