当用户滚动页面时,我使用此代码隐藏阻止。如何在外面滚动时隐藏块。
$('html, body').bind('scroll',function (e) {
var container = $("CONTAINER SELECTOR");
if (container.has(e.target).length === 0){
container.hide();
}
});
这是我的HTML
<div class="parent_block">
<div class="menu-button">
<img src="/img.svg" />
</div>
<div class="toggled_block">
<nav><a href="/">Home</a></nav>
</div>
</div>
<script>
$(".menu-button").click(function() {
$('.toggled_block').toggle();
});
$(document).on('click', function(e) {
if (!$(e.target).closest(".parent_block").length) {
$('.toggled_block').hide();
}
e.stopPropagation();
});
$(document).bind('scroll',function (e) {
var container = $(".toggled_block");
if (container.has(e.target).length === 0){
container.hide();
}
});