可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there any way to do the opposite of :hover
using only CSS? As in: if :hover
is on Mouse Enter
, is there a CSS equivalent to on Mouse Leave
?
Example:
I have a HTML menu using list items. When I hover one of the items, there is a CSS color animation from #999
to black
. How can I create the opposite effect when the mouse leaves the item area, with an animation from black
to #999
?
jsFiddle
(Have in mind that I do not wish to answer only this example, but the entire "opposite of :hover
" issue.)
回答1:
If I understand correctly you could do the same thing by moving your transitions to the link rather than the hover state:
ul li a {
color:#999;
transition: color 0.5s linear; /* vendorless fallback */
-o-transition: color 0.5s linear; /* opera */
-ms-transition: color 0.5s linear; /* IE 10 */
-moz-transition: color 0.5s linear; /* Firefox */
-webkit-transition: color 0.5s linear; /*safari and chrome */
}
ul li a:hover {
color:black;
cursor: pointer;
}
http://jsfiddle.net/spacebeers/sELKu/3/
The definition of hover is:
The :hover selector is used to select elements when you mouse over
them.
By that definition the opposite of hover is any point at which the mouse is not over it. Someone far smarter than me has done this article, setting different transitions on both states - http://css-tricks.com/different-transitions-for-hover-on-hover-off/
#thing {
padding: 10px;
border-radius: 5px;
/* HOVER OFF */
-webkit-transition: padding 2s;
}
#thing:hover {
padding: 20px;
border-radius: 15px;
/* HOVER ON */
-webkit-transition: border-radius 2s;
}
回答2:
Just use CSS transitions instead of animations.
A {
color: #999;
transition: color 1s ease-in-out;
}
A:hover {
color: #000;
}
Live demo
回答3:
No there is no explicit property for mouse leave in CSS.
You could use :hover on all the other elements except the item in question to achieve this effect. But Im not sure how practical that would be.
I think you have to look at a JS / jQuery solution.
回答4:
You can use CSS3 transition
Some good links:
http://css-tricks.com/different-transitions-for-hover-on-hover-off/
http://www.alistapart.com/articles/understanding-css3-transitions/
回答5:
You have misunderstood :hover
; it says the mouse is over an item, rather than the mouse has just entered the item.
You could add animation to the selector without :hover
to achieve the effect you want.
Transitions is a better option: http://jsfiddle.net/Cvx96/
回答6:
The opposite of :hover
appears to be :link
.
(edit: not technically an opposite because there are 4 selectors :link
, :visited
, :hover
and :active
. Five if you include :focus
.)
For example when defining a rule .button:hover{ text-decoration:none }
to remove the underline on a button, the underline shows up when you roll off the button in some browsers. I've fixed this with .button:hover, .button:link{ text-decoration:none }
This of course only works for elements that are actually links (have href attribute)
回答7:
Although answers here are sufficient, I really think W3Schools example on this issue is very straightforward (it cleared up the confusion (for me) right away).
Use the :hover
selector to change the style of a button when you move
the mouse over it.
Tip: Use the transition-duration property to determine the speed of
the "hover" effect:
Example
.button {
-webkit-transition-duration: 0.4s; /* Safari & Chrome */
transition-duration: 0.4s;
}
.button:hover {
background-color: #4CAF50; /* Green */
color: white;
}
In summary, for transitions where you want the "enter" and "exit" animations to be the same, you need to employ transitions on the main selector .button
rather than the hover selector .button:hover
. For transitions where you want the "enter" and "exit" animations to be different, you will need specify different main selector and hover selector transitions.