简体   繁体   English

如何将悬停效果更改为仅用于下拉菜单的onclick

[英]How to change hover effect to onclick only for a drop down menu

I've tried everything I can possibly think of and I can't get my test site to act similar to the example site button menu that moves upward on click (right now mine only works on hover). 我已经尝试了所有可能想到的方法,但我无法让测试站点的行为类似于单击时向上移动的示例站点按钮菜单(目前,我的菜单仅在悬停时有效)。 I've tried multiple drop down menus, javascript, css, accordion menus, fake on-click tutorials, etc... nothing works for my site. 我尝试了多个下拉菜单,javascript,css,手风琴菜单,伪造的单击教程等,但这些内容对我的网站均无效。

I got everything to work correctly, except the onclick function. 除了onclick函数之外,我所有的东西都能正常工作。 How can I get the current hover effect on my site to trigger only when I click the button first? 仅当我首先单击按钮时,如何才能在网站上获得当前的悬停效果以进行触发? Also, when I click another button, the previous one collapses back down to its normal state and the new clicked button opens like the hover state? 另外,当我单击另一个按钮时,前一个按钮会折叠回到其正常状态,而新单击的按钮会像悬停状态一样打开?

I think the hover effect is just to annoying/busy and I will need to make it responsive for mobile devices, so onclick seems the best route. 我认为悬停效果只是烦人/忙碌,我需要使其对移动设备响应,因此onclick似乎是最佳途径。

I've attached pictures of the buttons I'm referring to, Links to my site and the example site I'm trying to get to work like, and my css and html. 我已经附上了我所指的按钮的图片,指向我的网站的链接以及要尝试工作的示例网站以及CSS和html。 If anyone can help, you'd be the most amazing person in the world!!! 如果有人可以帮助您,您将成为世界上最神奇的人!!!

<div class="wrapper clearfix" id="insurance-buttons">
        <ul>
            <li><a href="#">Auto</a>
                <ul>
                    <li><a href="#">Auto</a></li>
                    <li><a href="#">Motorcycle</a></li>
                    <li><a href="#">Boat</a></li>
                    <li><a href="#">ATV</a></li>
                </ul>
             </li>
             <li><a href="#">Home</a>
                <ul>
                    <li><a href="#">Homeowners</a></li>
                    <li><a href="#">Condo / Co-op</a></li>
                    <li><a href="#">Flood Insurance</a></li>
                </ul>
             </li>
             <li><a href="#">Life &amp; Health</a>
                <ul>
                    <li><a href="#">Umbrella</a></li>
                    <li><a href="#">Life</a></li>
                    <li><a href="#">Health</a></li>
                </ul>
             </li>
             <li><a href="#">Business</a>
                <ul>
                    <li><a href="#">Business Owners</a></li>
                    <li><a href="#">General Liability</a></li>
                    <li><a href="#">Commercial Auto</a></li>
                    <li><a href="#">Workers Compensation</a></li>
                </ul>
             </li>
        </ul>
</div>


#insurance-buttons a { 
    display: block; 
    text-decoration: none; 
}
#insurance-buttons ul li ul li {
    width: 100%; 
    padding: 3px; 
    background: #fff;
    z-index:1;
}
#insurance-buttons ul li ul li a { 
    font-variant: small-caps;
    font-size:24px;
    line-height:25px;
    padding: 15px 0 !important;
}
#insurance-buttons li{ 
    position:relative; 
    float:left;
}
#insurance-buttons ul li ul, #insurance-buttons:hover ul li ul, #insurance-buttons:hover ul li:hover ul li ul { 
    display:none;
    list-style-type:none; 
    width: 101%;
}
#insurance-buttons:hover ul, #insurance-buttons:hover ul li:hover ul, #insurance-buttons:hover ul li:hover ul li:hover ul { 
    display:block;
}
#insurance-buttons:hover ul li:hover ul { 
    position: absolute;
    margin-top: 1px;
    margin-left:-3px;
}
#insurance-buttons>ul>li:hover>ul { 
    bottom:100%;
    border-bottom: 1px solid transparent;
}

#insurance-buttons ul {
    width:100%;
}
#insurance-buttons ul li {
    width:25%;
    float:left;
    text-align:center;
    text-transform:uppercase;
    font-size:42px;
    line-height:42px;
    font-family: 'Khand', sans-serif; 
    font-weight: 500 !important; 
    color:#fff;
}
#insurance-buttons ul li a {
    text-decoration:none !important; 
    padding:10px 15px !important;
    border-right: 4px solid  #fff;
}
#insurance-buttons ul li a:last-child {
    border-right: none;
}

http://www.taggrafx.com/testing/CIA/index.html http://www.taggrafx.com/testing/CIA/index.html 我的网站 http://www.nationwide.com http://www.nationwide.com 我正在尝试发挥作用的网站

I noticed you've still got the :hover pseudoclass in your CSS. 我注意到您的CSS中仍然有:hover伪类。 If you're trying to trigger the menus on click, you cannot use the :hover pseudoclass . 如果您试图在单击时触发菜单, 则不能使用:hover伪类 There's no faking it. 没有伪造。 So your first step is to replace the :hover pseudoclass with a proper class. 因此,第一步是用适当的类替换:hover伪类。 Here is a generic example: 这是一个通用示例:

/* change anything like this */
.my-element:hover { ... styles ... }

/* to this */
.my-element.hover { .. styles ... }

For this next part, I'm going to assume jQuery, because it's quicker and easier to write. 在下一部分中,我将假设使用jQuery,因为它编写起来更快,更容易。

Then, you need to detect a click on your element, apply the class, and remove it from your other elements. 然后,您需要检测到元素上的单击,应用该类,然后将其从其他元素中删除。 This can be done like so: 可以这样完成:

$('.my-element').on('click', function(e) {
  e.preventDefault();

  // remove .hover class from all other elements
  $('.my-element').removeClass('hover');

  // apply it to the current element
  $(this).addClass('hover');
});

These examples are not specific to your example, but it should get you on the right track. 这些示例并非特定于您的示例,但可以使您走上正确的道路。

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

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