js实现悬浮按钮并回到页面顶部

判断document.body.scrollTop 和 document.documentElement.scrollTop

其中一个大于零时,显示按钮,利用JavaScript的scrollTo将页面滚动到顶,并隐藏按钮。

 

 

 

 

代码(比其他版本,修正了对element.scrolltop的判断)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>漂浮回顶部按钮</title>
<style>
body{
height:2000px;
}
.rTop{
width:100px; height:25px;
text-align:center; font-size:small;
line-height:25px; border:1px solid #999;
position:fixed; right:0; bottom:0;
border-bottom-color:#333; 
border-right-color:#333; margin:5px;
cursor:pointer; display:none
}
</style>
<script>
<!--拖动滚动条或滚动鼠标轮-->
window.onscroll=function(){
    if(document.body.scrollTop||document.documentElement.scrollTop){
        document.getElementById('rTop').style.display="block"
    }else{
    document.getElementById('rTop').style.display="none"
    button1style.cursor="hand"
}
}
<!--点击“返回顶部”按钮-->
function toTop(){
    window.scrollTo('0','0');
    button1style = document.getElementById('rTop').style;
    button1style.display="none"
    //button1style.cursor="hand"
    document.getElementById('rTop').style.display="none"
}
function moreTest(){
    for(let i =0; i< 50; i++){
       document.write("sss"+i+"<br>");
    }
}
</script>
</head>
<body>
    <div class="rTop" id="rTop" onClick="toTop()">返回顶部</div>
    <h1>顶部</h1>

    <h3><script>moreTest()</script></h3>

    <h1>底部</h1>
</body>
</html>

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值