jquery实现点击图片返回顶部

本文介绍了一种使用jQuery实现页面滚动到一定位置时显示返回顶部按钮的方法,并通过点击该按钮使页面平滑滚动至顶部的具体实现。

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

 jquery实现窗口滚动条下拉,显示直达顶部的图片,点击图片返回顶部.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>返回顶部</title>
	<style type="text/css">
		body {
			height: 2000px;
		}

		img {
			display: none;
		}

		.box {
			position: relative;
			height: 2000px;
		}

		div .wrap img {
			position: fixed;
			bottom: 10px;
			right: 10px;
		}
	</style>
	<script text="text/javascript" src="js/jquery-3.3.1.js"></script>
</head>
<body>
	<div class="box">
		<div class="wrap"><img src="img/gotop.png"></div>
	</div>
	<script>
		$(function(){
			// 往下拉,出现广告框	
			$(window).scroll(function(){
				var height = $(window).height();
				// alert($(window).height());
				if ($(window).scrollTop() >= height * 0.5) {
					$("img").stop().fadeIn(1000);
				} else {
					$("img").stop().fadeOut(1000);
				}
			});

			// 点击图片,图片滚动条立即回到顶端
			$("img").click(function () {
				$("html,body").stop().animate({
					scrollTop: 0
				},1500)
			});
		});
	</script>
</body>
</html>

执行效果:

往下拖动鼠标,到达一定的位置,显示图片

往上托定滚动条到一定位置,图片消失

点击图片,回到顶部

转载于:https://my.oschina.net/korabear/blog/1811751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值