js 返回顶部toTop

直接上代码

html:

<!-- 返回顶部 -->
<meta name="toTop" content="true">

js:

if ($("meta[name=toTop]").attr("content") == "true") {
	  	$("<div id='toTop'><img src='images/toTop.png'></div>").appendTo('body');
	  	$('#toTop').css({
	  		width : '46px',
	  		height : '46px',
	  		bottom : '10px',
	  		right : '15px',
	  		position : 'fixed',
	  		cursor : 'pointer',
	  		zIndex : '666'
	  	});
	  	if ($(this).scrollTop() == 0) {
	  		$('#toTop').hide();
	  	}
	  	$(window).scroll(function(event) {
	  		if ($(this).scrollTop() == 0) {
	  			$('#toTop').hide();
	  		}
	  		if ($(this).scrollTop() != 0) {
	  			$('#toTop').show();
	  		}
	  	});
	  	$('#toTop').click(function(event) {
	  		$("html,body").animate({
	  			scrollTop : "0px"
	  		},666);
	  	});
	}

  

 

posted @ 2017-11-20 21:13  W_xiaobai  阅读(578)  评论(0编辑  收藏  举报