This is an experimental jQuery plugin which animates according to a CSS selector. The plugin looks up the selectors in the StyleSheet/s of a document and then animates according to the specified properties. It’s similar to jQuery UI’s ‘classTransitions‘ although it’s much more capable; not only can it animate to classes but any selector, as long as it’s specified in the CSS.
Probably the most useful thing about this plugin, and the main reason I created it, is that it can animate to pseudo classes such as ‘:hover’. This means the plugin can animate your hover states too; and because they’re specified in the CSS they won’t fail when JavaScript is disabled. This is what progressive enhancement is really about!
See demonstration and documentation (& download)!
With the plugin I’ve included Resig’s color extension so not only can it animate size properties like ‘width’, ‘left’ or ‘padding’ but it can also handle color properties like ‘border-color’, ‘color’ or ‘background-color’.
It’s not a simple “call n’ go” type of plugin, you do need to specify a selector (which must exist in the document’s StyleSheet/s) and you also need to specify properties to retrieve from that selector. Here’s an example of how one might call the plugin:
$('#navigation a').animateToSelector({ // One or more selectors; must exist // within the CSS StyleSheet. First one // has precedence ([0]): selectors: ['#navigation a:hover'], // You need to specify what properties to // animate within the selector: properties: [ 'background-color', 'padding-left', 'color' ], // An optional setting, without this setting // the animation proceeds straight away: events: ['mouseover', 'mouseout'], // Duration of animation: duration: 300 }); |
The above code would make all anchors within #navigation
animate to the specified properties of the specified selector onMouseOver
and then will revert the animation onMouseOut
. So, the plugin get’s the properties from here:
#navigation a:hover { padding-left: 20px; background-color: #bdd70d; color: #222; } |
The name of the plugin can be misleading – when you pass selectors into the ‘selectors’ array as a setting you are not selecting elements within the DOM – the plugin doesn’t even touch the DOM – it simply scans through the StyleSheets for the corresponding selectors and properties.
If you missed it earlier, here’s a link to the demo and documentation. There are a few issues (not bugs) with the implementation; it works in all modern browsers (only tested on Windows XP though). Comments and crits welcome!
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Very nice indeed. Your demo reminds me of the Safari animation CSS stuff.
Looks nice. It’ll come in handy for sure.
Any reason your using arrays instead of objects? Seems like jQuery uses objects more, plus I like the curly braces more, haha.
What would be cool is if the selector and properties also excepted strings. Like you said, your normally only going to use one selector, so let the user use a string or an array and figure out what it is behind the scenes.
The properties could even be just a comma seperated list.
im stupid, ignore the part about arrays and objects from my last comment.
nice plugin James, definitely gonna use this one!
By the way…if you specify the
@charset
At-Rule as first statement in your stylesheet (e.g. utf-8), the script will fail due to the following error: rule.SelectorText is undefinedHavent found time yet to hunt down this flaw….but perhaps you know a quick solution.
Cheers!
same for the
@import
rule I just found out…Great plugin! I’m wondering if it’s possible for the plugin to select ALL selector properties (ie. [ * ]), and then you have the option to define the specific properties if necessary. Just a thought.
I’m trying to use your plugin b/c it looks great, but I’m seeing an issue in firebug, and it’s not working. I was hoping you could offer some insight…Here’s the error:
@Shane, Thanks ๐
@Corey, yep, I’ll try and add that capability to the next version. Thank you for your comment. ๐
@Boye, I’ll have a look into it, the plugin is experimental so there was bound to be quite a few problems…
@Adam, The plugin can handle the ‘*’ selector, but it can’t select ALL properties within a StyleSheet.
@Will, It’s hard to tell but it looks like you’re not loading in jQuery. Do you have a link to the page?
Emailed you with the info, james.
James, scratch that…I figured it out..still not sure what was up w/ the function error, but I linked to Google and it worked. Another issue was that I’m an idiot….forgot $(document).ready….: thanks for the help anyways!
maybe you should add the little remark that the plugin won’t work if java script is placed in the head. When I put it at the end of the body it worked.
so far ! thank you for this nice plugin
really nice.
nice one …
James,
I’m working on a project and I’m having trouble. I keep getting security error code 1000 the line it references is $.each(ss.cssRules, function(i, rule){
Any ideas? The css code is the exact same as referenced in the js file.
Can you please confirm if this function call will work?
$(“#gallery a span”).animateToSelector({selectors: [‘#gallery a:hover span’], properties: [‘border’], events: [‘mouseover’,’mouseout’], duration: 3000});
Hi James,
Nice plugin.
The problem is, after a few mouse overs, I get an error at this line :
a.elem.style[c] = “rgb(” + [Math.max(Math.min(parseInt((a.pos * (a.end[0] – a.start[0])) + a.start[0]), 255), 0), Math.max(Math.min(parseInt((a.pos * (a.end[1] – a.start[1])) + a.start[1]), 255), 0), Math.max(Math.min(parseInt((a.pos * (a.end[2] – a.start[2])) + a.start[2]), 255), 0)].join(“,”) + “)”
The error says that it has invalid property value.
Please help with this error.
Thanks
Hi James,
I tried debugging the js file.
The problem is with the [c] in a.elem.style[c], which has a value ‘backgroundcolor’ instead of ‘background-color’
Please give a solution for this.
Thanks again! ๐