简体   繁体   English

悬停在另一个:hover上时保持:hover效果

[英]Keep :hover effect when hovering over another :hover

As you can see in this JSFiddle , while you hovering over text-content ; 正如您在此JSFiddle中所看到的那样 ,当您将鼠标悬停在text-content the opacity img and the img classes return to a normal state, and I would like to keep :hover from img and img opacity while hovering over text-content . opacity imgimg类返回到正常状态,我想将:hoverimgimg opacity同时将它们悬停在text-content

Also I would like to know how to set the background-color of image's background. 我也想知道如何设置图像背景的background-color

HTML HTML

<ul class="img-list">
    <li>
    <div class="table-responsive">
        <table align="left" cellpadding="1" cellspacing="1" class="modernTable">
            <tbody>
                <tr>
                    <td style="border-color: transparent; text-align: center; vertical-align: top; width: 175px; height: 80px; background-color: rgba(25, 25, 25, 0.1);">
                    <section class="opacity">
                        <img alt="" src="http://i.imgur.com/TrwyFfT.png" style="border-width: 0px; border-style: solid; margin: 0px; width: 175px; height: 79px;"><span style="font-size:14px;"><span class="text-content">This will show up</span></span></section>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    </li>
</ul>

CSS: CSS:

.opacity img {
    opacity:0.7;
    transition-duration: 0.5s;
}
.opacity img:hover {
    opacity:1;
    transition-duration: 1s;
}
img {
    background: rgba (0, 0, 0, 0.5);
    -webkit-transform: scale(0.9);
    -moz-transform: scale(0.9);
    -o-transform: scale(0.9);
    transform: scale(0.9);
    transition-duration: 0.5s;
}
img:hover {
    background: rgba (0, 0, 0, 0.9);
    -webkit-transform: scale(1.0);
    -moz-transform: scale(1.0);
    -o-transform: scale(1.0);
    transform: scale(1.0);
    transition-duration: 0.5s;
}
ul.img-list {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: center;
}
ul.img-list li {
    display: inline-block;
    height: 79px;
    margin: 0 1em 1em 0;
    position: relative;
    width: 175px;
}
span.text-content {
    background: rgba(0, 0, 0, 0.5);
    color: white;
    cursor: pointer;
    display: table;
    height: 20px;
    left: 0;
    position: absolute;
    top: 100px;
    width: 175px;
    opacity: 0;
    border: 1px dashed transparent;
    -webkit-transition: opacity 1s;
    -moz-transition: opacity 1s;
    -o-transition: opacity 1s;
    transition: opacity 1s;
}
span.text-content span {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
ul.img-list li:hover span.text-content {
    opacity: 1;
    border: 1px dashed rgba(255, 255, 255, 0.1);
}

How can I fix this? 我怎样才能解决这个问题?

No need for drastic changes in HTML or even to add javascript. 无需对HTML进行重大更改,甚至无需添加javascript。 ;) ;)

Instead of assigning CSS to .opacity img:hover try to assign it to .opacity:hover img like 与其将CSS分配给.opacity img:hover.opacity img:hover尝试将其分配给.opacity:hover img

    .opacity img { opacity:0.7; transition-duration: 0.5s; }

    .opacity:hover img { opacity:1; transition-duration: 1s; }

    .opacity img{ background: rgba (0, 0, 0, 0.5); -webkit-transform: scale(0.9); -moz-transform: scale(0.9); -o-transform: scale(0.9); transform: scale(0.9); transition-duration: 0.5s; }

    .opacity:hover img{ background: rgba (0, 0, 0, 0.9); -webkit-transform: scale(1.0); -moz-transform: scale(1.0); -o-transform: scale(1.0); transform: scale(1.0); transition-duration: 0.5s; }

The effect is basically the same and the size stays while hovering the text (except for the gap between image and text). 效果基本上是相同的,并且在悬停文本时大小保持不变(图像和文本之间的间隔除外)。 See the demo . 参见演示

