To achieve this, you must first understand the following basic knowledge:
Form rolling event: $ (window). scroll (function (){...});
Get the scrolling distance of the form: $ (window). scrollTop ();
Get the form height: $ (window). height ();
After learning about the above content, you can achieve the result of returning to the top through the hyperlink control.
1. Prepare the interface structure code:
Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Div>
<Asp: HyperLink ID = "Top" runat = "server"> </asp: HyperLink>
<H1 style = "text-align: center">
Returns the top result using jQuery. <Div style = "width: 800px; border: 1px; text-align: left; margin-left: 230px;">
...... (A lot of content can be rolled)
</Div>
<Asp: HyperLink ID = "backtoplink" runat = "server" CssClass = "backToTop"> Back to Top </asp: HyperLink>
</Div>
</Form>
2. Add a style to the back-to-top control:Copy codeThe Code is as follows: <style type = "text/css">
. BackToTop
{
Background-color: Yellow;
Width: 30px;
Border-style: outset;
Border-width: 1px;
Text-align: center;
Font-weight: bold;
Font-family: Arial;
Font-size: x-large;
Cursor: pointer;
Position: absolute; // be sure to set it to an absolute position.
Right: 100px;
}
</Style>
3. Add the script code to implement the stick effect:Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Backtoplink"). attr ("title", "Back to Top ");
$ ("# Backtoplink"). attr ("href", "# Top"); // click the link to Top the page.
$ (Window). scroll (function (){
If ($ (window). scrollTop () <= 100 ){
$ ("# Backtoplink"). fadeOut (200 );
}
Else {
$ ("# Backtoplink"). fadeIn (400 );
}
Var v_Top = $ (window ). height ()-$ (". backToTop "). height ()-10 + $ (window ). scrollTop (); // dynamically calculates the top position of the push button after rolling
$ (". BackToTop" ).css ("top", v_Top + "px ");
});
});
</Script>
Note: This code is only used to demonstrate the effect of the hyperlink control to return to the top. You can also use jQuery's animation effects to achieve smooth top placement.
The Code returned to the top of the smooth transition is as follows:
$ ('# Backtoplink '). click (function () {$ ('html, body '). animate ({scrollTop: '0px '}, 800) ;}); // Replace $ ("# backToTopLink "). attr ("href", "# Top ");