Jquery page back to the top of the effect of of icon fade write yourself

  • 2020-03-30 03:23:19
  • OfStack

Well, coming in to develop requirements, when the web content is more grass chicken, users need to have a button to quickly go back to the top, rather than rolling their own pulley ~

Originally thought to be more difficult to say, because the top requirements of all js to achieve, which page reference, which page display.

As a result, the silk tried to write, well, did not find it, it is easy to say ~~

Fart I soon put, directly on the code, fart put more will become shit ~ ~ oh, sin, amitabha Buddha, amen ~ ~
 
<pre name="code" class="javascript">//Back to top js
$(function(){ 
var $btn_top = $('<a id="scrollTopBtn"><img src="css/web/images/scrollTop.png"></a>'); 
$btn_top.css({ 
'display':'none', 
'width':'40px', 
'height':'40px', 
'position':'fixed', 
'right':'20px', 
'bottom':'100px', 
'z-index':'999' 
}); 
$("body").append($btn_top); 
$("body").on("click","#scrollTopBtn",function(){ 
$("html,body").animate({scrollTop: 0},"slow"); 
}); 
var $btn = $("#scrollTopBtn"); 
if($(window).scrollTop() > 100){ 
$btn.fadeIn(600); 
} 
$(window).scroll(function(){ 
if($(window).scrollTop() > 100){ 
$btn.fadeIn(600); 
}else{ 
$btn.fadeOut(600); 
} 
}); 
}); 

Yourself a picture is ok, here I use is the absolute path "CSS/web/images/scrollTop PNG"

Simply change the size in lines 5 and 6 to avoid adding cat tags to the page. ^ ^ omega <) Meow ~ ~ ~

Save js file, direct reference, which pain with which, who use who know ~~~

Related articles: