简体   繁体   English

滚动时jquery导航颜色和高度更改

[英]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. http://createone.com/contact-us/我看过关于改变尺寸的帖子很棒,我也会使用它,但我想主要从透明到颜色。 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. 我对jquery有一点经验,但是无法弄明白或者根据我的需要修改别人以前的问题。

I saw this but wasn't able to get it to work for me. 我看到了这个但却无法让它为我工作。 jquery change opacity and height on scroll jquery在滚动时改变不透明度和高度

A jsfiddle demo would be great! 一个jsfiddle演示会很棒!

Thanks in advance for the help. 在此先感谢您的帮助。 Also I'm using Bootstrap 4 so if there's any plugins out there. 我也在使用Bootstrap 4,所以如果那里有任何插件。 I'm open to that as well. 我也对此持开放态度。

Hi you can check this to begin : http://jsfiddle.net/rcAev/177/ 嗨,你可以检查这个开始: 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 () $(window).scroll(function()

  • Second read the value from the top of the scroll to know how much you advance 第二个读取滚动顶部的值,以了解您前进了多少

var sT = $(this).scrollTop(); 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. 第三个比较你想要的断点,在这种情况下我选择300,因为我想在从图像传递高度后更改导航。

     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: 四是改变透明上色我申请的行为是增加一个class有新的背景和不同的高度:

     $('.overlay').addClass('cambio') 

In this site , the navigation height is not actually changing. 在此站点中 ,导航高度实际上并未更改。 It's fixed positioning . 这是固定的定位 The navigation is positioned relative to the browser window. 导航相对于浏览器窗口定位。

jsfiddle 的jsfiddle

HTML: HTML:

<ul class="nav nav-pills">
  <li class="active"><a href="#">Home</a></li>
  <li><a href="#">Profile</a></li>
  <li><a href="#">Messages</a></li>
</ul>
<div id="first"></div><div id="second"></div><div id="third"></div>

CSS: CSS:

ul.nav{
    position:fixed;
}
div#first{
    height:600px;
    width:100%;
    background:#59071D;
}
div#second{
    height:600px;
    width:100%;
    background:#735448;
}
div#third{
    height:600px;
    width:100%;
    background:#F2DDB6;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM