admin管理员组文章数量:1296279
I was making a blog, and tried to use $(window).scroll(function()
, but something prevents it from working.
I try to add class named scrolled
to body when user scrolls down. Any idea which would prevent it working properly? Console doesn't give any error regarding to this.
JS
$(document).ready(function($) {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 100) {
$("body").addClass("scrolled");
} else {
$("body").removeClass("scrolled");
}
});
});
LIVE PREVIEW
/
I was making a blog, and tried to use $(window).scroll(function()
, but something prevents it from working.
I try to add class named scrolled
to body when user scrolls down. Any idea which would prevent it working properly? Console doesn't give any error regarding to this.
JS
$(document).ready(function($) {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 100) {
$("body").addClass("scrolled");
} else {
$("body").removeClass("scrolled");
}
});
});
LIVE PREVIEW
http://personaii-demo.tumblr./
Share Improve this question edited Jul 25, 2014 at 16:01 morkro 4,6755 gold badges27 silver badges35 bronze badges asked Jul 25, 2014 at 15:49 Aleksi TappuraAleksi Tappura 711 gold badge2 silver badges6 bronze badges 2-
Remove
$
from function. If you want to protect function then write(function($) { ... })(jQuery);
– Milan and Friends Commented Jul 25, 2014 at 15:51 - @mdesdev Thanks, but it doesn't work for me. – Aleksi Tappura Commented Jul 25, 2014 at 16:26
4 Answers
Reset to default 5Remove overflow:auto property added to the container. This will work.
Your function takes a $
argument, but you're not passing anything in so it gets overwritten, you should do it like this:
$(document).ready(function() {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 100) {
$("body").addClass("scrolled");
} else {
$("body").removeClass("scrolled");
}
});
});
Here's a working shorthand solution, fiddle
$(function() {
$(window).scroll(function() {
var scroll = ($(this).scrollTop() > 100) ? $("body").addClass("scrolled") : $("body").removeClass("scrolled");
});
});
Also if you're using jQueryUI then you can add a little animation to class changing process with switchClass()
e.g.
var scroll = ($(this).scrollTop() >= 100) ? $("body").switchClass("default","scrolled", 1000) : $("body").switchClass("scrolled","default", 1000);
*Note: Also don't forget to include jQuery/jQueryUI libraries in your document.
Better JS:
$(function() {
$(window).on('scroll', function() {
$('body').toggleClass('scrolled', $(this).scrollTop() >= 100);
});
});
On your website, I see no css associated with .scrolled
anywhere even if I apply it via the console.
本文标签: javascriptWindow Scroll Function Not WorkingStack Overflow
版权声明:本文标题:javascript - Window Scroll Function Not Working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741631927a2389416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论