Side bar Following the simple implementation of scrolling code _javascript tips

Source: Internet
Author: User
Tags jquery library
Browsing the site often see the site, the sidebar of some content scrolling to the top of the page after the fixed in that position, no longer follow the scroll bar and scrolling. This effect is called "Side bar follow scrolling". It is useful for content that does not want to be scrolled out of the page.
Side column following the implementation of the rolling method there are many, more common there are two, these two methods in a neoease written article is very clear, one is the use of jquery to implement, this method for those who do not need to load the jquery library site, Obviously a burden (jquery is now getting bigger ...). )。 Another approach is the effect of native JavaScript, which is much lighter than the previous one. However, I am still not satisfied, native JavaScript write a file of 4K more, for me this to the first person, still too complicated. Is there a simpler way to achieve this?

The answer is certainly yes. The following is a specific description.
First of all, the HTML file (but also dynamic file, there is always HTML code)
Copy Code code as follows:

<div id= "box" >
<div id= "float" class= "Div1" >
Add here to follow the scrolling content
</div>
</div>

And then the CSS code
Copy Code code as follows:

#box {float:left; position:relative;width:250px;}
. div1{width:250px;}
. div2{position:fixed;_position:absolute;top:0;z-index:250;}

Finally, JS code (can be placed in the need to scroll the page, you can also put in a separate JS file and then call)
Copy Code code as follows:

(function () {
var Odiv=document.getelementbyid ("float");
var h=0,ie6;
var Y=odiv;
while (Y) {h+=y.offsettop; Y=y.offsetparent};
Ie6=window. Activexobject&&!window. XMLHttpRequest;
if (!IE6) {
Window.onscroll=function ()
{
var s=document.body.scrolltop| | Document.documentElement.scrollTop;
if (s>h) {odiv.classname= "Div1 div2"; if (iE6) {odiv.style.top= (s-h) + "px";}}
Else{odiv.classname= "Div1";}
};
}
})();

OK, it's easy enough.
Finally, give an example.

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.