Home > Web Front-end > JS Tutorial > JQuery animated page scroll Back to top Animation effects (compatible with Chrome)_jquery

JQuery animated page scroll Back to top Animation effects (compatible with Chrome)_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:34:30
Original
1303 people have browsed it

First, add a Class to these 'return to top' links:
Return to top↑
Then add the following jQuery code. You can place this line of code before , or in other locations. Of course you also need to include the jQuery library file in the . (

Copy code The code is as follows:

jQuery(document).ready(function() {
jQuery('.backtotop').click(function(){
jQuery('html').animate({scrollTop:0}, 'slow');
}

Is it that simple? Basically! But using jQuery ('html') can't select the object we want in Safari and Chrome (I remember seeing it somewhere: chrome uses the core of safari). Under these two browsers, we can use jQuery('body') instead. Therefore, in order to make the code more compatible, we can add browser judgment. Here we use the jQuery.browser method provided by jQuery. . Note: In jQuery1.3, this method is no longer recommended. jQuery1.3 adds a new jQuery.support method to display the property collection of different browsers' respective features and bugs, which means that jQuery1.3 is no longer recommended. To judge the browser, it is recommended to judge a certain feature directly. But I don't know which feature this selector problem should belong to, so I still use the old method (jQuery.browser method in jQuery1.3. Still supported)
Copy code The code is as follows:

jQuery(document).ready(function () {
jQuery('.backtotop').click(function(){
if(jQuery.browser.safari) {//Here to determine whether the browser is safari
jQuery('body') .animate({scrollTop:0}, 'slow');
return false;//Returning false can avoid adding # after the original link
}
else{
jQuery('html') .animate({scrollTop:0}, 1500);
return false;
}
});
});

In the above code, I use Go to jQuery('body').animate({scrollTop:0}, 'slow'); We can change the scroll speed according to actual needs. You can use 'slow', 'fast', or use a specific number, such as 1000 (Represents one second, please note that you do not need to add single quotes when using specific numbers). In addition, {scrollTop:0} means returning to the top of the page. If you just want the page to scroll to a specific location you want, we can use the label (ID) method, for example: you want to move to an area with the ID 'myID' (
....
) At the top, we can use a similar method, but first calculate the distance between this area and the top of the page. The code is as follows:
Copy code The code is as follows:

jQuery('body').animate({scrollTop:jQuery('#myID ').offset().top}, 'slow');
//jQuery('#myID').offset().top can calculate the distance to the top of the page in the area with the ID myID
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template