How jQuery achieves intelligent floating location on the Page scrolling Layer

Source: Internet
Author: User

Dear friends, you may have encountered positioning problems, especially those on your blog or Weibo. As a result, our product staff will have that effect during planning ,,, however, we need to implement, implement, and so on ..... No way. Who makes us a city attacker:

As shown in the following figure, when the scroll bar is pulled down, the Black Block TOP is 0, and the display is fixed:

The Code is as follows:Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Title> jQuery implements intelligent floating location at the Page scrolling time layer </title>
<Meta name = "description" content = ""/>
<Script type = "text/javascript" src = "http://jt.875.cn/js/jquery.js"> </script>
<Style type = "text/css">
* {Margin: 0; padding: 0 ;}
. Top {height: 100px; background: # ccc; text-align: center; line-height: 100px; font-size: 40px ;}
Body {font: 12px/1.8 Arial; color: #666; height: 2000px ;}
. Float {width: 200px; height: 200px; border: 1px solid # ffecb0; background-color: #000; position: absolute; right: 10px; top: 150px ;}
</Style>
</Head>
<Body>
<Div class = "top"> navigation, navigation, and navigation </div>
<Div class = "float" id = "float"> </div>
<Script type = "text/javascript">
$. Fn. smartFloat = function (){
Var position = function (element ){
Var top = element. position (). top, pos = element.css ("position ");
$ (Window). scroll (function (){
Var scrolls = $ (this). scrollTop ();
If (scrolls> top ){
If (window. XMLHttpRequest ){
Element.css ({
Position: "fixed ",
Top: 0
});
} Else {
Element.css ({
Top: scrolls
});
}
} Else {
Element.css ({
Position: pos,
Top: top
});
}
});
};
Return $ (this). each (function (){
Position ($ (this ));
});
};
// Bind
$ ("# Float"). smartFloat ();
</Script>
</Div>
</Body>
</Html>

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.