Javascript returns the top result (self-written code)

Source: Internet
Author: User

Nowadays, many websites use the result of returning to the top. If you are lazy, you can link a directly to # To achieve the effect. Today, I took the time to write it using native javascript. Due to my limited level, please note if you have any questions.
Html code:
Copy codeThe Code is as follows:
<! Doctype html>
<Html lang = "en-US">
<Head>
<Meta charset = "UTF-8">
<Title> js-back to the top </title>
<Style type = "text/css">
* {Margin: 0; padding: 0 ;}
. Wrapper {width: 960px; margin-left: auto; margin-right: auto ;}
P {margin: 10px; line-height: 30px ;}
# Top {position: fixed; right: 130px; bottom: 50px; background: gray; padding: 5px; display: block ;}
</Style>
</Head>
<Body>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<P> notepad is a basic text editor used to create simple documents. Notepad is most commonly used to view or edit text (.txt) files. However, many users find that notepad is a simple tool for creating web pages. </P>
<P> because notepad only supports basic formats, you cannot occasionally save special formats in documents that require plain text. Because special characters or other formats cannot be displayed on the published webpage, otherwise errors may occur, it is particularly useful when creating HTML documents for the webpage. </P>
<P> you can save notepad files in Unicode, ANSI, UTF-8, or high-front Unicode format. These formats provide you with more flexibility when using documents of different character sets. </P>
<A href = "javascript:;" id = "top"> back to the top </a>
</Body>
</Html>
<Script type = "text/javascript" src = "js/toTop. js"> </script>
<Script type = "text/javascript">
Window. onload = function (){
ToTop ('top', false );
}
</Script>

ToTop. js Code:
Copy codeThe Code is as follows:
// The first parameter is the button id. The second parameter is a Boolean value. true indicates that the button is always displayed. false indicates that when the scroll distance is not 0, the button is displayed.
Function toTop (id, show ){
Var oTop = document. getElementById (id );
Var bShow = show;
If (! BShow ){
OTop. style. display = 'none ';
SetTimeout (btnShow, 50 );
}
OTop. onclick = scrollToTop;
Function scrollToTop (){
Var scrollTop = document.doc umentElement. scrollTop | document. body. scrollTop;
Var iSpeed = Math. floor (-scrollTop/2 );
If (scrollTop <= 0 ){
If (! BShow ){
OTop. style. display = 'none ';
}
Return;
}
Document.doc umentElement. scrollTop = document. body. scrollTop = scrollTop + iSpeed;
SetTimeout (arguments. callee, 50 );
}
Function btnShow (){
Var scrollTop = document.doc umentElement. scrollTop | document. body. scrollTop;
If (scrollTop <= 0 ){
OTop. style. display = 'none ';
} Else {
OTop. style. display = 'block ';
}
SetTimeout (arguments. callee, 50 );
}
}

Note::
In ie6, position: fixed is invalid. For compatibility, click various bugs in the browser. In addition, I have written a css article to solve the failure of position: fixed under ie6. This effect is not supported.
PS: I am bored. I learned some javascript knowledge to write some effects at will.

Method 2:
Many Web pages place a "Back to Top" button at the bottom of the page, especially those without navigation at the bottom of the page, which can help visitors find the navigation again or review the advertisement (think really beautiful ). As JavaScript has become more and more widely used in recent years, and the sliding effect is everywhere, I followed suit and made the function of returning to the top into a sliding effect. Later, in order to better fit the physical characteristics, the system made the sliding effect of deceleration.

Let's talk about the principle first. We will get the distance from the scroll bar to the top of the page, move it up to a certain distance, and then get the distance from the scroll bar to the top of the page, move up a certain distance (a little smaller than the previous one); and so on...
Run the code first:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
/**
* Return to the top of the page
* @ Param acceleration
* @ Param time interval (MS)
**/
Function goTop (acceleration, time ){
Acceleration = acceleration | 0.1;
Time = time | 16;
Var x1 = 0;
Var y1 = 0;
Var x2 = 0;
Var y2 = 0;
Var x3 = 0;
Var y3 = 0;
If (document.doc umentElement ){
X1 = document.doc umentElement. scrollLeft | 0;
Y1 = document.doc umentElement. scrollTop | 0;
}
If (document. body ){
X2 = document. body. scrollLeft | 0;
Y2 = document. body. scrollTop | 0;
}
Var x3 = window. scrollX | 0;
Var y3 = window. scrollY | 0;
// Horizontal distance from the scroll bar to the top of the page
Var x = Math. max (x1, Math. max (x2, x3 ));
// Vertical distance from the scroll bar to the top of the page
Var y = Math. max (y1, Math. max (y2, y3 ));
// Rolling distance = current distance/speed, because the smaller the distance, the speed is greater than 1, so the rolling distance will be smaller and smaller
Var speed = 1 + acceleration;
Window. scrollTo (Math. floor (x/speed), Math. floor (y/speed ));
// If the distance is not zero, continue to call the iteration function.
If (x> 0 | y> 0 ){
Var invokeFunction = "goTop (" + acceleration + "," + time + ")";
Window. setTimeout (invokeFunction, time );
}
}
</Script>

Document.doc umentElement. scrollTop, document. body. scrollTop, window. scrollY are the same, but they only play a role in some browsers. You can debug which browser is used by yourself.
How to use it?
Copy codeThe Code is as follows:
<A href = "#" onclick = "goTop (); return false;"> TOP </a>

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.