admin管理员组文章数量:1323497
I Just did that way with pure javascript to change navbar color after scrolling, it worked with google chrome without any problem but when I tried to test it at firefox it's not working with it.
Anyone can give me any advice about this ? Thanks in advance.
var myNav = document.getElementById("nav");
window.onscroll = function () {
"use strict";
if (document.body.scrollTop >= 280) {
myNav.classList.add("scroll");
} else {
myNav.classList.remove("scroll");
}
};
body {
margin:0;
padding:0;
height:4000px;
}
.nav {
position:fixed;
width:100%;
height:60px;
background-color:#111;
transition:all .5s ease-in-out;
}
.scroll {
background-color:transparent;
}
<script src=".1.1/jquery.min.js"></script>
<div id="nav" class="nav"></div>
I Just did that way with pure javascript to change navbar color after scrolling, it worked with google chrome without any problem but when I tried to test it at firefox it's not working with it.
Anyone can give me any advice about this ? Thanks in advance.
var myNav = document.getElementById("nav");
window.onscroll = function () {
"use strict";
if (document.body.scrollTop >= 280) {
myNav.classList.add("scroll");
} else {
myNav.classList.remove("scroll");
}
};
body {
margin:0;
padding:0;
height:4000px;
}
.nav {
position:fixed;
width:100%;
height:60px;
background-color:#111;
transition:all .5s ease-in-out;
}
.scroll {
background-color:transparent;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nav" class="nav"></div>
Share
edited Oct 1, 2016 at 2:50
kukkuz
42.4k6 gold badges64 silver badges102 bronze badges
asked Oct 1, 2016 at 2:25
Yahya EssamYahya Essam
1,9122 gold badges25 silver badges44 bronze badges
3 Answers
Reset to default 3If you are using pure javascript:
Firefox and IE has the overflow at html
while Chrome, Safari and Opera places it at the body
level:
See here for more info:
var body = document.body; // For Chrome, Safari and Opera
var html = document.documentElement; // Firefox and IE places the overflow at the level, unless else is specified. Therefore, we use the documentElement property for these two browsers
So you must use this:
if (document.body.scrollTop >= 280 || document.documentElement.scrollTop >= 280) {
and it will work cross-browser. Cheers!
var myNav = document.getElementById("nav");
window.onscroll = function() {
"use strict";
if (document.body.scrollTop >= 280 || document.documentElement.scrollTop >= 280) {
myNav.classList.add("scroll");
} else {
myNav.classList.remove("scroll");
}
};
body {
margin: 0;
padding: 0;
height: 4000px;
}
.nav {
position: fixed;
width: 100%;
height: 60px;
background-color: #111;
transition: all .5s ease-in-out;
}
.scroll {
background-color: transparent;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nav" class="nav"></div>
If you are using jquery:
var myNav = $("#nav");
$(window).on('scroll', function() {
"use strict";
if ($(window).scrollTop() >= 280) {
myNav.addClass("scroll");
} else {
myNav.removeClass("scroll");
}
});
body {
margin: 0;
padding: 0;
height: 4000px;
}
.nav {
position: fixed;
width: 100%;
height: 60px;
background-color: #111;
transition: all .5s ease-in-out;
}
.scroll {
background-color: transparent;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nav" class="nav"></div>
$(window).scroll(function() {
if ($(this).scrollTop() > 20){
$('#navBar').css({property: "value"});
} else {
$('#navBar').css({property: "value"});
}
});
Not really an answer, but this works great for me in all browsers that I've tested on (ie, edge, safari, chrome & firefox)
Try this. Change the "280" to how many px down you want to scroll before the navbar background color changes to transparent.
HTML:
<div id="navbar"></div>
JavaScript:
$(function() {
$(window).on("scroll", function() {
if($(window).scrollTop() > 280) {
//background on scroll
$("#navbar").addClass("scroll");
} else {
//background at top
$("#navbar").removeClass("scroll");
}
});
});
CSS:
#navbar {
background-color:#111;
transition:all .5s ease-in-out;
}
.scroll {
background-color:transparent;
}
本文标签: javascriptChange navbar color while scrollingStack Overflow
版权声明:本文标题:javascript - Change navbar color while scrolling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742143653a2422692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论