写了一个简单的监听滚动条事件

您所在的位置:网站首页 滑动事件监听 写了一个简单的监听滚动条事件

写了一个简单的监听滚动条事件

2023-02-12 09:15| 来源: 网络整理| 查看: 265

写了一个简单的监听滚动条事件

描述

鼠标滚动到一定距离,点击回到顶部按钮就会出现,初始状态是 隐藏的,下面开代码

// html 部分 我是首页面 回到顶部 // css 样式部分 body { background-color: antiquewhite; height: 3000px; } .arrow { /* 字体排列模式 */ writing-mode: vertical-lr; position: fixed; top: 50%; right: 5px; /* letter-spacing 字体垂直排列的时候 用这个属性 设置字体之间的间距 */ letter-spacing: 3px; color: blue; display: none; } .arrow:hover { /* 鼠标悬停变成小手 */ cursor: pointer; } // js 部分 window.onload = () => { const arrow = document.querySelector('.arrow') // 给window加监听事件 window.addEventListener('scroll', () => { // document.documentElement.scrollTop //获取滚动条的位置 let scroll = document.documentElement.scrollTop console.log(document.documentElement.scrollTop) if (scroll > 500) { // scroll>500 显示回到顶部按钮 arrow.style.display = "block" } else { // scroll


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3