Yesterday I posted an example of highlight/fadeout effects done with jQuery. It wasn't necessarily that exciting, but it's not something I've done before so it was fun to build. Fellow evangelist (and my boss, so yeah, his comments get special attention) Kevin Hoyt commented that what I had achieved would be possible with CSS and transitions.
Now - I will admit to being a bit codependent on jQuery. It's not like I'm addicted. I can stop using jQuery whenever I want to. In fact, I went out of my way to in this post. I love jQuery, but I can definitely recognize that I'm almost depending on it like a crutch, so I thought I'd take the opportunity to try it with pure CSS.
Before even approaching the idea of an animation, I thought I'd try a simple hover. That turned out to be trivial:
.tagcloudword:hover {
opacity:1;
font-weight: bold;
}
.tagcloudword { opacity: 0.5; }
That worked well enough where even if everything else I did failed, I'd be ok. Notice I also added the bold there to make it a bit more fancy. You can try this here: http://www.raymondcamden.com/demos/2012/feb/3/take2.html
I knew next to nothing about animation and transitions in CSS, but quickly found the docs at MDN to be incredibly useful. Begin with their CSS page: https://developer.mozilla.org/en/CSS. They've got a page on animations and transitions. Obviously transitions are what we want here.
I won't try to replicate the excellent docs over at MDN, but the basic gist of it seems to be this:
- Define your CSS for the item.
- Define your CSS for the hover.
- Then tell the CSS what properties are changing, how it changes (from a list of options like ease-in), and the duration.
The only thing missing from the docs that may trip you up is the use of vendor prefixes. MDN focuses on what works for them - the moz prefix. For Chrome, I had to add webkit as well. This means you have to duplicate your code, which is kinda sucky, but passable. So here was my first update:
.tagcloudword:hover {
opacity: 1;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 2000ms;
-moz-transition: opacity;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 2000ms;
}
.tagcloudword {
opacity: 0.1;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 2000ms;
-moz-transition: opacity;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 2000ms;
}
That's a lot of code, but if you ignore the moz lines it's really just 3 lines per state. Something else not immediately obvious to me was that if you define the transitions on hover and not on the core style block, you won't get an animation backwards from your hover state. You can demo this here: http://www.raymondcamden.com/demos/2012/feb/3/test3.html Be sure to notice that I slowed the transition down quite a bit to make it more obvious. I'd probably use 500ms instead.
Ok - finally. So how about multiple changes? Just add additional properties. Here's how I added some color too. In Chrome it seems to work really well. It goes from black to blood red to a pure red. (Don't hate me for color choices.)
.tagcloudword:hover {
opacity: 1;
color: red;
-webkit-transition: opacity,color;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 2000ms;
-moz-transition: opacity,color;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 2000ms;
}
.tagcloudword {
opacity: 0.1;
color: black;
-webkit-transition: opacity,color;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 2000ms;
-moz-transition: opacity,color;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 2000ms;
}
You can view this one here: http://www.raymondcamden.com/demos/2012/feb/3/test4.html
It works in Chrome and Firefox. In IE9 it is not supported, but it fails ok. You still get a hover effect.
Archived Comments
Ah, this is nice. And if the user has JavaScript turned off, the css method will continue to work.
http://caniuse.com/ is always mentioned when it comes to css3 techniques.
Oh, BTW, sorry about "going off" in your previous blog post. I've been giving it some thought over the past few days.
Don't apologize. I love it when topics mutate.
Ray,
you posted this "<cfajaxproxy bind="cfc:test.checkArtist({artist@keyup})" onSuccess="handleConflict">
<script>
function handleConflict(r){
if (r == true) {
document.getElementById("warning").innerHTML="This artist exists.";
document.getElementById("submitBtn").setAttribute("disabled", "disabled");
}
else {
document.getElementById("warning").innerHTML="";
document.getElementById("submitBtn").removeAttribute("disabled");
}
}
</script>
<cfform name="mainForm">
New Artist: <cfinput name="artist"><br/>
<div id="warning"></div>
<input type="submit" id="submitBtn" value="Save">
</cfform>
remote function checkArtist(string n) {
var q = new com.adobe.coldfusion.query();
q.setDatasource("cfartgallery");
q.setSQL("select lastname from artists where upper(lastname) = :name");
q.addParam(name="name",value="#ucase(arguments.n)#",cfsqltype="cf_sql_varchar");
var res = q.execute().getResult();
return res.recordCount==1;
}
" is it posible to call the remote function with javascript so that you have the user stay in the field until they enter something that is not found in the database?
I'm so spoiled. Thanks, Ray.
Ray - to save yourself some typing, you only actually need to apply the transitions to .tagcloudword (not to the :hover state). This then controls the transitions for all states of that class...
Arthur - your comment doesn't make sense. Can you please email me directly.
Seb - mind sharing an example of that? (Use pastebin of course, or post it on your server.)
"This means you have to duplicate your code, which is kinda sucky, but passable."
This is one of the advantages of using less css. It wouldn't make a great difference in your code example but for the transition code you code write a mixin and call it twice. The mixin would then output the css with all the different browser prefixes.
It would look something like:
.transition() {
-webkit-transition: opacity,color;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 2000ms;
-moz-transition: opacity,color;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 2000ms;
}
(And it could also take in arguments if needed).
The class definitions would look like:
.tagcloudword {
opacity: 0.1;
color: black;
.transition();
}
.tagcloudword:hover {
opacity: 1;
color: red;
.transition();
}
I haven't really added Less to my developer stack, but I'm very impressed by it. I'm not a fan of CoffeeScript since JS seems simple/powerful enough, but CSS _definitely_ needs this.
Or just:
[code]
.tagcloudword {
opacity: 0.1;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
}
.tagcloudword:hover {
opacity: 1;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
}
[/code]
What is "all" a shorthand for? Does it mean "All the crap I defined in this style block" ?
All means all properties specified in the rule should be transitioned. Your example for just opacity could be shortened to below.
.tagcloudword { opacity:0.5;
-webkit-transition: opacity 2s ease-out;
-moz-transition: opacity 2s ease-out;
transition: opacity 2s ease-out;
}
.tagcloudword:hover { opacity:1.0; }
What seb is eluding to above is the fact that you don't need to define the transition styles again on the hover pseudo selector.
Nope, I'm not seeing that. When I do this:
.tagcloudword {
opacity: 0.1;
color: black;
-webkit-transition: opacity,color 2s ease-out;
-moz-transition: opacity,color 2s ease-out;
}
.tagcloudword:hover {
opacity: 1;
color: red;
}
It correctly works _to_ the hover state, but when I mouse out, it instantly returns.
(So to be clear, I agree with you about the shorthand, and thank you, but don't agree, from what I see, that you can skip defining stuff on both 'sides')
Actually, something funky is going on. Check it here:
http://www.raymondcamden.co...
It seems to change the opacity immediately. Only color goes over time.
Ah, ok, this is where you use "all" instead of listing each item.
http://www.raymondcamden.co...
Works perfectly here, and a lot less typing.
Thanks Richard/Seb/Blackjk/all.