JQuery creates smooth Page scrolling (top or bottom)

Source: Internet
Author: User

In this article, I will show you how to create a smooth scrolling effect using jQuery. You can scroll to the top or bottom of your webpage.

How it works
First, load the jquery library before the Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "http://demo.jb51.net/jslib/jquery/jquery1.3.2.js"> </script> jQuery scroll to the bottom:

Link:
<A href = "#" class = "scrollToBottom"> Scroll to bottom </a> jQuery
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
// Scroll page to the bottom
$ ('A. scrollToBottom '). click (function (){
$ ('Html, body,. content'). animate ({scrollTop: $ (document). height ()}, 300 );
Return false;
});
})
</Script>

How it works:
The first line of code is executed before the page is loaded.
$ (Document). ready (function () {execute the action in {} when the connected. scrollToBottom class is clicked

$ (Document). ready (function (){
$ ('A. scrollToBottom '). click (function (){
});
}) In this function, execute this code
$ ('Html, body'). animate ({scrollTop: $ (document). height ()}, 'low ');
Return false; when the Code clicked by a link runs inside the function, scrollTop uses smooth scrolling. The above code will scroll to the bottom of the page and use the window height to determine the bottom height. Using Speed Control for "slow", "medium", and "fast", I used 'slow.
JQuery scroll to the top
First, insert a link to the footer of Your webpage. When the clicked jQuery code is executed. Animation functions. It is very important because it references the links of classes in jQuery.
Link:
<A href = "#" class = "scrollToTop"> Scroll to bottom </a> jQuery
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ('A. scrollToTop '). click (function (){
$ ('Html, body'). animate ({scrollTop: 0}, 'low ');
Return false;
});
})
</Script>

How it works:
When the class is loaded on the page. When the scrollToTop link is clicked, jQuery will execute this
$ ('Html, body'). animate ({scrollTop: 0}, 'low ');
Return false ;. the animate () method enables us to create the animation effect of CSS attributes of any number. The scrollTop function is set to 0, which indicates the position of the scroll bar at the top, "slow" refers to the speed at which the animation will run. When you notice this line:
<Code> return false; </code> This prevents events that are triggered by default actions. In our example, it prevents User links.
You can also:
Copy codeThe Code is as follows:
Event. preventDefault ();

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.