admin管理员组文章数量:1310254
I would like to change the colour of my navigation bar to suit the colour of the section scrolling past, and in reverse when scrolling the other way.
For example, if the navigation bar is currently dark and the next section to hit the navigation bar is light, I would like to change the nvigation bar to dark.
This is what I have done so far which I feel is relatively close. Please advise what is missing to make this work:
HTML
<nav>Navigation</nav>
<section data-section-color="dark">Section 1</section>
<section data-section-color="light">Section 2</section>
<section data-section-color="dark">Section 3</section>
<section data-section-color="light">Section 4</section>
<section data-section-color="dark">Section 5</section>
<section data-section-color="light">Section 6</section>
SCSS
$dark: #2B3336;
$light: #EEEEEE;
body {
padding-top: 67px;
margin: 0;
}
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 67px;
background: $dark;
color: $light;
padding: 0 25px;
line-height: 67px;
&.dark {
background: $dark;
color: $light;
}
&.light {
background: $light;
color: $dark;
}
}
section {
height: 400px;
padding: 25px;
&[data-section-color="dark"] {
background: $dark;
color: $light;
}
&[data-section-color="light"] {
background: $light;
color: $dark;
}
}
jQuery
$(window).on('scroll',function() {
var $nav = $('nav');
var navHeight = $nav.height();
var amountScrolled = $(window).scrollTop();
$('section[data-section-color]').each(function() {
var thisTop = $(this).offset().top;
if ((thisTop - navHeight) == 0) {
var color = $(this).data('section-color');
$nav.addClass(color);
console.log(color);
}
});
});
JSFiddle:
JSFiddle
I would like to change the colour of my navigation bar to suit the colour of the section scrolling past, and in reverse when scrolling the other way.
For example, if the navigation bar is currently dark and the next section to hit the navigation bar is light, I would like to change the nvigation bar to dark.
This is what I have done so far which I feel is relatively close. Please advise what is missing to make this work:
HTML
<nav>Navigation</nav>
<section data-section-color="dark">Section 1</section>
<section data-section-color="light">Section 2</section>
<section data-section-color="dark">Section 3</section>
<section data-section-color="light">Section 4</section>
<section data-section-color="dark">Section 5</section>
<section data-section-color="light">Section 6</section>
SCSS
$dark: #2B3336;
$light: #EEEEEE;
body {
padding-top: 67px;
margin: 0;
}
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 67px;
background: $dark;
color: $light;
padding: 0 25px;
line-height: 67px;
&.dark {
background: $dark;
color: $light;
}
&.light {
background: $light;
color: $dark;
}
}
section {
height: 400px;
padding: 25px;
&[data-section-color="dark"] {
background: $dark;
color: $light;
}
&[data-section-color="light"] {
background: $light;
color: $dark;
}
}
jQuery
$(window).on('scroll',function() {
var $nav = $('nav');
var navHeight = $nav.height();
var amountScrolled = $(window).scrollTop();
$('section[data-section-color]').each(function() {
var thisTop = $(this).offset().top;
if ((thisTop - navHeight) == 0) {
var color = $(this).data('section-color');
$nav.addClass(color);
console.log(color);
}
});
});
JSFiddle:
JSFiddle
Share Improve this question asked Feb 2 at 16:20 TheCarverTheCarver 19.7k27 gold badges103 silver badges153 bronze badges2 Answers
Reset to default 0jQuery:
var $nav = $("nav")
var navHeight = $nav.height()
$(window).on("scroll", function () {
var amountScrolled = $(window).scrollTop()
$("section[data-section-color]").each(function () {
var thisTop = $(this).offset().top
var thisHeight = $(this).height()
if (
parseInt(thisTop - navHeight) < amountScrolled &&
parseInt(thisTop + thisHeight - navHeight) > amountScrolled
) {
var color = $(this).data("section-color")
$nav.attr("class", color)
}
})
})
JSFiddle:
JSFiddle
The math ((thisTop - navHeight) == 0
) your using to target the current section is probably a little too restricted.
The following trys to determine if the nav is contained within the sections bounds.
$(window).on('scroll',function() {
var $nav = $('nav');
var navHeight = $nav.height();
var navTop = $nav.offset().top;
$('section[data-section-color]').each(function() {
var thisTop = $(this).offset().top;
var thisHeight = $(this).height();
var color = $(this).data('section-color');
if ((navTop >= thisTop) && (navTop+navHeight <= thisTop+thisHeight)) {
$nav.removeClass(); // dark + light = ???
$nav.addClass(color);
console.log(color);
return false // stop trying after weve found a match
}
});
});
Had another go, using getBoundingClientRect()
$(window).on('scroll', function() {
let nav = $('nav')
let navRect = nav[0].getBoundingClientRect()
$('section[data-section-color]').each(function() {
let itemRect = $(this)[0].getBoundingClientRect();
if (navRect.bottom >= itemRect.top && navRect.top <= itemRect.bottom) {
nav.removeClass(['dark','light'])
if ($(this).data('section-color') === 'light') {
nav.addClass('light')
} else {
nav.addClass('dark')
}
}
})
})
本文标签: htmlMake nav bar color match color of sections when scrollingStack Overflow
版权声明:本文标题:html - Make nav bar color match color of sections when scrolling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741841601a2400533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论