Up to this point, the most common use for CSS3 Transitions has been in conjunction with the well-known CSS
Here’s a typical transition that changes link color on mouseover using pure CSS:
This will animate the
You can animate CSS properties via transitions using some other CSS techniques, a number of which I’ve outlined below. I’ve also included a demo for each example.
Here’s some CSS and an accompanying demo that demonstrates using
With this code, the box’s width and height properties are animated to
become larger as you hold the mouse down on the element. Once you
release, the box animates back to its original dimensions.
Here’s a demo:
And here’s a live demo:
We’ll just do a simple width change, which will appear to indent any selected checkbox:
And here’s a simple demo:
And the quick-and-dirty jQuery that adds/removes the
So when the last radio button is selected (the one with a value of “other”), the text box has its
The
Here’s the demo:
Nonetheless, this is just another way to use CSS3 transitions. The new Modernizr design does this.
Here’s the code:
This example animates two different properties simultaneously,
separating the two by a comma. The media query tells the box to shrink
if the screen size goes below 961px.
Resize your window in the demo page to see it work:
Also, I’ve included the
And the code examples that you can see in this post aren’t exactly the same as those used in the demo pages. I’ve made the code as brief as possible for the purpose of focusing only on the parts being discussed in this article.
:hover
pseudo-class.Here’s a typical transition that changes link color on mouseover using pure CSS:
- a, a:link, a:visited {
- color: lightblue;
- -webkit-transition: color .4s linear;
- -moz-transition: color .4s linear;
- -o-transition: color .4s linear;
- -ms-transition: color .4s linear;
- transition: color .4s linear;
- }
- a:hover {
- color: white;
- }
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 via transitions using some other CSS techniques, a number of which I’ve outlined below. I’ve also included a demo for each example.
Transitions Using :active
The:active
pseudo-class matches any element that’s in the process of being activated. The primary way that an element is “activated” is by means of a mouse click, or mouse down.Here’s some CSS and an accompanying demo that demonstrates using
:active
along with CSS3 transitions to mimic a mousedown
event:- .box {
- width: 300px;
- height: 300px;
- background: #222;
- -webkit-transition: width 2s ease, height 2s ease;
- -moz-transition: width 2s ease, height 2s ease;
- -o-transition: width 2s ease, height 2s ease;
- -ms-transition: width 2s ease, height 2s ease;
- transition: width 2s ease, height 2s ease;
- }
- .box:active {
- width: 500px;
- height: 500px;
- }
Here’s a demo:
Transitions Using :focus
You can use the:focus
pseudo-class to do something
similar. This time we’ll use a form, and we’ll animate the width of any
form element that receives focus:- input, textarea {
- width: 280px;
- -webkit-transition: width 1s ease;
- -moz-transition: width 1s ease;
- -o-transition: width 1s ease;
- -ms-transition: width 1s ease;
- transition: width 1s ease;
- }
- input:focus, textarea:focus {
- width: 340px;
- }
Transitions Using :checked
You can animate checkboxes and radio buttons when they become “checked” — although you’re limited with what you can do with those, since you can’t really change their native styling.We’ll just do a simple width change, which will appear to indent any selected checkbox:
- input[type="checkbox"]:checked {
- height: 20px;
- -webkit-transition: width 1s ease;
- -moz-transition: width 1s ease;
- -o-transition: width 1s ease;
- -ms-transition: width 1s ease;
- transition: width 1s ease;
- }
- input[type="checkbox"]:checked {
- width: 30px;
- }
Transitions Using :disabled
Keeping with the theme of web forms, this time we’ll combine CSS3 transitions with some jQuery to animate the background color of form elements when they become disabled via thedisabled
attribute:- input[type="text"], textarea {
- background: #e2e2e2;
- -webkit-transition: background 1s ease;
- -moz-transition: background 1s ease;
- -o-transition: background 1s ease;
- -ms-transition: background 1s ease;
- transition: background 1s ease;
- }
- input:disabled, textarea:disabled {
- background: #666666;
- }
disabled
attribute is:- $(function() {
- $('input[type="radio"]').click(function() {
- if ($(':checked').val() === "other") {
- $('input[type="text"]').removeAttr("disabled");
- } else {
- $('input[type="text"]').attr("disabled", "disabled");
- }
- });
- });
disabled
attribute removed. If another option is selected, the disabled
attribute is re-applied.The
:disabled
pseudo-class is dependent on that
attribute being present, so the animation will occur whenever that
attribute is added or removed.Here’s the demo:
Transitions Using Media Queries
This last one may be the least practical, because let’s face it, the only people that ever resize their window to see what happens are web developers.Nonetheless, this is just another way to use CSS3 transitions. The new Modernizr design does this.
Here’s the code:
- .box {
- width: 440px;
- height: 440px;
- background: #222;
- margin: 0 auto;
- -webkit-transition: width 2s ease, height 2s ease;
- -moz-transition: width 2s ease, height 2s ease;
- -o-transition: width 2s ease, height 2s ease;
- -ms-transition: width 2s ease, height 2s ease;
- transition: width 2s ease, height 2s ease;
- }
- @media only screen and (max-width : 960px) {
- .box {
- width: 300px;
- height: 300px;
- }
- }
Resize your window in the demo page to see it work:
Some Notes About the Code
If you’re wondering about the code, I’ve included the standard property last, even though it’s not supported by any browser that I know of.Also, I’ve included the
-ms-
prefix, even though IE9 and IE10 do not yet support transitions. But evidently they are coming in IE10, so it’s worth including an extra line.And the code examples that you can see in this post aren’t exactly the same as those used in the demo pages. I’ve made the code as brief as possible for the purpose of focusing only on the parts being discussed in this article.
CSS3 Transitions Without Using :hover
Reviewed by JohnBlogger
on
6:12 PM
Rating:
No comments: