Navbar change color javascript not working
up vote
0
down vote
favorite
as other here I try to modify the navbar during scrolling down. I read this question:
Changing nav-bar color after scrolling?
Transition in Navbar when Scroll Down
Bootstrap navbar change color to the scroll
I can't make it work on my website, I don't understand the issue:
HTML:
<!-- Navbar -->
<nav class="navbar justify-content-center navbar-expand-sm navbar-dark fixed-top navbar-custom">
<!-- Menu Links -->
<ul class="navbar-nav" >
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<!-- Wanted Logo -->
<a class="navbar-brand" href="#">
<img src="img/logo-light.png" alt="wanted_logo" style="width: 3vw;">
</a>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<!-- End Navbar -->
CSS:
.navbar-custom {
background-color: rgba(0,0,0,0.5);
}
.navbar-custom ul li{
text-transform: uppercase;
font-size: 14px;
color: #fff;
padding-top: 34px;
}
.navbar-custom img{
margin-left: 20px;
margin-right: 7px;
}
.navbar-custom.scrolled {
background-color: red !important;
transition: background-color 200ms linear;
}
JS:
<script>$(function () {
$(document).scroll(function () {
var $nav = $(".navbar-custom");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
From my understanding when scrolling down further than the size of the navbar it should change color.
I though it could be a issue because the height of the navbar is not explicitly defined here so I tried adding:
CSS
.navbar-custom{
background-color: rgba(0,0,0,0.5);
height: 100px;
}
It still didn't work, so I tried also to use another version of the JS proposed here:Changing nav-bar color after scrolling?
JS:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
$(".navbar-custom").css("background-color", "#f8f8f8");
} else {
$(".navbar-custom").css("background-color", "blue");
}
});
});
</script>
Same result nothing changes.
I am new using JS what doesn't work here ?
javascript html css twitter-bootstrap navbar
add a comment |
up vote
0
down vote
favorite
as other here I try to modify the navbar during scrolling down. I read this question:
Changing nav-bar color after scrolling?
Transition in Navbar when Scroll Down
Bootstrap navbar change color to the scroll
I can't make it work on my website, I don't understand the issue:
HTML:
<!-- Navbar -->
<nav class="navbar justify-content-center navbar-expand-sm navbar-dark fixed-top navbar-custom">
<!-- Menu Links -->
<ul class="navbar-nav" >
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<!-- Wanted Logo -->
<a class="navbar-brand" href="#">
<img src="img/logo-light.png" alt="wanted_logo" style="width: 3vw;">
</a>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<!-- End Navbar -->
CSS:
.navbar-custom {
background-color: rgba(0,0,0,0.5);
}
.navbar-custom ul li{
text-transform: uppercase;
font-size: 14px;
color: #fff;
padding-top: 34px;
}
.navbar-custom img{
margin-left: 20px;
margin-right: 7px;
}
.navbar-custom.scrolled {
background-color: red !important;
transition: background-color 200ms linear;
}
JS:
<script>$(function () {
$(document).scroll(function () {
var $nav = $(".navbar-custom");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
From my understanding when scrolling down further than the size of the navbar it should change color.
I though it could be a issue because the height of the navbar is not explicitly defined here so I tried adding:
CSS
.navbar-custom{
background-color: rgba(0,0,0,0.5);
height: 100px;
}
It still didn't work, so I tried also to use another version of the JS proposed here:Changing nav-bar color after scrolling?
JS:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
$(".navbar-custom").css("background-color", "#f8f8f8");
} else {
$(".navbar-custom").css("background-color", "blue");
}
});
});
</script>
Same result nothing changes.
I am new using JS what doesn't work here ?
javascript html css twitter-bootstrap navbar
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
as other here I try to modify the navbar during scrolling down. I read this question:
Changing nav-bar color after scrolling?
Transition in Navbar when Scroll Down
Bootstrap navbar change color to the scroll
I can't make it work on my website, I don't understand the issue:
HTML:
<!-- Navbar -->
<nav class="navbar justify-content-center navbar-expand-sm navbar-dark fixed-top navbar-custom">
<!-- Menu Links -->
<ul class="navbar-nav" >
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<!-- Wanted Logo -->
<a class="navbar-brand" href="#">
<img src="img/logo-light.png" alt="wanted_logo" style="width: 3vw;">
</a>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<!-- End Navbar -->
CSS:
.navbar-custom {
background-color: rgba(0,0,0,0.5);
}
.navbar-custom ul li{
text-transform: uppercase;
font-size: 14px;
color: #fff;
padding-top: 34px;
}
.navbar-custom img{
margin-left: 20px;
margin-right: 7px;
}
.navbar-custom.scrolled {
background-color: red !important;
transition: background-color 200ms linear;
}
JS:
<script>$(function () {
$(document).scroll(function () {
var $nav = $(".navbar-custom");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
From my understanding when scrolling down further than the size of the navbar it should change color.
I though it could be a issue because the height of the navbar is not explicitly defined here so I tried adding:
CSS
.navbar-custom{
background-color: rgba(0,0,0,0.5);
height: 100px;
}
It still didn't work, so I tried also to use another version of the JS proposed here:Changing nav-bar color after scrolling?
JS:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
$(".navbar-custom").css("background-color", "#f8f8f8");
} else {
$(".navbar-custom").css("background-color", "blue");
}
});
});
</script>
Same result nothing changes.
I am new using JS what doesn't work here ?
javascript html css twitter-bootstrap navbar
as other here I try to modify the navbar during scrolling down. I read this question:
Changing nav-bar color after scrolling?
Transition in Navbar when Scroll Down
Bootstrap navbar change color to the scroll
I can't make it work on my website, I don't understand the issue:
HTML:
<!-- Navbar -->
<nav class="navbar justify-content-center navbar-expand-sm navbar-dark fixed-top navbar-custom">
<!-- Menu Links -->
<ul class="navbar-nav" >
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<!-- Wanted Logo -->
<a class="navbar-brand" href="#">
<img src="img/logo-light.png" alt="wanted_logo" style="width: 3vw;">
</a>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<!-- End Navbar -->
CSS:
.navbar-custom {
background-color: rgba(0,0,0,0.5);
}
.navbar-custom ul li{
text-transform: uppercase;
font-size: 14px;
color: #fff;
padding-top: 34px;
}
.navbar-custom img{
margin-left: 20px;
margin-right: 7px;
}
.navbar-custom.scrolled {
background-color: red !important;
transition: background-color 200ms linear;
}
JS:
<script>$(function () {
$(document).scroll(function () {
var $nav = $(".navbar-custom");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
From my understanding when scrolling down further than the size of the navbar it should change color.
I though it could be a issue because the height of the navbar is not explicitly defined here so I tried adding:
CSS
.navbar-custom{
background-color: rgba(0,0,0,0.5);
height: 100px;
}
It still didn't work, so I tried also to use another version of the JS proposed here:Changing nav-bar color after scrolling?
JS:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
$(".navbar-custom").css("background-color", "#f8f8f8");
} else {
$(".navbar-custom").css("background-color", "blue");
}
});
});
</script>
Same result nothing changes.
I am new using JS what doesn't work here ?
javascript html css twitter-bootstrap navbar
javascript html css twitter-bootstrap navbar
asked Nov 12 at 2:27
Unic0
95
95
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
It's because you're using inconsistent scrolling. You have
$(window).scroll(function() {...});
However you're checking it with
if ($(document).scrollTop() > 100) {...}
And you also have:
$(document).ready(function() {...});
Make these all either $(window)
or $(document)
, and it will work.
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
add a comment |
up vote
0
down vote
Finally, I made it work using this (as part of the w3 back-to-top button tutorial):
JS:
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
var $nav = $(".navbar-custom");
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 50) {
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
}
}
</script>
Link to the w3school back to top button: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
Thank you
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255275%2fnavbar-change-color-javascript-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It's because you're using inconsistent scrolling. You have
$(window).scroll(function() {...});
However you're checking it with
if ($(document).scrollTop() > 100) {...}
And you also have:
$(document).ready(function() {...});
Make these all either $(window)
or $(document)
, and it will work.
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
add a comment |
up vote
0
down vote
It's because you're using inconsistent scrolling. You have
$(window).scroll(function() {...});
However you're checking it with
if ($(document).scrollTop() > 100) {...}
And you also have:
$(document).ready(function() {...});
Make these all either $(window)
or $(document)
, and it will work.
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
add a comment |
up vote
0
down vote
up vote
0
down vote
It's because you're using inconsistent scrolling. You have
$(window).scroll(function() {...});
However you're checking it with
if ($(document).scrollTop() > 100) {...}
And you also have:
$(document).ready(function() {...});
Make these all either $(window)
or $(document)
, and it will work.
It's because you're using inconsistent scrolling. You have
$(window).scroll(function() {...});
However you're checking it with
if ($(document).scrollTop() > 100) {...}
And you also have:
$(document).ready(function() {...});
Make these all either $(window)
or $(document)
, and it will work.
answered Nov 12 at 2:34
Jack Bashford
4,20131233
4,20131233
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
add a comment |
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
But then why this doesn't work: <script>$(function () { $(document).scroll(function () { var $nav = $(".navbar-custom"); $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height()); }); }); </script>
– Unic0
Nov 12 at 2:41
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Because you are checking different things to what your functions are focused on.
– Jack Bashford
Nov 12 at 2:42
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
Hum.. I don't fully understand. And I tried to change all the $(document) for $(window) it didn't work then I change to all $(document) and it didn't work either :/ By the way I don't use the 2 scripts at the same time.
– Unic0
Nov 12 at 2:50
add a comment |
up vote
0
down vote
Finally, I made it work using this (as part of the w3 back-to-top button tutorial):
JS:
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
var $nav = $(".navbar-custom");
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 50) {
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
}
}
</script>
Link to the w3school back to top button: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
Thank you
add a comment |
up vote
0
down vote
Finally, I made it work using this (as part of the w3 back-to-top button tutorial):
JS:
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
var $nav = $(".navbar-custom");
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 50) {
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
}
}
</script>
Link to the w3school back to top button: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
Thank you
add a comment |
up vote
0
down vote
up vote
0
down vote
Finally, I made it work using this (as part of the w3 back-to-top button tutorial):
JS:
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
var $nav = $(".navbar-custom");
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 50) {
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
}
}
</script>
Link to the w3school back to top button: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
Thank you
Finally, I made it work using this (as part of the w3 back-to-top button tutorial):
JS:
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
var $nav = $(".navbar-custom");
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 50) {
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
}
}
</script>
Link to the w3school back to top button: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
Thank you
answered Nov 12 at 22:07
Unic0
95
95
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255275%2fnavbar-change-color-javascript-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown