web开发中经常有页面回顶的需求,近期使用,代码通俗易懂,而且还很实用!
html:
<a id="goTop" class="go-top" href="#header"> <span class="go-top-inner"></span></a>css:
.go-top { background: url("../img/xxx.png") no-repeat scroll left top transparent; bottom: 65px; color: #444444; display: none; height: 50px; margin-left: 510px; position: fixed; right: 160px; text-align: center; width: 50px;}.go-top:hover { background-position: -50px top;}js:(基于jquery)
<!-- 页面回顶 -->var gotop = $('#goTop');gotop.click(function() { $("html,body").animate({ scrollTop: 0 }, 200); return false;});$(window).scroll(function() { if (gotop.offset().top > window.innerHeight) { gotop.show(); } else { gotop.hide(); } });图片

本文介绍了一种在web开发中实现页面回顶功能的方法,包括HTML、CSS样式和基于jQuery的JS交互代码,使页面滚动到顶部时按钮隐藏,滚动到一定距离后显示。
2211

被折叠的 条评论
为什么被折叠?



