返回顶部和回到底部

本文介绍了一种利用JQuery的scrollTop()方法实现页面元素滚动到顶部或底部的简便方法。通过具体示例代码,展示了如何在按钮点击事件中调用此方法,使指定的div元素返回到其顶部或底部位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

示例:


<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我只会写Bug啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值