jQuery CSS Abstraction
PUBLISHED BY CHRIS COYIER
It should be said, first and foremost, that you should try to keep your styling and your JavaScript away from each other. If you want to change the style of an element with JavaScript, add (or remove) a class name from the element. Then use that class name as your hook in the CSS to affect the styling.
However, there are times when you need to apply styling within JavaScript. jQuery has a function just for it:
$("#thing").css("margin-right", 0);
It can even be kind of appealing, because jQuery handles some cross-browser weirdness for you. For example, opacity is one of those things that requires a variety of CSS properties to be cross-browser compatible. But with jQuery, you don't need to apply all of them. Just setting the opacity with the CSS function works. jQuery knows what kind of browser environment it is in and will apply the right property.
// just works
// also alternate CSS syntax
$("#thing").css({
opacity: 0.5
});
So you might extend that thinking into assuming that jQuery will help you out with other things that require multiple CSS properties to get the same cross browser effect, like the quintessential example of border-radius. We need -moz-border-radius for Mozilla browsers, -webkit-border-radius for WebKit browsers, and border-radius for Opera (and the future). But jQuery doesn't help us here.
// not gonna help in current Firefox or Safari/Chrome
$("#thing").css({
"border-radius": "20px"
});
To get cross-browser compatible CSS through jQuery, we'll still have to list all three:
$("#thing").css({
"border-radius": "20px",
"-moz-border-radius": "20px",
"-webkit-border-radius": "20px"
});
So what's up with that? David Walsh thinks it would be library bloat to have this abstraction within jQuery. Ben Alman thinks jQuery should handle fully supported CSS properties (the border-radius spec isn't officially final). Screwlewse thinks that the only reason opacity is supported that way is because it's required for animation (core functions like fadeToggle).
I'm not sure what I think 100%. On one hand it sure would be nice to have that handled magically for me. On the other I can understand the bloat and not-final-spec arguments.
What do you think?
If you have found yourself needing to apply rounded corners through jQuery, you might be smart to abstract it away into a little plugin for yourself.
$.fn.roundThis = function(radius) {
return this.each(function(e) {
$(this).css({
"border-radius": radius,
"-moz-border-radius": radius,
"-webkit-border-radius": radius
});
});
};
$(function() {
// usage
$("#thing-that-will-become-rounded").roundThis("20px");
});
This still won't handle IE though, but there are great plugins that do.
Getting Buggy CSS Selectors to Work Cross-Browser via jQuery
ReplyDeleteBy Louis Lazaris on March 1st, 2010 | 16 Comments
Although the lack of cross-browser CSS selector support has caused a number of useful CSS selectors to go almost unnoticed, developers can still manipulate styles on their pages using some of these little-used selectors through jQuery.
Below I’ve prepared a simple table that describes a number of CSS selectors that are not cross-browser compatible, along with the jQuery syntax for each. The syntaxes are exactly the same as they would be in CSS, save for the jQuery wrapper (just remove $() and the quotes to get the CSS syntax), so using these selectors in jQuery will provide somewhat of a practice ground to prepare you for when they’re fully supported by all commonly-used browsers.
No surprise that the biggest selector problems occur in connection with IE6.
CSS Selector Incompatible Browser(s) jQuery/CSS Syntax
Chained classes IE6 (no support) $(“p.one.two.three”)
ID and class on the same element IE6 (buggy support) $(“#one.two”)
Attribute Selector IE6 (no support)
All Other Browsers (buggy) $(“p[style]”)
CSS3 Attribute Selectors IE6 (no support),
IE7/8 (buggy) $(“a[href^='http:']”)
$(“a[href$='pdf']”)
$(“div[style*='border']”)
Child Selector IE6 (no support) $(“div>span”)
Adjacent Sibling Selector IE6 (no support),
Safari (buggy) $(“p+span”)
General Sibling Selector (CSS3) IE6 (no support),
IE7/8 (buggy) $(“p~span”)
First Child pseudo-class IE6 (no support),
Some Other Browsers (buggy) $(“li:first-child”)
If you want detailed information on what each selector is supposed to do, see the selector section in the SitePoint CSS Reference.