[英]hover affects all elements in class, how to make it only affect the one I'm hovering over?
I have a menu bar with various buttons that drop down slightly and change color as I hover over them. 我有一个菜单栏,上面有各种按钮,当我将鼠标悬停在它们上面时,它们会略微下拉并更改颜色。
Changing color works fine, but when I hover over one button, all the buttons drop down (rather than just the one I'm hovering over). 更改颜色效果很好,但是当我将鼠标悬停在一个按钮上时,所有按钮都会下拉(而不仅仅是我正在悬停的按钮)。
HTML: HTML:
<div id="menu">
<div id="Button1" class="menuButton"></div>
<div id="Button2" class="menuButton"></div>
<div id="Button3" class="menuButton"></div>
</div>
CSS: CSS:
#menu {
display: block;
float: right;
position: absolute;
top:0;
left:0;
height: 30px;
}
.menuButton {
width: 36px;
height :40px;
position: relative;
display: inline-block;
margin-top: -10px;
background-color: rgba(200,50,50,0.25);
transition: margin-top .3s, background-color .3s;
}
.menuButton:hover {
margin-top: 0;
background-color: rgba(100,100,255,0.6);
}
Can anyone figure out why this is happening (and what I can do to make it so ONLY the element I hover over comes down)? 谁能弄清楚为什么会发生这种情况(以及我能做些什么,所以只有我悬停的元素下降了)?
JSFiddle: http://jsfiddle.net/howkx1nv/ JSFiddle: http : //jsfiddle.net/howkx1nv/
Your outer container height is 30px, while your buttons are 40px, but with a negative bottom margin. 您的外部容器高度为30px,而按钮为40px,但底边距为负。
When hovering over one of the buttons, it loses its top margin and thus stretches the outer container height to +10px, which causes all your other buttons to fill out the full 40px. 将鼠标悬停在其中一个按钮上时,它将失去其上边距,从而将外部容器的高度拉伸到+ 10px,这将导致您所有其他按钮填充整个40px。
That being said, a culprit in your code seems to be the usage of inline-block, as the blocks will behave in relation to each other. 话虽这么说,代码中的罪魁祸首似乎是内联块的使用,因为块之间的行为会相互关联。 If you simply change
display: inline-block
to float: left
in your original fiddle, you will see the correct behaviour. 如果仅将
display: inline-block
更改为float: left
放在原始小提琴中,您将看到正确的行为。
I've updated your jsFiddle to accomplish what you wanted: http://jsfiddle.net/howkx1nv/1/ 我已经更新了您的jsFiddle以完成您想要的: http : //jsfiddle.net/howkx1nv/1/
.menuButton {
width: 36px;
height :40px;
position: relative;
display: inline-block;
margin-top: -10px;
background-color: rgba(200,50,50,0.25);
transition: top .3s, background-color .3s;
}
.menuButton:hover {
position:relative;
top:10px;
background-color: rgba(100,100,255,0.6);
}
SquareCat explains the phenomenon of why all the divs are peeking out altogether. SquareCat解释了为什么所有div都一起偷看的现象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.