在类上滚动时更改导航
Change nav when scrolled over class
下面有一段代码,用于更改固定导航的颜色,以匹配用户滚动经过的div。
我遇到的问题是,在整个页面中有多个相同元素的实例,当用户不在其上时,它们都试图删除背景类。
我怎样才能让这个触发,只有当使用滚动离开元素,而不是当它是在区域之外?
我是jQuery新手,所以任何建议都会非常感激。谢谢!
$(window).scroll(function(){
$(".banner").each(function(){
var windowScroll = $(window).scrollTop(),
bannerTop = $(this).offset().top,
bannerHeight = $(this).outerHeight(),
bannerbottom = (bannerTop + bannerHeight),
bgColor = $(this).attr("primary-colour");
// When the window scrolls over the banner then change the nav colour
if ((windowScroll >= bannerTop) && (windowScroll <= bannerbottom)) {
$(".body-header").css("background", bgColor);
}
else {
$(".body-header").css("background", "");
}
});
});
https://jsfiddle.net/x789qh7m/1/你的代码将只工作,当标题在最后一个横幅。它不能正常工作,因为当滚动时,你在上迭代所有横幅。所以即使第一个横幅设置了颜色,.each
将迭代直到最后一个(橙色),它将重置它。
如果我正确理解你,这是你的解决方案:https://jsfiddle.net/x789qh7m/3/-我只是把else
的身体在横幅迭代之前。算法很简单-滚动设置默认没有背景,如果下面有一些横幅-然后将背景更改为相同的颜色。
$(window).scroll(function () {
$(".body-header").css("background", "");
$(".body-header").removeClass("invert");
$(".banner").each(function () {
var windowScroll = $(window).scrollTop(),
bannerTop = $(this).offset().top,
bannerHeight = $(this).outerHeight(),
bannerbottom = (bannerTop + bannerHeight),
bgColor = $(this).attr("primary-colour");
if ((windowScroll >= bannerTop) && (windowScroll <= bannerbottom)) {
$(".body-header").css("background", bgColor);
$(".body-header").addClass("invert");
}
});
});
返回false跳出each
循环。不能遍历所有,因为你的else覆盖了background css属性的值:
$(window).scroll(function() {
$(".banner").each(function() {
var windowScroll = $(window).scrollTop(),
bannerTop = $(this).offset().top,
bannerHeight = $(this).outerHeight(),
bannerbottom = (bannerTop + bannerHeight),
bgColor = $(this).attr("data-colour");
// When the window scrolls over the banner then change the nav colour
if ((windowScroll >= bannerTop) && (windowScroll <= bannerbottom)) {
$(".body-header").css("background", bgColor);
$(".body-header").addClass("invert");
return false;
} else {
$(".body-header").css("background", "");
$(".body-header").removeClass("invert");
}
});
});
.body-header {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 20px;
border: 1px solid;
}
.banner {
margin-bottom: 150px;
}
.banner:nth-child(5) {
margin-bottom: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="body-header"></nav>
<section class="banner" data-colour="rgb(52, 49, 57)">
<div style="background: rgb(52, 49, 57); width: 100%; height: 200px"></div>
</section>
<section class="banner" data-colour="rgb(234, 104, 52)">
<div style="background: rgb(234, 104, 52); height: 200px"></div>
</section>
<section class="banner" data-colour="rgb(14, 104, 52)">
<div style="background: rgb(14, 104, 52); height: 200px"></div>
</section>
相关文章:
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 如何在定位导航后向下滚动页面
- fullpage.js和物化.js;使用实体侧导航禁用鼠标滚动
- 如何创建可以向下滚动的导航
- 缩小并更改滚动时的背景导航栏
- 当用户已经向下滚动页面时,我如何导航到锚标记
- 具有子菜单和平滑滚动的粘性导航
- 如何让左侧导航跟随用户向下滚动页面
- JQuery 滚动列表框模仿键盘导航
- 导航栏在向下滚动时会更改背景,但在向上滚动时不会更改回来
- 在离子滚动时隐藏离子导航栏
- 导航到新页面不会滚动到顶部
- 猫头鹰旋转木马需要滚动效果的导航悬停
- AngularJS指令:将jQuery导航滚动变成指令
- 使用主导航滚动网站问题
- 导航滚动到子链接时需要使用相同的颜色
- 一个页面的网站hashbang导航滚动
- 导航滚动与javascript
- 如何控制js导航滚动的速度
- 离开画布的导航滚动问题