简体   繁体   English

如何防止隐藏的按钮阻止链接被单击

[英]How to prevent hidden button blocking links from being clicked

I'm using the below code to add a "back to top" button that appears after scrolling past a certain point on the page. 我正在使用下面的代码添加一个“返回页首”按钮,该按钮在滚动超过页面上的某个点后出现。 The issue is that this button is blocking my CTA button from being clicked on the top part of the page on mobile because even though it's hidden it's still there. 问题在于此按钮阻止我的CTA按钮在移动设备页面的顶部被单击,因为即使它被隐藏了,它仍然存在。 I've discovered that this is due to the fact I'm using "z-index: 10" but if I remove that then the back to top button appears under the rest of the content on the page. 我发现这是由于我使用的是“ z-index:10”,但是如果我删除了它,则“返回页首”按钮将显示在页面其余内容下方。

How can I ensure the button doesn't block any other content while hidden or completely remove it from the page until you scroll past a certain point? 我如何确保按钮在隐藏时不会阻止任何其他内容,或者直到您滚动到某个点之前才将其从页面中完全删除? Is this possible with js? js有可能吗? I'd like to avoid jquery if possible. 如果可能,我想避免使用jquery。

 myID = document.getElementById("myID"); var myScrollFunc = function () { var y = window.scrollY; if (y >= 700) { myID.className = "bottomMenu show" } else { myID.className = "bottomMenu hide" } }; window.addEventListener("scroll", myScrollFunc); 
 .bottomMenu { position: fixed; bottom: 0; z-index: 10; transition: all 1s; } .hide { opacity:0; left:-100%; } .show { opacity:1; left:0; } .sticky-divi-button { color: #ffffff; font-family: "Roboto"; font-size: 18px; background-color: #f5a623; border-radius: 30px; Letter-spacing: 0.8px; text-transform: uppercase; text-decoration: none; box-shadow: 0px 25px 28px -21px rgba(194,180,190,1); padding-left: 30px !important; padding-right: 30px !important; padding: 20px 3%; z-index: 10; position: fixed; bottom: 40px; right: 40px; } @media (max-width: 767px) and (min-width: 0px) { .sticky-divi-button { bottom: 10px !important; right: 10px !important; } } 
 <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><div id="myID" class="bottomMenu hide"><a href="#" class="sticky-divi-button">GET STARTED</a></div> 

The answer is as simple as adding pointer-events: none; 答案就像添加pointer-events: none;一样简单pointer-events: none; to your css (on the hidden state). 到您的CSS(处于隐藏状态)。 It will prevent any interactions with the element, allowing you to "click through" it. 它将阻止与元素的任何交互,从而允许您“单击”它。 :) :)

I modified css structure. 我修改了CSS结构。

 myID = document.getElementById("myID"); var myScrollFunc = function () { var y = window.scrollY; if (y >= 700) { myID.classList.add("show"); } else { myID.classList.remove("show"); } }; window.addEventListener("scroll", myScrollFunc); 
 .bottomMenu { position: fixed; left: -100%; bottom: 0; z-index: 10; transition: all 1s; } .bottomMenu .sticky-divi-button{ right: -100%; } .bottomMenu.show .sticky-divi-button{ right:2%; } .sticky-divi-button { color: #ffffff; font-family: "Roboto"; font-size: 18px; background-color: #f5a623; border-radius: 30px; Letter-spacing: 0.8px; text-transform: uppercase; text-decoration: none; box-shadow: 0px 25px 28px -21px rgba(194,180,190,1); padding-left: 30px !important; padding-right: 30px !important; padding: 20px 3%; z-index: 10; transition: all 1s; position: fixed; bottom: 40px; right: 40px; } @media (max-width: 767px) and (min-width: 0px) { .bottomMenu.sticky-divi-button{ bottom: 10px !important; } .bottomMenu.show .sticky-divi-button { right: 10px !important; } } 
 <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <div id="myID" class="bottomMenu"><a href="#" class="sticky-divi-button">GET STARTED</a></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM