Some projects need something to integrate (2)--floating navigation

Source: Internet
Author: User

This thing actually understood very understood, is the application of position:fixed

Examples are as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<style type= "Text/css" >
. Wrapper{width:1000px;height:2000px;margin-left:auto;margin-right:auto;}
. header{height:150px;}
#nav {padding:10px;position:relative;top:0;background:black;width:1000px;}
a{display:inline-block;margin:0 10px;*display:inline;zoom:1;color:white;}
</style>
<body>
<div class= "wrapper" >
<div class= "Header" ></div>
<div id= "NAV" >
<a href= "#" >11111</a>
<a href= "#" >22222</a>
<a href= "#" >33333</a>
<a href= "#" >44444</a>
<a href= "#" >55555</a>
</div>
</div>
</body>
<script type= "Text/javascript" src= "Menufixed.js" ></script>
<script type= "Text/javascript" >
Window.onload = function () {
Menufixed (' Nav ');
}
</script>

/*js*/

JavaScript Document
function menufixed (ID) {
var obj = document.getElementById (ID);
var _getheight = obj.offsettop;

Window.onscroll = function () {
Changepos (Id,_getheight);
}
}
function Changepos (id,height) {
var obj = document.getElementById (ID);
var scrolltop = Document.documentElement.scrollTop | | Document.body.scrollTop;
if (ScrollTop < height) {
obj.style.position = ' relative ';
}else{
obj.style.position = ' fixed ';
}
}

Floating navigation is actually divided into several styles, either above, below, in the side of those ads I don't say--!

The only thing to consider is when your navigation is not at the top, how much to scroll the window to navigate (scrolltop This is the window scrolling value),

To add a title bar, you need to consider the window scrolling distance and the height (height) of the ad column; scrolltop < height This is the window scrolling distance is not higher than the title bar,

I don't need to say you know it, when scrolltop > height give a fixed positioning on it, is usually fixed in top:0 that, how the capital to see your needs.

What's wrong please point out, thank you, sharing is a virtue!

In fact, I do is mobile, do not know is not the speed of the problem or how, I slide when the navigation switch is very slow, that effect is not good to see, I looked for a long time did not find any reason, there is the great God know to share experience, thank you!

Some projects need something to integrate (2)--floating navigation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.