简体   繁体   English

向下滚动时如何将主导航固定在顶部

[英]How to fix main navigation on top when scrolling down

First, Merry Christmas to all 首先,祝大家圣诞快乐

I want to fix my top navigation menu bar on top when scrolling down like : csschopper.com 向下滚动时,我想将顶部导航菜单栏固定在顶部: csschopper.com

this happen by position: fixed; 这是按position: fixed;发生的position: fixed; css property but when i put it in css menu will be fixed at top. css属性,但是当我将其放在css菜单中时将固定在顶部。 this will require some javascript code but I don't know what exactly the code is. 这将需要一些javascript代码,但我不知道代码到底是什么。 also when someone scroll down i also want to display small logo beside the menu like csschopper.com 当有人向下滚动时,我也想在菜单旁边显示小徽标,例如csschopper.com

Thanks in advance. 提前致谢。

This is what the code would look like: jsFiddle In this example I created two navigation bars but you can also have one and then add an item 代码如下所示: jsFiddle在此示例中,我创建了两个导航栏,但您也可以拥有一个导航栏,然后添加一个项目

This is jQuery code, so make sure to load the jQuery libery: 这是jQuery代码,因此请确保加载jQuery libery:

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>

Initial position of topbar is static. 顶栏的初始位置是静态的。 But when user scroll (you have to catch onscroll event) like this on jquery : 但是,当用户在jquery上滚动(您必须捕获onscroll事件)时,如下所示:

$(window).scroll(function());

with a scroll position bigger than header height, the Javascript modifiy the topbar position to fixed with this for example : 当滚动位置大于标题高度时,Javascript修改顶栏位置以固定此位置,例如:

documentById().style.display

The same thing for the logo on left. 左侧徽标也是如此。

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

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