JavaScript imitation Taobao intelligent floating

Source: Internet
Author: User

We often see this special effect on Taobao, the list of goods is very long, and the product column name always stay at the top. If you scroll to the top of the scroll bar, it will automatically determine whether it is to the top, and then always set up to not be afraid of occlusion.

This special effect is through JavaScript and CSS implementation, in the actual development of a lot of uses, the following is a use of JavaScript I found a fake Taobao intelligent floating source code, compatibility is good, in IE, Firefox, chrome under the normal work.

In Z-blog Use this special effect code to note that if used in the sidebar, you need to note that the sidebar can not use JavaScript dynamic loading, must use static format, otherwise in JavaScript will be wrong to calculate the height of the page, scroll up and down when the dislocation phenomenon.

The following are the relevant code:

JavaScript Code

(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";}

};

}

})();

HTML Code

HTML Code

<div id= "box" >

<div id= "float" class= "Div1" >

Part of the code that moves with the scroll

</div>

</div>

CSS Code

#box {float:left;position:relative;width:295px;}

. div1{}

. div2{position:fixed;_position:absolute;top:3px;z-index:295;}

Original Address: Moonlight Blog

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.