扫码关注官方订阅号
弹出一个层,上面的内容较多,想用鼠标滑轮上下滑动的时候,层上下移动,但是页面不动。请教一下。这个效果用原生JS要怎么写呀?谢谢大神们!
走同样的路,发现不同的人生
监听鼠标滚轮事件,触发 dom 元素的移动就可以了吧
弹出层的时候 给 body 增加 样式 overflow:hidden 这个时候整个页面就不会滚动了同时使用 @北去 提到的 给层增加 overflow-y:auto 这个时候层就可以滚动了
body
overflow:hidden
overflow-y:auto
弹出的层固定定位,设置宽高,然后overflow-y:auto 试试。
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>Title</title> <style> #myPopUP{ width:300px; height:300px; border:1px solid #cccccc; background-color: #ffffff; color:#000000; box-sizing: border-box; display: none; } #myPopUP .content{ width:100%; height:250px; box-sizing: border-box; overflow:auto; } #myPopUP .actionBar{ width:100%; height:50px; background-color: #cccccc; color:#000000; box-sizing: border-box; line-height: 50px;; } </style> </head> <input type="button" name="myButton" id="myButton" value="POPUP"> <p id="myPopUP"> <p class="content"> <p style="background-color: #00a23f;width:100%;height:1000px;overflow: hidden">内容很多朵儿弧度第三方回送开放很舒服是SDK是</p> </p> <p class="actionBar"> <p style="display: inline-block;float:right;margin-right:5px;"><input type="button" name="myCloseButton" id="myCloseButton" value="Close" ></p> </p> </p> <p style="background-color: #1e83c9;widht:500px;height: 1000px;"> </p> <script> var buttonDom=document.getElementById("myButton"); var myPopUPDom=document.getElementById("myPopUP"); var contentDom; var popupWidth=300; var popupHeight=300; buttonDom.addEventListener("click",function(){ var fixedX=(document.documentElement.clientWidth-300)/2; var fixedY=(document.documentElement.clientHeight-300)/2; myPopUPDom.style.position="fixed"; myPopUPDom.style.left=fixedX+"px"; myPopUPDom.style.top=fixedY+"px"; myPopUPDom.style.position="10px"; myPopUPDom.style.display="block"; document.body.style.overflow="hidden"; myPopUPDom.setAttribute("popup","popup"); contentDom= myPopUPDom.getElementsByClassName("content")[0]; function closeHandler(event){ myPopUPDom.style.display="none"; document.getElementById("myCloseButton").removeEventListener("click",closeHandler,false); document.body.style.overflow="auto"; contentDom=null; } document.getElementById("myCloseButton").addEventListener("click",closeHandler,false); },false); window.addEventListener("wheel",function(event){ contentDom&&(contentDom.scrollTop=contentDom.scrollTop+event.deltaY) },false); </script> </body> </html>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
监听鼠标滚轮事件,触发 dom 元素的移动就可以了吧
弹出层的时候 给
body增加 样式overflow:hidden这个时候整个页面就不会滚动了同时使用 @北去 提到的 给层增加
overflow-y:auto这个时候层就可以滚动了弹出的层固定定位,设置宽高,然后overflow-y:auto 试试。