Javascript 滚动时切换粘性菜单类,页面上隐藏溢出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33228338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toggle class of sticky menu on scroll with overflow hidden on page
提问by Ashwani Shukla
I want to add a class .custom-menu-bg
to sticky menu .custom-menu
on scroll, while having overflow: hidden
on body. Here's my code :
我想在滚动时.custom-menu-bg
向粘性菜单添加一个类.custom-menu
,同时overflow: hidden
在身体上。这是我的代码:
<script type="text/javascript" src="css/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var _rys = jQuery.noConflict();
_rys("document").ready(function() {
_rys(window).scroll(function() {
if (_rys(this).scrollTop() > 1) {
_rys('.custom-menu').addClass("custom-menu-bg");
} else {
_rys('.custom-menu').removeClass("custom-menu-bg");
}
});
});
</script>
But this code doesn't work with overflow: hidden
on body
tag
so I tried :
但是此代码不适overflow: hidden
用于body
标记,所以我尝试了:
$('html').on('DOMMouseScroll', function(e) {
var delta = e.originalEvent.detail;
if (delta < 0) {
if ($('body').hasClass('section-element-1'))
$('.custom-menu').addClass("custom-menu-bg");
} else if (delta > 0) {
$('.custom-menu').removeClass("custom-menu-bg");
}
});
But this code only works for Mozilla and it's not a solution even, it's just a temp fix or work-around.
但是此代码仅适用于 Mozilla,它甚至不是解决方案,它只是临时修复或变通方法。
What I want is when I scroll down $('.custom-menu').addClass("custom-menu-bg");
i.e. custom-menu-bg
class gets added to custom-menu
.
我想要的是当我向下滚动时,$('.custom-menu').addClass("custom-menu-bg");
即custom-menu-bg
类被添加到custom-menu
.
And when I scroll up to the top $('.custom-menu').removeClass("custom-menu-bg");
i.e. custom-menu-bg
class gets removed from custom-menu
.
当我向上滚动到顶部时,$('.custom-menu').removeClass("custom-menu-bg");
即custom-menu-bg
类从custom-menu
.
The top of body
,document
,window
etcetera is always 0
.
And top of my div
with class custom-menu
also has top: 0
always.
body
、document
、window
等的顶部始终是0
。而我div
的同班之巅custom-menu
也top: 0
一直如此。
I'm looking for a permanent solution which works on all browsers.
我正在寻找适用于所有浏览器的永久解决方案。
采纳答案by Ahmad Baktash Hayeri
I've reproduced the same effect you wanted HERE.
我已经复制了您想要的相同效果HERE。
The only change that I've brought in comparison to your code is that I've made a makeshift body div
and applied overflow: hidden
on it.
与您的代码相比,我带来的唯一更改是我制作了一个临时主体div
并对其进行了应用overflow: hidden
。
Then, using jQuery, you'll be checking for the scroll
event triggered by a wrapperinside the body div
- which is in charge of holding the content) - and not by itself (or even document
).
然后,使用 jQuery,您将检查scroll
由主体内部的包装器触发的事件div
- 负责保存内容) - 而不是它本身(甚至document
)。
$('.wrapper').scroll(function () {
if ($(this).scrollTop() > 0) {
$('.custom-menu').addClass("custom-menu-bg");
} else {
$('.custom-menu').removeClass("custom-menu-bg");
}
});
This is because the makeshift body div has an overflow
property set to hidden
, and therefore won't generate that particular scroll
event (maybe it would if you had the handler registered using browser-specific scroll events). Whereas the inner wrapper
div will always have it's height
property determined by it's content and is therefore scrollable.
这是因为临时主体 div 的overflow
属性设置为hidden
,因此不会生成该特定scroll
事件(如果您使用特定于浏览器的滚动事件注册了处理程序,可能会这样)。而内部wrapper
div 将始终具有height
由其内容决定的属性,因此是scrollable。
NOTE:jQuery's scroll()
is cross-browser, and hence a permanentsolution.
注意:jQueryscroll()
是跨浏览器的,因此是一个永久的解决方案。
回答by Keval Bhatt
You can bind on any id or on class also . its on you for now demo i am using window .
您也可以绑定任何 id 或类。现在由你来演示我正在使用 window 。
This single event works for both if you have scroll or not. i.e overflow:hidden or overflow:scroll
无论您是否滚动,此单个事件都适用于两者。即溢出:隐藏或溢出:滚动
$(window).bind('mousewheel DOMMouseScroll', function(event){
if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
// scroll up
$('.custom-menu').removeClass("custom-menu-bg");
}
else {
// scroll down
$('.custom-menu').addClass("custom-menu-bg");
}
});
.custom-menu {
background-color: black;
height: 100px;
width: 100%
}
.custom-menu-bg{
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-menu">
</div>
Or you can also use this jQuery mousewheel plugin https://github.com/brandonaaron/jquery-mousewheel.
或者你也可以使用这个 jQuery mousewheel 插件https://github.com/brandonaaron/jquery-mousewheel。