Jquery scroll down (fixed layer position)

Source: Internet
Author: User

We will now see that many networks will have a fixed layer effect, that is, when we are not out of date, it will be displayed normally, otherwise it will scroll down with the mouse.


I will introduce you to several instances below

The Code is as follows: Copy code

<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> Document </title>
<Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
<Script>

Window. onload = function (){
$ (Window). scroll (function (){
Var sTop = $ (window). scrollTop ();
If (sTop> 100 ){
$ ('# Pinning'). show ();
} Else {
$ ('# Pinning'). hide ();
};;
});
};

</Script>
</Head>
<Body>
<Div id = "pinning" style = "display: none; position: fixed; top: 5px; left: 5px; width: 500px; height: 30px; border: 3px solid # ccc; text-align: right; "> fixed layer </div>
<Div style = "width: 500px; border: 1px solid #000; height: 3000px; "> content <br> br> This is the content <br> </div>
</Body>
</Html>


Instance 2

The Code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$ (Window). scroll (function (){
Var scrollTop = $ (document). scrollTop ();
Var headheight = $ (". head"). height ();
If (scrollTop> = headheight ){
$ ("# Fudong"). addClass ("fixed ");
} Else {
$ ("# Fudong"). removeClass ("fixed ");
}
})
})
</Script>
<Style>
* {Margin: 0; padding: 0}
* Html, * html body {background: url (about: blank) fixed}
# Wrap {width: 950px; height: 2000px; margin: 0 auto; border: 1px solid #000; position: relative}
# Wrap. head {height: 200px; background: # AA5F55}
# Fudong {width: 950px; height: 30px; background: # ccc ;}
. Fixed {position: fixed; _ position: absolute; top: 0; top: expression(eval(document.doc umentElement. scrollTop + 0 ))}
</Style>
</Head>

<Body>
<Div id = "wrap">
<Div class = "head"> </div>
<Div id = "fudong"> 1111 </div>
</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.