滚动锚点未激活
Scroll anchor not activating
当我滚动并且锚点接触到窗口顶部时,框的背景颜色应该会改变,但我似乎无法使其正常工作。
https://jsfiddle.net/6p2pnnq4/1/
var scrollFn = function() {
var targetOffset = $(this).find(".anchor-point")[0].offsetTop;
console.log('Scrolling...');
if ($(this).scrollTop() > targetOffset) {
$(this).find(".footer_wrap").addClass("topper");
} else {
$(this).find(".footer_wrap").removeClass("topper");
}
};
$(window).scroll(scrollFn);
您不需要
$(this).find
这是无用的,尝试以下操作:
var targetOffset = $('#footer_wrap').offset().top,
$window = $(window);
$(window).on( 'scroll', function(){
if ( $window.scrollTop() >= targetOffset ) {
$("#footer_wrap").addClass("topper");
}else{
$("#footer_wrap").removeClass("topper");
}
});
和CSS
#footer_wrap {
margin-top: 200px;
height: 130vmax;
background-color: #f4f4f4;
-ms-transition: all 300ms ease-in-out;
-webkit-transition: all 300ms ease-in-out;
-moz-transition: all 300ms ease-in-out;
-o-transition: all 300ms ease-in-out;
transition: all 300ms ease-in-out;
}
#footer_wrap.topper {
background-color: #000;
}
然后,当#footer_wrap位于顶部时,它将更改背景。
查看Fiddle
相关文章:
- 幻灯片滚动javascript不起作用
- 将视口底部滚动到元素底部
- jQuery Lazy加载动画滚动
- 设置滑块分区上的滚动
- 结合jQuery和jetpack无限滚动
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- Javascript滚动激活
- 如何在激活灯箱插件时禁用鼠标滚轮的背景滚动
- 当目标处于顶部滚动状态时,如何从菜单中激活项目
- 滚动功能在滚动前激活
- 仅在移动设备上激活滚动深度
- 滚动锚点未激活
- 在fullpage.js中重新激活自动滚动
- 我如何编程一个JS按钮,它也可以在点击和滚动时激活转换
- 如何在gif动画完成后激活页面滚动
- jQuery激活(绑定)滚动后解除绑定
- 滚动时,导航条悬停在所有链接上激活
- 滚动功能在看到元素时激活
- 将按钮激活的图像滚动器制作成自动滚动器
- 视差滚动,动画WITH滚动而不是在滚动时激活