Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery navigation color and height change on scroll

I'm looking to change the navigation from transparent to a color on scroll. Much like this site. http://createone.com/contact-us/ I've seen posts about changing size which is great, I'll use this as well but would like to mainly go from transparent to a color. Any help would be great. I do have a little experience with jquery but haven't been able to figure it out or modify someone else's previous questions to my needs.

I saw this but wasn't able to get it to work for me. jquery change opacity and height on scroll

A jsfiddle demo would be great!

Thanks in advance for the help. Also I'm using Bootstrap 4 so if there's any plugins out there. I'm open to that as well.

like image 489
znels Avatar asked Dec 07 '25 00:12

znels


1 Answers

Hi you can check this to begin : http://jsfiddle.net/rcAev/177/

Here i made this function:

$(document).ready (function () {
 $(window).scroll (function () {
    var sT = $(this).scrollTop();
        if (sT >= 300) {
            $('.overlay').addClass('cambio')
        }else {
            $('.overlay').removeClass('cambio')
        }
  })
})

I'll explain you step by step:

  • First this executes the function every time you scroll the window

$(window).scroll (function ()

  • Second read the value from the top of the scroll to know how much you advance

var sT = $(this).scrollTop();

  • Third compare to the breakpoint you want , in this case i choose 300 because i want to change the nav after pass the height form the image.

    if (sT >= 300) {
            /*action you want after the 300 or more scroll*/
        }else {
            /*action you want before the 300 scroll*/
        }
    
  • Fourth to change the transparent to color the action i apply is add a class with new background and different height:

    $('.overlay').addClass('cambio')
    
like image 101
DaniP Avatar answered Dec 08 '25 13:12

DaniP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!