Here is one solution using only CSS. 这是仅使用CSS的一种解决方案。 It does rearrange a bit of your HTML and CSS, though, because I felt you had a bit too much going on. 但是,它确实重新排列了HTML和CSS的一部分,因为我觉得您的工作太多了。 I have provided a simplified solution that should be transferable to whatever markup you're using without much/any difficulty. 我提供了一种简化的解决方案,该解决方案应该可以毫无问题地转移到您正在使用的任何标记上。

With the exact markup that you provided, the effect was not possible because the child element was removed from the normal document flow, thanks to position: absolute; 使用您提供的确切标记,由于子元素已从常规文档流中删除,因此无法实现效果,这要归功于position: absolute; . The key to keeping a parent element's hover state active when hovering over its child is to make sure the child is still within the parent's box model (and, therefore, "within the flow"). 将鼠标悬停在其父元素上时,保持其父元素的悬停状态处于活动状态的关键是确保该子元素仍位于父元素的框模型内(因此,在“流程内”)。

The below implementation differs from yours visually only in that the "grey" background extends downward to cover the child span element. 下面的实现与您的视觉上的不同之处仅在于“灰色”背景向下延伸以覆盖子span元素。

JSFiddle JSFiddle

 .opacity img:hover { opacity:1; transition-duration: 1s; } img { opacity:0.7; background: rgba (0, 0, 0, 0.5); -webkit-transform: scale(0.9); -moz-transform: scale(0.9); -o-transform: scale(0.9); transform: scale(0.9); transition-duration: 0.5s; } img:hover { background: rgba (0, 0, 0, 0.9); -webkit-transform: scale(1.0); -moz-transform: scale(1.0); -o-transform: scale(1.0); transform: scale(1.0); transition-duration: 1s; } .text-content { background: rgba(0, 0, 0, 0.5); color: white; cursor: pointer; display: table; height: 20px; width: 175px; opacity: 0; border: 1px dashed transparent; -webkit-transition: opacity 1s; -moz-transition: opacity 1s; -o-transition: opacity 1s; transition: opacity 1s; } .opacity:hover .text-content { opacity: 1; border: 1px dashed rgba(255, 255, 255, 0.1); } .opacity:hover img { background: rgba (0, 0, 0, 0.9); opacity: 1; -webkit-transform: scale(1.0); -moz-transform: scale(1.0); -o-transform: scale(1.0); transform: scale(1.0); transition-duration: 1s; } 
 <table align="left" cellpadding="1" cellspacing="1" class="modernTable"> <tbody> <tr> <td style="border-color: transparent; text-align: center; vertical-align: top; width: 175px; height: 80px; background-color: rgba(25, 25, 25, 0.1);"> <section class="opacity"> <img alt="" src="http://i.imgur.com/TrwyFfT.png" style="border-width: 0px; border-style: solid; margin: 0px; width: 175px; height: 79px;" /><span class="text-content">This will show up</span> </section> </td> </tr> </tbody> </table> 

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

相关问题 将文字悬停在其前面时保持图像悬停效果 - Keep image hover effect when hovering the text in front of it 将鼠标悬停在文本上时,悬停效果消失 - Hover-Effect disappears, when hovering over Text 将鼠标悬停在不是子代的工具提示上时,保持父样式的悬停样式 - Keep hover style on parent when hovering over a tooltip that is NOT a descendant 鼠标悬停在孩子上方时,对父对象保持:hover效果 - Keep :hover effect on parent, when mouse is over child 悬停在图像上的效果 - Hover Effect when over image hover 上的图像在将鼠标悬停在另一张图像上后出现,但将两张图像彼此相邻 - Image on hover appears after hovering over another image, but keep both images next to one another 悬停在分隔符上时取消悬停 - Cancel hover when hovering over seperator 在下拉列表中悬停时,如何在导航栏上保持悬停效果? - How do I keep the hover effect on the navbar when hovering through dropdown lists? 将鼠标悬停在表中的特定元素上时,如何不对表行触发悬停效果 - How to NOT trigger hover effect on table row when hovering over specific element inside it 悬停效果不起作用:将鼠标悬停在<a>锚标记上</a>时不会更改Div颜色 - Hover effect is not working: Div color is not changed when hovering over a <a> anchor tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM