so i’m redesigning our companys wordpress webpage with the divi builder and for the last two days i was searching for a way to get the nav bar to change css class upon scrolling.
I found a lot of info about this topic on various sites including stackoverflow but i can’t seem to find a solution specific to the Divi builder.
i tried to mess around with the provided html, css and javascript from this link and some other sites, but i can’t get it to work:
http://codepen.io/taylorleejones/pen/KJsvz
HTML:
<nav class="nav">
<a href="#" class="logo">[logo]</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>
CSS:
.nav {
background-color:transparent;
color:#fff;
transition: all 0.25s ease;
position:fixed;
top:0;
width:100%;
background-color:#ccc;
padding:1em 0;
/* make sure to add vendor prefixes here */
}
.nav.past-main {
background-color:#fff;
color:#444;
}
#main {
height:500px;
background-color:red;
}
#below-main {
height:1000px;
background-color:#eee;
}
Javascript:
// get the value of the bottom of the #main element by adding the offset of that element plus its height, set it as a variable
var mainbottom = $('#main').offset().top + $('#main').height();
// on scroll,
$(window).on('scroll',function(){
// we round here to reduce a little workload
stop = Math.round($(window).scrollTop());
if (stop > mainbottom) {
$('.nav').addClass('past-main');
} else {
$('.nav').removeClass('past-main');
}
});
Here is an example how the result should turn out:
https://www.arvato.com/de.html
I want our nav bar to do the same thing, be transparent when on top and turn white when scrolling down or reaching a certain point.
I guess my biggest problem is that i don’t know where add html in divi.
Thanks in advance for the answers and help.
2
Answers
Okay after a good 1-2 hours of debugging a coworker and i managed to make it work.
The problem is you can't add html direcly in divi, so we used some Javascript to get around that, made some small adjustments to the CSS and the jquery link was at the wrong position so we moved it to the correct section.
Here is the working Code if anyone needs it in the future.
CSS:
Javascript:
The CSS gets added in the "Costum CSS" section of the Divi ePanel and the Javascript needs to be in the "Body" section of the "Integration Tab" from the "ePanel", not in the Head section.
The scroll script you have at the moment requires JQuery, do you have JQuery on the site?
If you add an ID to your Nav (“nav”) bar this should work.
You can run the snippet to see it working. You would have to change the ’25’ to whatever the height of your nav bar is in pixels.