JQuery determines that the div stops when it scrolls to a certain position with the scroll bar.

Source: Internet
Author: User

Implementation Code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var rollSet = $ ('# widget ');
Var offset = rollSet. offset ();
Var fwidth = $ ("# footer"). height ();
$ (Window). scroll (function (){
Var scrollTop = $ (window). scrollTop ();
Var scrollBtm = $ (document). height ()-$ (window). scrollTop ()-$ ("# widget"). height ();
If (offset. top <scrollTop ){
If (scrollBtm> fwidth ){
RollSet. removeClass ('absolute '). addClass ('fixed ')
} Else {
RollSet. removeClass ('fixed'). addClass ('absolute ')
}
} Else {
RollSet. removeClass ('fixed ')
}
})
</Script>

Method description:
The height of the page minus the scroll height and the height of the layer with ID as the widget is equal to the height from the bottom of the layer to the bottom;
When the distance from the bottom is less than or equal to the distance from the bottom of a specific position, remove the style fixed and add absolute positioning to the layer!
Add position: relative to the parent layer in CSS;

Related Article

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.