Jquery writes a drag navigation pin to the top instance

Source: Internet
Author: User

Fixed to the top is that no matter how you drag the page, the position of the scroll navigation menu is fixed. Next I will introduce you to a drag navigation fixed to the top instance, I hope to give you some friends.

The specific effect is to drag the scroll bar. Once the scrolling area of the screen exceeds the area where the element is located, the element is fixed at the top and kept at the top along with the screen scrolling.

Such a program can greatly increase the user experience on some websites to gain a good reputation.


Today, I added a piece of jquery code to the pioneers, so that the "random articles" on the right of the website can achieve similar results,

 

In order to avoid monotonous content on the right side when reading long articles, add similar scrolling fixed jquery code.

The code implementation method is as follows:

JS Code:

The Code is as follows: Copy code

$ (Function (){
Navbod = $ ('# advancedrandomposts-5'); // select the target Element
Var nav = navbod. position (); // obtain the position of the target element; left, top Value
NavWidth = navbod. width () // obtain the current width of the target element (because after position: fixed is used, you need to set the width to keep the original size of the element)
Navbod.css ('left', nav. left); // you can specify the X axis for an element.
$ (Document). scroll (function () {// listens to scroll events
NavTop = $ ('body'). scrollTop (); // get the scroll length of the current page
If (navTop <= nav. top-50) // compare the top value of the element with the page scroll length. If the page scroll length exceeds the top position of the element, set the fixed position for the element.
{
Navbod.css ('position', 'fixed ')
Navbod.css ('top', '50px ')
Navbod.css ('z-Index', '99 ')
Navbod.css ('background', '# efefef ')
Navbod.css ('width', navWidth)
} Else {
Navbod.css ('position', 'static ')
Navbod.css ('background', 'None ')
}
})
})


Call Method

The Code is as follows: Copy code
<Div id = advancedrandomposts-5> here is the content of the navigation menu, there is no css effect you can define, but js can use the above </div>

There are no restrictions on CSS and HTLM. You only need to load the above JS Code to apply it in most cases.

The following is a fixed QQ customer service on the right.

 

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 = gb2312"/>

<Title> jquery provides a menu for scrolling to a fixed position to suspend the effect-by bKjia. c0m </title>

<Script src = "http://code.jquery.com/jquery-1.5.2.min.js" language = "javascript"> </script>

<Style type = "text/css">

* {Margin: 0; padding: 0}

. Tag {width: 100px; height: 300px; background: red; float: right ;}

. Fixer {position: fixed; right: 0px; top: 0px; _ position: absolute; _ top: expression (documentElement. scrollTop + 10 + "px ")}

</Style>

</Head>

 

<Body>

<Div id = "content" style = "height: 2000px; margin-top: 500px;">

<Div class = "tag"> tag test <br> fixed when scrolling to 540px. Contact me. </div>

</Div>

 

<Script language = "javascript">

 

$ (Window). scroll (function (){

Var h_num = $ (window). scrollTop ();

If (h_num> 540 ){

$ ('. Tag'). addClass ('fixer ');

} Else {

$ ('. Tag'). removeClass ('fixer ');

}

});

 

</Script>

</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.