html代码
<div class="back-to-top" style="left: 1200px; position: fixed; bottom: 0px; ">
<a href="#" style="top: 0px;">↑回顶部</a>
</div>
<script>
$(function(){
$(window).scroll(function(){ //只要窗口滚动,就触发下面代码
var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //获取滚动后的高度
if( scrollt >200 ){ //判断滚动后高度超过200px,就显示
$(".back-to-top").fadeIn(400); //淡入
}else{
$(".back-to-top").stop().fadeOut(400); //如果返回或者没有超过,就淡出.必须加上stop()停止之前动画,否则会出现闪动
}
});
$(".back-to-top").click(function(){ //当点击标签的时候,使用animate在200毫秒的时间内,滚到顶部
$("html,body").animate({scrollTop:"0px"},200);
});
});
</script>
css代码
.back-to-top a {
position: absolute;
left: 0;
top: 40px;
}
.back-to-top {
display: none;
position: fixed;
width: 100px;
height: 40px;
overflow: hidden;
}
记得引入jquery.min.js,最终效果如下,可百度搜索【购物吧https://www.gowu8.net/】查看