Sticky NavBar onScroll?
我现在正在为自己建立一个网站,我发现这对导航栏位于图像下方的多个网站上的效果非常好,但是当您滚动浏览它时,它会粘在顶部。
例
我怎样才能做到这一点? 另外,如何使用类似的导航栏样式获得与此网站类似的效果?
这是@Matthew在谈论的内容:
检查这个小提琴http://jsfiddle.net/luckmattos/yp8HK/1/
JQUERY
var num = 200; //number of pixels before modifying styles $(window).bind('scroll', function () { if ($(window).scrollTop() > num) { $('.menu').addClass('fixed'); } else { $('.menu').removeClass('fixed'); } });
希望它有所帮助,我使用了bootstrap navbar。
以下是一些例子
在引导程序中,您可以使用navbar-fixed-top
类。
看看以下链接:
固定到顶部: http : //getbootstrap.com/components/#navbar-fixed-top
修复了Navbar示例: http : //getbootstrap.com/examples/navbar-fixed-top/
这是一个Bootstrap示例,一旦用户滚过顶部标题,就会将导航栏附加到顶部。
这是另一个使用CSS transition
动画附加导航栏的示例
这是一个真实的快速示例,我与jquery和一些非常简单的CSS一起扔。
JS
$(window).on('scroll', function(){ if($(window).scrollTop()>=95 && !$('nav').hasClass('fixed')){ $('nav').addClass('fixed'); } else if($(window).scrollTop()<95 && $('nav').hasClass('fixed')){ $('nav').removeClass('fixed') } });
CSS
.container { height: 1800px; } .header { text-align: center; } nav { background-color: #efefef; border-top: 2px solid #666; border-bottom: 2px solid #666; padding: 15px; width: 100%; } nav ul { margin: 0; padding: 0; list-style: none; } nav li { display: inline-block; } .fixed { position: fixed; top: 0; left: 0; }
HTML
以下是粘性导航栏On-Scroll的示例,其中包含动态高度获取
JS
您需要先添加jQuery Library文件。
这里是小提琴链接 – https://jsfiddle.net/CV_pawan/4nr4codq/1/
$(document).ready(function(){ var num = $(".header-class").offset().top; $(window).bind('scroll', function() { if ($(window).scrollTop() > num) { $('.header-class').addClass('fixed'); } else { $('.header-class').removeClass('fixed'); } }); });
CSS
body { margin: 0px; padding: 0px; } .nav-bar-white { background-color: #125456; width: 100%; } .active-2 { float: right; width: 49%; text-align: right; } .fixed { position: fixed; width: 100%; top: 0; } h3 { color: #fff; } .first { background: #000; width: 49%; display: inline-block; margin: 0px; padding: 0px; border: 1px solid #fff; } .first p { color: #fff; } .second { background: #000; width: 49%; display: inline-block; margin: 0px; padding: 0px; border: 1px solid #fff; } .second p { color: #fff; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #300E06; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #000; }
HTML
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum