悬浮div设置回到顶部按钮

样式:

<style type="text/css">  
.btns{font-weight: bold;display: inline-block;width: 80px;height: 27px;border: #5fb878;background: #5fb878;font-size: 1.4em;text-align: center;padding-top: 5px;color: white;}
a:hover{color:#FFFFFF}
</style>

主要代码:

<div style="overflow: hidden;position: fixed;right: 10px;bottom: 20px;z-index: 10;">
	    <div style="overflow: hidden;">
	    	<div style="padding-right:50px;">
	    		<a id="js_print" class="btns">打印</a>
			</div>
	    	<div style="padding-top:20px;padding-right:50px;">
				<a href="list.jhtml" class="btns">返回</a>
			</div>
	    	<div style="padding-top:20px;padding-right:50px;padding-bottom:50px">
				<a href="#" style="float: right;" class="btns">回到顶部</a>
			</div>
	    </div>
    </div>

基本上是拿来就可以用,确保你的页面足够长。

效果图:


  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现HTML悬浮按钮返回顶部的效果,你可以按照以下步骤进行操作。 首先,在HTML中添加两个div元素,一个用于显示顶部内容,另一个用于显示返回顶部按钮。为它们设置相应的class名和内容。 然后,在CSS中设置这两个元素的样式。对于顶部内容,可以设置宽度为100%,高度为80px,使用flex布局居中对齐文字,设置背景颜色和过渡效果,并将其定位为固定位置,初始时显示在页面顶部之外。 对于返回顶部按钮,可以设置宽度为50px,高度为50px,设置背景颜色和字体样式,并将其定位为固定位置,在页面底部右下角,默认隐藏。 接下来,在JavaScript中添加逻辑代码。使用滚动事件来判断浏览器卷去的高度是否大于等于一个临界点(比如300px),如果是,则设置顶部内容显示出来,返回顶部按钮显示出来;如果不是,则设置顶部内容隐藏,返回顶部按钮隐藏。 最后,在JavaScript中添加点击事件监听,当点击返回顶部按钮时,使用动画效果将页面滚动到顶部位置。 具体的代码实现可以参考以下示例代码。请注意,示例代码中的一些具体细节可能需要根据实际情况进行调整。 HTML代码: ``` <div class="header">顶部</div> <div class="goTop">回到顶部</div> ``` CSS代码: ``` .header { width: 100%; height: 80px; display: flex; justify-content: center; align-items: center; font-size: 30px; color: #fff; background-color: rgb(86, 226, 180); transition: top 0.5s linear; overflow: hidden; position: fixed; top: -80px; left: 0; } .goTop { width: 50px; height: 50px; background-color: pink; font-size: 20px; text-align: center; line-height: 25px; color: #fff; position: fixed; bottom: 50px; right: 50px; display: none; /* 默认隐藏 */ } ``` JavaScript代码: ``` document.addEventListener('DOMContentLoaded', function() { var header = document.querySelector('.header'); var goTop = document.querySelector('.goTop'); window.addEventListener('scroll', function() { var height = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; if (height >= 300) { header.style.top = '0px'; goTop.style.display = 'block'; } else { header.style.top = '-80px'; goTop.style.display = 'none'; } }); goTop.addEventListener('click', function() { window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); ``` 通过以上的HTML、CSS和JavaScript代码,你可以实现一个HTML悬浮按钮返回顶部的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个猴子请来的救兵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值