左右随屏动画实现
问题:页面中的左右按钮会随着页面滑动而出现和消失。
解答:
采用 IntersectionObserver API 来检测页面上某个元素是否出现在屏幕中,从而控制左右按钮的显示和隐藏。
代码范例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>左右随屏动画</title> <style> body { margin: 0; padding: 0; } .container { display: flex; } .left-panel { background: #ccc; width: 200px; height: 100vh; position: sticky; left: 0; top: 0; } .right-panel { flex: 1; background: #eee; height: 100vh; overflow: scroll; } .end-marker { position: absolute; bottom: 0; width: 1px; height: 1px; } </style> </head> <body> <div class="container"> <div class="left-panel"> <h1>左侧面板</h1> </div> <div class="right-panel"> <h1>右侧面板</h1> <div style="height: 2000px;"></div> <div class="end-marker"></div> </div> </div> <script> const leftPanel = document.querySelector('.left-panel'); const endMarker = document.querySelector('.end-marker'); const observer = new IntersectionObserver((entries, observer) => { if (entries[0].isIntersecting) { leftPanel.style.display = 'none'; } else { leftPanel.style.display = ''; } }); observer.observe(endMarker); </script> </body> </html>
工作原理:
注意:
以上就是如何实现页面滚动时左右按钮的显示和隐藏?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号