0

Okay, total noob here. I have a blog on a blogspot platform, and I'm trying to create a navbar that fades in when the user is scrolling down. I've made my navbar in flash, put it in the right place, made it transparent and fixed etc. It is a div. I've no idea what code to use and where exactly to put it to make it fade-in on scroll down. HELP :D http://blackforestdesigns.blogspot.com

1 Answers1

0

Add this code just before </body>.

<script>
var navbar = document.querySelector('.sticknav');
navbar.style.opacity = 0;
window.addEventListener('scroll', function() {
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    if(scrollTop > 400) {
        navbar.style.opacity = 1;
    }
    else {
        navbar.style.opacity = 0;
    }
});
</script>

If you want to get more adventurous, make a CSS class that animates the transition:

<style type="text/css">
.sticknav {
    opacity: 0;
    transition: opacity 0.8s ease;
}
.sticknav.is-visible {
    opacity: 1;
}
</style>

And replace the if/else above with:

if(scrollTop > 400) {
    navbar.classList.add('is-visible');
}
else {
    navbar.classList.remove('is-visible');
}
imcg
  • 2,601
  • 1
  • 18
  • 13
  • where exactly should I put this? – Noa Kruppa Aug 16 '14 at 11:51
  • imcg3 I'm sorry, I have nooooo idea what you're talking about:) – Noa Kruppa Aug 16 '14 at 12:04
  • so I've added the code above the

    between script tags. Nothing happened.

    – Noa Kruppa Aug 16 '14 at 12:08
  • added what @imcg suggested above the closing body tag, and this above closing body tag, and this is the code of my navbar, but nothing seems to happen: `
    `
    – Noa Kruppa Aug 16 '14 at 12:19
  • thanks so much for trying to help, but nothing seems to happen :( – Noa Kruppa Aug 16 '14 at 12:30
  • omg, it's actually working! What do I need to change to make it fade-in instead of out, and can I control when exactly (after how many pixels scrolled down) does it fade in? Thank you so much! – Noa Kruppa Aug 16 '14 at 12:58
  • thanks! It's working now, but every time I load my page, at the beginning it is visible, then on scrolling down it abruptly disappears and reappears again after I've scrolled down 400px. I want it to appear after 400px, but not to appear at the beginning, can you help me with this last bit? I'm so so thankful! – Noa Kruppa Aug 16 '14 at 13:35
  • I changed the htlm to is hidden but you can still see it when the page loads the first time. Maybe I'm not putting the html in the right place? I put it above the – Noa Kruppa Aug 16 '14 at 14:02
  • the new version should do it. make sure you changed both the css and javascript. – imcg Aug 16 '14 at 16:24