简体   繁体   English

向下滚动引导导航栏,向下滚动,同时更改文本颜色

[英]Fading bootstrap navbar on scrolldown, while changing text color

so I'm working on a Ruby on Rails app with a design given to me which calls for the navigation bar to be transparent at the top of the page, and then fade into solid white after scrolling down past a certian <section> on the page, at the same time, the navbar link text is white at the top, and will fade to grey at the same <section> . 因此,我正在开发一个Ruby on Rails应用程序,该应用程序具有给我的设计,该设计要求导航栏在页面顶部透明,然后在向下滚动经过certian <section>之后变淡为纯白色。页面上,同时,导航栏链接文本在顶部为白色,并在同一<section>处逐渐变为灰色。

I've looked into JavaScript that changes opacity on scrolldown, but I haven't had success getting it to work. 我已经研究过JavaScript,该JavaScript可以在向下滚动时改变不透明度,但是我还没有成功使其能够正常工作。 I'd imagine that the same function(s) to fade in the navbar to white, would also work with the navbar links fading to grey as well. 我以为可以在导航栏中淡化为白色的相同功能也可以与导航栏链接也逐渐淡化为灰色。

I've also looked into the .affix js [plugin on Bootstrap, but I don't know very advanced javascript to modify it to my needs. 我也研究了.affix js [Bootstrap上的插件,但是我不知道非常高级的javascript来修改它以满足我的需要。 In case it's helpful, the navbar's structure in my the app's view starts as: 如果有帮助,在应用程序视图中导航栏的结构开始为:

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div>
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">
        <%= image_tag "logo-small.png" %>
      </a>

... "render partial" lines depending on if the user is logged in or not ...

Any and all help is appreciated! 任何和所有帮助表示赞赏! I'd really love to get this design working, and I've done most of it already, this particular part has me stumped. 我真的很想让这个设计起作用,并且我已经完成了大部分设计,这部分让我感到困惑。 Any help is appreciated! 任何帮助表示赞赏!

Something like this might help: 这样的事情可能会有所帮助:

window.addEventListener("scroll", function() {
    if (window.scrollY > 500) {
        $('.navbar').fadeOut();
    }
    else {
        $('.navbar').fadeIn();
    }
},false);

Change 500 with however many pixels from the top the place is that you want to do the fadeOut at. 将500更改为从顶部到顶部有多少像素,这是您要执行fadeOut的位置。

Simpler solution is to create a CSS class which you then add/remove on the scroll: 更简单的解决方案是创建一个CSS类,然后在滚动条上添加/删除它:

.navbar-fixed-top { background-color: rgba(255,255,255,1);transition: background-color 2s ease 0s;}
.navbar-fixed-top.opaque { background-color: rgba(255,255,255,0);transition: background-color 2s ease 0s;

} }

Javascript: Javascript:

$(window).scroll(function() {
    if($(this).scrollTop() > 300) {
        $('.navbar-fixed-top').addClass('opaque');
    } else {
        $('.navbar-fixed-top').removeClass('opaque');
    }
});

Our website has a similar effect: www.kmo.com.au 我们的网站具有类似的效果:www.kmo.com.au

This is great. 这很棒。 For newbies, for the opaque class, I did the following: 对于新手,对于不透明的类,我做了以下工作:

 .navbar-default { min-height: 120px; opacity: 0; transition: opacity .5s; } .opaque { opacity: 1; transition: opacity .5s; } 

Change the fade time under the transition attribute. 在过渡属性下更改淡入淡出时间。 This doesn't work for all browsers but it looks great for now. 并非所有浏览器都支持此功能,但现在看起来不错。

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

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