CSS-如何根据页面中的位置隐藏元素?
我在移动设备上设置了一个粘性元素。这是代码:
.sticky-btn {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
运行良好,但我不希望按钮显示用户何时在页面的顶部。只有在您向下滚动后出现20-30像素后才出现,这将是完美的。是否可以在CSS中以某种方式完成此操作?
谢谢!
I have a sticky element I've setup for a button on mobile devices. This is the code:
.sticky-btn {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
Works great but I don't want the button to show when the user is at the very top of the page. It would be perfect if it only appeared after you scrolled down say 20-30 pixels. Is it possible to accomplish this in CSS somehow?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
布莱恩·普雷斯顿(Brian Preston)。不幸的是,我们不能仅使用CSS来做到这一点。我们应该使用该类javascript将“粘性”类添加到粘性按钮和切换按钮。
JS代码应该是这样。
希望这会有所帮助。愉快的编码〜:)
Brian Preston. Unfortunately, we can't do this using only CSS. We should Javascript to add "sticky" class to sticky button and toggle button using that class.
And JS code should be like this.
Hope this helps. Happy coding ~ :)