简体   繁体   English

保持 hover state 在 div 中处于活动状态

[英]Keep hover state active across divs

I want to know how you keep the hover state of div element active accross multiple div elements.我想知道您如何在多个 div 元素中保持 div 元素的 hover state 处于活动状态。 So if i hover over Menu-item 1 then go to Menu-item 6 Menu item 1 is active while menu item 6 is active then I'd goto menu item 9 and thats active so my previous Menu Items are active.因此,如果我在菜单项 1 上使用 hover 然后 go 到菜单项 6 菜单项 1 处于活动状态,而菜单项 6 处于活动状态,那么我将转到菜单项 9 并且那是活动的,因此我以前的菜单项处于活动状态。

CSS: CSS:

.menuitem-active {
     background-color: #ff9900;
}
.menuitem {
     background-color: #ffffff;
}

HTML: HTML:

<div class="container">
    <div class="menu">
        <div class="menu-item">Menu Item 1</div>
        <div class="menu-item">Menu Item 2</div>
        <div class="menu-item">Menu Item 3</div>
        <div class="menu-item">Menu Item 4</div>
    </div>
</div>
<div class="container">
    <div class="menu">
        <div class="menu-item">Menu Item 5</div>
        <div class="menu-item">Menu Item 6</div>
        <div class="menu-item">Menu Item 7</div>
        <div class="menu-item">Menu Item 8</div>
    </div>
</div>
<div class="container">
    <div class="menu">
        <div class="menu-item">Menu Item 9</div>
        <div class="menu-item">Menu Item 10</div>
        <div class="menu-item">Menu Item 11</div>
        <div class="menu-item">Menu Item 12</div>
    </div>
</div>

Javascript: Javascript:

$('.container .menu .menu-item').mouseenter(function(){

});

Assuming you define a selected class for the hover effect:假设您为 hover 效果定义了一个selected的 class:

$('.container .menu .menu-item').mouseenter(function() {
    $(this).siblings().removeClass('selected');
    $(this).addClass('selected');
});

The siblings() function will select the menu items only within the current element's parent. siblings() function 将 select 菜单项仅在当前元素的父级中。

you might want to close off some of those class container since it was left unclosed.您可能想要关闭其中一些 class 容器,因为它未关闭。 Anyhow, mouseenter doesn't really seem practical since the divs will span 100% and as you move down, every one of them will become active.无论如何,mouseenter 似乎并不实用,因为 div 将跨越 100%,并且当您向下移动时,它们中的每一个都会变得活跃。

Assuming you set an active style in css just change its attribute to "active" or "item" I'd recommend changing it to click event假设您在 css 中设置了一个活动样式,只需将其属性更改为“活动”或“项目”我建议将其更改为点击事件

$('.container .menu .menu-item').click(function() {
    $(this).attr("class","menu-active");
});

Here is an example http://jsfiddle.net/robx/RsNw3/这是一个示例http://jsfiddle.net/robx/RsNw3/

You can do this in pure CSS(3): http://jsfiddle.net/rudiedirkx/W8McN您可以在纯 CSS(3) 中执行此操作: http://jsfiddle.net/rudiedirkx/W8McN

I added -webkit- and -moz- vendor prefixes, because (unstandardized) transitions are necessary for this.我添加了-webkit--moz-供应商前缀,因为为此需要(非标准化)转换。

You could try this:你可以试试这个:

$('.container > .menu > .menu-item').hover(

function() { // first function, the 'mouseenter'
    $(this).addClass('active');
},
function() { // the second function, the 'mouseleave'
    $(this).removeClass('active')
});

JS Fiddle demo . JS 小提琴演示

Incidentally, please pay attention to your quoting of attribute values, the class="container left an un-termintated string, which can cause problems all by itself.顺便说一句,请注意您对属性值的引用, class="container留下了一个未终止的字符串,这可能会导致问题。

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

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