简体   繁体   English

如何仅使用“普通”html、CSS 和 Javascript 使按钮出现在滚动条上

[英]How can I make a button appear on scroll just using 'plain' html, CSS and Javascript

I've tried to accomplish this, (since I am a noob at coding...) with a concept I found online... it didn't work out very well, and offline the scroll padding and "Sign In' button appears for a second than disappears...我试图用我在网上找到的一个概念来实现这一点(因为我是编码的菜鸟......)......效果不太好,并且离线滚动填充和“登录”按钮出现一秒钟后就消失了……

To view web:要查看 web:

Click Here点击这里

if someone can please send me a simple code including: Html, (Css,) JavaScript, (no Jquery) of how to accomplish the same idea (of the button) on that site...如果有人可以请给我一个简单的代码,包括:Html,(Css,)JavaScript,(没有Jquery)如何在该网站上完成相同的想法(按钮)......

Thank You!谢谢你!

You want to listen for window scroll events and check to see if the main button is visible.您想监听 window 滚动事件并检查主按钮是否可见。 You can do this by checking it's position inside the window using Element.getBoundingClientRect();您可以通过使用Element.getBoundingClientRect();

The bottom property tells you how many pixels the bottom of the element is from the top of the screen. bottom属性告诉你元素的底部距离屏幕顶部有多少像素。 If the number is below zero, it means the bottom of the element is off the top of the screen.如果数字小于零,则表示元素的底部不在屏幕顶部。

At that point, just toggle the hidden button into view.此时,只需将隐藏按钮切换到视图中。 In my demo, I did this by adding a CSS class to that button.在我的演示中,我通过向该按钮添加 CSS class 来做到这一点。

If the user scrolls up and the main button is visible again, you can hide the side button by removing the class you previously added to it.如果用户向上滚动并且主按钮再次可见,您可以通过删除之前添加到其中的 class 来隐藏侧按钮。

JS Fiddle Demo Here JS Fiddle 演示在这里

Using scroll event with getBoundingClientRect may decrease performance You can use intersection observer for checking an element enter in viewport https://developers.google.com/web/updates/2016/04/intersectionobserver使用带有 getBoundingClientRect 的滚动事件可能会降低性能您可以使用交叉点观察器检查在视口中输入的元素https://developers.google.com/web/updates/2016/04/intersectionobserver

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在按下按钮后显示某些获取数据。 使用 JavaScript 和 HTML - How can I make certain fetch data appear after pressing a button. Using JavaScript and HTML 我如何使用JavaScript和CSS使栏出现和消失 - How can i make a bar appear and dissapear using javascript and css 如何在不使用任何mysql查询,仅使用简单的javascript或jquery的情况下在HTML表中进行搜索? - How can I search in a HTML table without using any mysql queries, just a plain javascript or jquery to be specific? 如何使图像出现在 javascript 中(并在放大时滚动) - How can I make a image appear in javascript (and scroll whilst zoomed in) 如何在HTML / Javascript / CSS中使动态文本在曲线上滚动? - How can I make dynamic text scroll on a curve in HTML/Javascript/CSS? 如何通过单击Javascript来使HTML TD设置为overflow:hiddenscroll隐藏滚动? - How can I make an HTML TD that's set to overflow:hidden scroll smoothly with the click of a button through Javascript? 如何使用 Javascript 使 div 出现 onclick? - How can I make a div appear onclick using Javascript? 如何仅使用 javascript(无 jquery)使 html 图像在滚动时淡入? - How can I make html images fade in on scroll using only javascript (no jquery)? 如何使 HTML/CSS 按钮出现在被隐藏的表格上方 - How to make HTML/CSS button appear above a table being hidden 选择选项(CSS)后如何使单选按钮显示为选中状态 - How can i make a radio button appear checked after selecting an option (CSS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM