简体   繁体   English

如何使纯 css 下拉菜单与两个可重定向的链接一起工作?

[英]How to make pure css dropdown menu work with two re-directable links withim them?

I have already created a drop-down contact menu with 2 links within them.我已经创建了一个下拉联系菜单,其中包含 2 个链接。 But it seems that when I click on the contact button and then on the desired link, it just closes the dropdown menu without redirecting the user anywhere.但似乎当我点击联系按钮然后点击所需的链接时,它只是关闭下拉菜单而不将用户重定向到任何地方。 I read somewhere that it's because another mouse down event would trigger the :focus to be unfocused basically?我在某处读到这是因为另一个鼠标按下事件会触发 :focus 基本上不聚焦? How can I make this menu work?我怎样才能使这个菜单工作? I know only a little bit of javascript.我只知道一点点 javascript。

HTML below -下面的 HTML -

<div class="contact">
            <button class="contactbtn">Contact</button>
            <ul>
               <li>
                  <p>For Any Enquiries→</p>
               </li>
               <li><a id="mail" href="mailto:hello@shekleung.com">hello@shekleung.com</a></li>
               <li>+44 7463 070158</li><br>
               <li><a id="ig" href="https://www.instagram.com/samsonleung/?hl=en">Instagram</a></li>
            </ul>
         </div>

 .contact { position: relative; top: 5vh; right: 0; font-family: Helvetica, sans-serif; height: 10vh; letter-spacing: 3px; } .contactbtn { color: black; text-transform: uppercase; font-size: 1.5rem; font-weight: bold; background: none; border: none; text-decoration: none; cursor: pointer; outline: none; } .contactbtn:hover { opacity: 0.6; } .contact ul, .contact-a ul { position: absolute; left: -10vw; margin-top: 20px; width: 300px; height: 150px; font-size: 1.5rem; font-weight: bold; display: flex; flex-direction: column; justify-content: space-around; align-items: flex-end; list-style: none; text-transform: uppercase; font-size: 1rem; opacity: 0; pointer-events: none; transform: translateY(-10px); transition: all 0.4s ease; } .contact a, .contact-a a { color: black; text-decoration: none; } .contact a:hover, .contact-a a:hover { /* opacity: 0.6; */ background-color: var(--grCol3); } .contactbtn:focus+ul { opacity: 1; pointer-events: all; transform: translateY(0); outline: none; } .contact-a { position: absolute; top: 5vh; left: 5vw; font-family: Helvetica, sans-serif; height: 10vh; letter-spacing: 3px; color: var(--grCol3); } .contact-a ul { align-items: flex-start; left: 0; }
 <div class="contact-a"> <button class="contactbtn">Contact</button> <ul class="contact-content"> <li> <p>For Any Enquiries→</p> </li> <li><a href="mailto:hello@shekleung.com">hello@shekleung.com</a></li> <li>+44 7463 070158</li><br> <li><a href="https://www.instagram.com/samsonleung/?hl=en">Instagram</a></li> </ul> </div>

That is because you have set pointer-events: none for the unordered list.那是因为您为无序列表设置了pointer-events: none This affects it's children <li> as well.这也会影响它的孩子<li>

.contact ul,
.contact-a ul {
   position: absolute;
   left: -10vw;
   margin-top: 20px;
   width: 300px;
   height: 150px;
   font-size: 1.5rem;
   font-weight: bold;
   display: flex;
   flex-direction: column;
   justify-content: space-around;
   align-items: flex-end;
   list-style: none;
   text-transform: uppercase;
   font-size: 1rem;
   opacity: 0;
   pointer-events: none;  <<<<<<<<<<<<<<<<<<<<  cause of problem
   transform: translateY(-10px);
   transition: all 0.4s ease;
}

Better remove that.最好去掉那个。

Apply your focus logic on the container instead and don't use button:将您的焦点逻辑应用于容器而不是使用按钮:

 .contact { position: relative; top: 5vh; right: 0; font-family: Helvetica, sans-serif; height: 10vh; letter-spacing: 3px; } .contactbtn { color: black; text-transform: uppercase; font-size: 1.5rem; font-weight: bold; background: none; border: none; text-decoration: none; cursor: pointer; outline: none; } .contactbtn:hover { opacity: 0.6; } .contact ul, .contact-a ul { position: absolute; left: -10vw; margin-top: 20px; width: 300px; height: 150px; font-size: 1.5rem; font-weight: bold; display: flex; flex-direction: column; justify-content: space-around; align-items: flex-end; list-style: none; text-transform: uppercase; font-size: 1rem; opacity: 0; transform: translateY(-10px); transition: all 0.4s ease; } .contact a, .contact-a a { color: black; text-decoration: none; } .contact a:hover, .contact-a a:hover { /* opacity: 0.6; */ background-color: var(--grCol3); } .contact-a:focus ul { opacity: 1; pointer-events: all; transform: translateY(0); outline: none; } .contact-a { position: absolute; top: 5vh; left: 5vw; font-family: Helvetica, sans-serif; height: 10vh; letter-spacing: 3px; color: var(--grCol3); outline: none; } .contact-a ul { align-items: flex-start; left: 0; }
 <div class="contact-a" tabindex="-1"> <div class="contactbtn">Contact</div> <ul class="contact-content"> <li> <p>For Any Enquiries→</p> </li> <li><a href="mailto:hello@shekleung.com">hello@shekleung.com</a></li> <li>+44 7463 070158</li><br> <li><a href="https://www.instagram.com/samsonleung/?hl=en">Instagram</a></li> </ul> </div>

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

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