I have following HTML:
<ul>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
</ul>
CSS
ul {
list-style: none;
text-align: center;
}
ul li {
position: relative;
float: right;
margin-right: 20px;
width: 30%;
}
ul li {
transition: all 0.3s;
}
ul li:hover {
top: -10px;
}
ul li> a{
color: red;
}
The question is the transition does not work with moz, it works on webkit. How do I implement this in a cross browser way?
DEMO
If you have a transition not working, check that the starting value of the property is explicitly set. Sometimes, you'll want to animate height and width when the starting or finishing value is auto . (For example, to make a div collapse, when its height is auto and must stay that way.)
This will animate the color property when you hover over a link on the page. Pretty simple, and you've probably seen or used something similar. But transitions are not just limited to use with :hover . You can animate CSS properties, thus use CSS transitions without hover.
The transition-property property specifies the name of the CSS property the transition effect is for (the transition effect will start when the specified CSS property changes). Tip: A transition effect could typically occur when a user hover over an element.
To trigger an element's transition, toggle a class name on that element that triggers it. To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. Then set those CSS properties of that element equal to those values you just got.
Browsers don't apply transition
on a property if an initial value for it is not specified in the element. Hence, adding top: 0px
to ul li
will solve the issue.
ul {
list-style: none;
text-align: center;
}
ul li {
position: relative;
float: right;
margin-right: 20px;
top: 0px; /* this line was added */
width: 30%;
}
ul li {
transition: all 0.3s;
}
ul li:hover {
top: -10px;
}
ul li> a {
color: red;
}
<!-- Library included just to avoid prefixes so that users with older browser can view -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<ul>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
<li>
<a href="#">
<h2>title</h2>
</a>
</li>
</ul>
Note: I would also suggest using the same option (transform
) as mentioned in Mr_Green's answer.
I don't know why top
css property is acting weird to do animation in firefox even it is listed as animation behaviour property in css.
Anyway, using margin-top
instead of top
is working fine in Firefox.
But I would like to suggest going with transform
's "translateX" and "translateY" css properties instead of using positioning from next time because it is efficient. (recommended by Paul Irish)
Try this:
ul li {
/* standard property and other vendor prefixes */
-moz-transition: -moz-transform 0.3s;
}
ul li:hover {
/* standard property and other vendor prefixes */
-moz-transform: translateY(-10px);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With