在单个条件下检查滚动顶部和 id 偏移量
checking both scrollTop and id offset in single condition
我想检查滚动位置是否大于 350,并且在相同条件下菜单粘性偏移位置为 92。我给了我的一段代码
$(window).scroll(function(){
if(($(document).scrollTop() >= 350) && (($('.menuSticky').offset().top )==92)) {
console.log('true');
$('.dropdown').hover(function() {
$('.secMenu').hide();
$(this).toggleClass("open");
}).mouseleave(function(){
$('.secMenu').show();
});
}
});
尝试将 $(document).scrollTop() 替换为 $(window).scrollTop()
$(window).scroll(function() {
if (($(window).scrollTop() >= 350) && (($('.menuSticky').offset().top) == 92)) {
console.log('true');
$('.dropdown').hover(function() {
$('.secMenu').hide();
$(this).toggleClass("open");
}).mouseleave(function() {
$('.secMenu').show();
});
}
});
您的代码没有任何问题,在以下代码片段中,我在没有任何编辑的情况下使用您的代码,但我给出了.menuSticky
margin-top:92px;
,因此您的第二个条件将为真并且工作正常.
$(window).scroll(function(){
if(($(document).scrollTop() >= 350) && (($('.menuSticky').offset().top )==92)) {
console.log('true');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="menuSticky" style="margin-top:92px"> Menu </div>
<div style="height:1000px;">Some content</div>
相关文章:
- 添加新数据时D3.JS条形图列偏移量
- 当偏移量改变时滚动顶部
- 引导数据偏移量底部
- 可以在调整窗口大小时重新调整stellar.js的元素偏移量
- 可滚动元素,如何在调整大小时更改顶部偏移量
- 使用JavaScript日期的任何时间的时区偏移量
- 时刻时区:UTC 偏移量差异
- jQuery偏移量顶部没有'工作不正常
- 如何获取当前时区偏移量(并正确格式化)
- d3.mouse 偏移量返回的值不正确
- jQuery根据鼠标位置计算DIV偏移量和边界
- 更改 for 循环中 SVG 行的笔触虚线偏移量
- 时区偏移量计算
- jquery 如何设置偏移量 () 值
- 在javascript中向本地时间戳添加时间戳偏移量
- jQuery平滑滚动到相同页面和其他页面上的ID:如何设置偏移量
- 滚动到页面上的 ID 未正确计算偏移量
- 比较 2 列表得到移动 ID + 偏移量 + 方向
- 在单个条件下检查滚动顶部和 id 偏移量
- 当页面加载时,会出现平滑滚动,当ID和哈希值设置相同时,偏移量会跳跃