To achieve this effect, you first need to understand the following basics:
Form Scrolling Event: $ (window). Scroll (function () {...});
Get form scrolling Distance: $ (window). scrolltop ();
Get form Height: $ (window). Height ();
Understanding the above can achieve the effect of returning the top through the hyperlink control.
1. Prepare the interface structure code:
Copy Code code as follows:
<form id= "Form1" runat= "Server" >
<div>
<asp:hyperlink id= "Top" runat= "Server" ></asp:HyperLink>
<H1 style= "Text-align:center" >
Use jquery implementation to return top effect
<div style= "width:800px; border:1px; Text-align:left; margin-left:230px; " >
。。。。。。 (lots of content that can be scrolled)
</div>
<asp:hyperlink id= "Backtotoplink" runat= "Server" cssclass= "BackToTop" > Back to Top </asp:HyperLink>
</div>
</form>
2. Add style to top control:
Copy Code code 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; Note to be set to absolute position
right:100px;
}
</style>
3. Add the implementation of the top effect script code:
Copy Code code as follows:
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#backToTopLink"). attr ("title", "Back to Top");
$ ("#backToTopLink"). attr ("href", "#Top"); Through links to achieve a sticky
$ (window). Scroll (function () {
if ($ (window). scrolltop () <= 100) {
$ ("#backToTopLink"). fadeout (200);
}
else {
$ ("#backToTopLink"). FadeIn (400);
}
var v_top = $ (window). Height ()-$ (". BackToTop"). Height ()-Ten + $ (window). scrolltop (); Dynamically calculate top position of top button after scrolling
$ (". BackToTop"). CSS ("top", V_top + "px");
});
});
</script>
Note that this code is only intended to demonstrate the hyperlink control to achieve the effect of returning to the top. can also be achieved through jquery animation effect, smooth top.
Smooth transitions return the top code as follows:
$ (' #backToTopLink '). Click (function () {$ (' html,body '). Animate ({scrolltop: ' 0px '}, 800);}); Replace $ ("#backToTopLink"). attr ("href", "#Top");