javascript 如何在到达特定 div 时停止滚动 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28170718/
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
How to stop scrolling of a div when it reaches on a specific div?
提问by user1881845
After clicking on a button im getting a pop up. What i want to achieve is to stop scrolling of this pop up when it reaches to the menu of my website.
单击按钮后,我会弹出一个窗口。我想要实现的是当它到达我网站的菜单时停止滚动这个弹出窗口。
here is my code
这是我的代码
<div id="dialog_box" class="dbox" style="display: none; position: fixed;
right: 192px ! important; z-index: 1000; top: 0px;">
I want to stop scrolling of #dialog_box when it reaches to #menu id div using jquery or java script.
当 #dialog_box 使用 jquery 或 java 脚本到达 #menu id div 时,我想停止滚动。
I have tried this so far but its not working
到目前为止我已经尝试过这个,但它不起作用
jQuery(window).scroll(function(){
jQuery('#dialog_box').scrollTo('#menu'); // i would like to stop scrolling of dialog_box when it meet the #menu id div.
jQuery("#dialog_box").css("top", Math.max(0, 162 -
jQuery(this).scrollTop())); // i have tried this to position dialog_box from top but its not giving me the exact result.
});
});
回答by Noman Ur Rehman
Here is a detailed tutorial on this.
这是有关此的详细教程。
I am sure this will help.
我相信这会有所帮助。
回答by Noman Ur Rehman
you can use the js code like this
你可以像这样使用js代码
var wrap = $("#wrap");
wrap.on("scroll", function(e) {
if (this.scrollTop > 100) {
wrap.addClass("class_name_of_div");
} else {
wrap.removeClass("class_name_of_div");
}
});
and in .CSS
并在 .CSS 中
.class_name_of_div{
position: fixed;
top: 10px;
}
try this will work
试试这会奏效