返回顶部和回到底部

2023-12-25 14:51:46 浏览数 (2)

经常会有返回顶部、回到底部的需求,推荐一种比较简单的写法 JQ中有 scrollTop() 方法可以帮助我们快速实现该需求

示例:

代码语言:javascript复制
<html>
<body >
<div class="div1" style="width:200px;height:200px;overflow:auto">This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</div>
<button  onclick="test()">测试返回</butoon>
</body>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
	function test(){
		 $(".div1").scrollTop(0);//此为滚动条的位置--返回顶部
		 $(".div1").scrollTop($(document).height());//此为滚动条的位置---返回底部 直接取dom的高度
	}
</script>
</html>

0 人点赞