在没有jQuery的情况下悬停时图像改变不透明度

时间:2022-06-17 19:56:01

I am trying to make a list of social buttons. When one button is hovered it lights up, and the other buttons should darken.

我正在尝试列出社交按钮。当一个按钮悬停时,它会亮起,其他按钮应该变暗。

I got it to work partially. When the first button is hovered all works fine, but it is malfunctioning when the other buttons are hovered. I think this happens because all the latter list items are siblings of the first and the :hover event can't cause previous elements to change style. Does anyone have any thoughts on how to get this to work?

我让它部分工作了。当第一个按钮悬停时,一切正常,但是当其他按钮悬停时,它会出现故障。我认为这是因为所有后面的列表项都是第一个的兄弟节点而且:hover事件不能导致以前的元素改变样式。有没有人对如何使这个工作有任何想法?

Here's the code (jsFiddle live demo):

这是代码(jsFiddle现场演示):

<div id="bg">

<ul id="social_buttons">
    <li id="item_1"><img src="http://i43.tinypic.com/104rasi.png"></li>
    <li id="item_2"><img src="http://i43.tinypic.com/k4tide.png"></li>
    <li id="item_3"><img src="http://i44.tinypic.com/2ry0gt3.png"></li>
</ul>

</div>
#bg {
    height: 100px;
    width: 215px;
    background-color: black;
    position: relative;
}

#social_buttons img {
     width: 24px; 
     height: 24px;

    -webkit-transition:all 0.2s ease-in-out;
    -moz-transition:all 0.2s ease-in-out;
    -o-transition:all 0.2s ease-in-out;
    transition:all 0.2s ease-in-out;
}



#social_buttons {
    list-style-type: none;
    position: absolute;
    top: 20px;
    right: 70px;

    -webkit-transition:all 0.2s ease-in-out;
    -moz-transition:all 0.2s ease-in-out;
    -o-transition:all 0.2s ease-in-out;
    transition:all 0.2s ease-in-out;
}


#social_buttons li {
    float: left;
    margin-right: 2px;

    -webkit-transition:all 0.2s ease-in-out;
    -moz-transition:all 0.2s ease-in-out;
    -o-transition:all 0.2s ease-in-out;
    transition:all 0.2s ease-in-out;
}


/*  puur css3 multiple hover animaties */
#item_1 {
    opacity: 0.8;
}
#item_1:hover ~ #item_2, :hover ~ #item_3 {
    opacity: 0.5;
}
#item_1:hover {
    opacity: 0.9;
}


#item_2 {
    opacity: 0.8;
}
#item_2:hover ~ #item_1, :hover ~ #item_3 {
    opacity: 0.5;
}
#item_2:hover {
    opacity: 0.9;
}

#item_3 {
    opacity: 0.8;
}
#item_3:hover ~ #item_1, :hover ~ #item_2 {
    opacity: 0.5;
}
#item_3:hover {
    opacity: 0.9;
}

3 个解决方案

#1


3  

This is actually pretty easy with the right selectors

对于正确的选择器,这实际上非常简单

jsFiddle

ul li {
    opacity: 0.8;
}

ul:hover li {
    opacity: 0.5;
}

ul li:hover {
    opacity: 1.0;
}

#2


5  

Something like this?

像这样的东西?

#social_buttons:hover li {
    opacity: 0.5;
}

#social_buttons li:hover {
    opacity: 0.9;
}

General sibling selector (~) can't select preceding element, it works only in one direction.

一般兄弟选择器(〜)不能选择前一个元素,它只能在一个方向上工作。

#3


1  

http://jsfiddle.net/VnMFP/4/

#social_buttons li {
    opacity: 0.8;
}

#social_buttons:hover li {
    opacity: 0.5;
}

#social_buttons li:hover {
    opacity: 0.9
}

Don't select the li:hover, select ul#social_buttons:hover li. Then it works.

不要选择li:hover,选择ul#social_buttons:hover li。然后它工作。

#1


3  

This is actually pretty easy with the right selectors

对于正确的选择器,这实际上非常简单

jsFiddle

ul li {
    opacity: 0.8;
}

ul:hover li {
    opacity: 0.5;
}

ul li:hover {
    opacity: 1.0;
}

#2


5  

Something like this?

像这样的东西?

#social_buttons:hover li {
    opacity: 0.5;
}

#social_buttons li:hover {
    opacity: 0.9;
}

General sibling selector (~) can't select preceding element, it works only in one direction.

一般兄弟选择器(〜)不能选择前一个元素,它只能在一个方向上工作。

#3


1  

http://jsfiddle.net/VnMFP/4/

#social_buttons li {
    opacity: 0.8;
}

#social_buttons:hover li {
    opacity: 0.5;
}

#social_buttons li:hover {
    opacity: 0.9
}

Don't select the li:hover, select ul#social_buttons:hover li. Then it works.

不要选择li:hover,选择ul#social_buttons:hover li。然后它工作。