通过js滑动到指定内容区

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.container {
			width: 100%;
			height: 1500px;
			background: gray;
			border: 1px solid black;
		}
	</style>
</head>

<body>
	<button id="but">滑动到指定内容区</button>
	<div class="container"></div>
	<p id="artical">指定内容毒贩夫妇反反复复凤飞飞发发发发发发发发发发发发发发发发</p>
	<script>
		const but = document.getElementById('but')
		but.onclick = () => {
			const dom = document.getElementById('artical')
			dom.scrollIntoView({ behavior: 'smooth' })
		}

	</script>
</body>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaScript来实现一个滑动指定位置悬浮按钮的效果。具体的实现步骤如下: 1. 首先需要在页面上添加一个悬浮按钮的HTML元素,可以使用<a>或<button>标签来创建一个按钮,并设置其样式和位置。 2. 使用JavaScript监听页面的滚动事件,当页面滚动指定的位置时,显示该按钮。 3. 当用户点击该按钮时,使用JavaScript将页面滚动指定位置。 下面是一个示例代码,可以让你更好地理解这个实现过程: ```html <!DOCTYPE html> <html> <head> <title>Scroll to Top Button</title> <style> #scroll-to-top { display: none; position: fixed; bottom: 20px; right: 20px; z-index: 99; font-size: 18px; border: none; outline: none; background-color: #333; color: #fff; cursor: pointer; padding: 15px; border-radius: 50%; } </style> </head> <body> <!-- Content here --> <button id="scroll-to-top">Top</button> <script> var scrollToTopBtn = document.querySelector("#scroll-to-top"); window.addEventListener("scroll", function() { if (window.pageYOffset > 100) { scrollToTopBtn.style.display = "block"; } else { scrollToTopBtn.style.display = "none"; } }); scrollToTopBtn.addEventListener("click", function() { window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); }); </script> </body> </html> ``` 在这个示例中,我们创建了一个“返回顶部”按钮,并将其添加到页面中。然后,我们使用JavaScript监听页面的滚动事件,并在页面滚动超过100像素时,显示该按钮。最后,当用户点击该按钮时,使用JavaScript将页面平滑地滚动到页面顶部。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值