<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:series="http://unfoldingneurons.com/"
><channel><title>David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞. &#187; CSS</title> <atom:link href="http://davidwalsh.name/tutorials/css/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Sun, 20 May 2012 22:40:48 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Selector Engines: Right to&#160;Left</title><link>http://davidwalsh.name/selectors</link> <comments>http://davidwalsh.name/selectors#comments</comments> <pubDate>Wed, 09 May 2012 01:33:59 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5428</guid> <description><![CDATA[One lessor known fact about CSS selectors, querySelectorAll, and JavaScript-based selector engines is that they read your selectors from right to left.  This news hit me as illogical at first, as you&#8217;d think that the first element in a selector string like &#8220;#myElement a.something .else&#8221; would provide a base context, but no:  the &#8220;.else&#8221; is [...]<p><a
href="http://davidwalsh.name/selectors">Selector Engines: Right to&nbsp;Left</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One lessor known fact about CSS selectors, querySelectorAll, and JavaScript-based selector engines is that they read your selectors from right to left.  This news hit me as illogical at first, as you&#8217;d think that the first element in a selector string like &#8220;#myElement a.something .else&#8221; would provide a base context, but no:  the &#8220;.else&#8221; is search for first.  After more thought, searching for the right-most selector piece makes sense, as you instead collect the &#8220;.else&#8221; elements first (instead of, theoretically, all the elements under &#8220;#myElement&#8221;, then &#8220;a.something&#8221; elements, and so on) and then look up the chain for matches.  Essentially, you grab all the potential matches and then confirm by walking up the DOM tree, instead of grabbing the parent and look for matches on the way down.</p><p>I was recently looking at popular development site and found the following snippet:</p><pre class="js">
jQuery("#subscribe-main li:nth-child(4)")....
</pre><p>This snippet found the desired elements in 1ms according to FireBug&#8217;s console.  A millisecond is lightning fast, but if you slightly change the selector code, you get a faster result:</p><pre class="js">
jQuery("li:nth-child(4)", "#subscribe-main");

// Could use this as well
// jQuery("#subscribe-main").find("li:nth-child(4)")....
</pre><p>The selection code above returns the same elements in 0ms.  A millisecond difference is negligible in one instance, but in a large application, these milliseconds will add up!</p><p>This post simply acts a a reminder that selector composition is important.  Here&#8217;s a task for you:  go to the sites you&#8217;ve written the JavaScript selectors for and compare your selectors like I have above.  If you aren&#8217;t familiar with basic selector time testing via the console, here&#8217;s how you do it:</p><pre class="js">
console.time("someKey");
jQuery("#mySelector .more .stuff")...
console.timeEnd("someKey");
</pre><p>The console doesn&#8217;t do better than millisecond precision, but a different result at that precision gets you started in selector enhancement.  Happy selector revising!</p><p><a
href="http://davidwalsh.name/selectors">Selector Engines: Right to&nbsp;Left</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/selectors/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Detect DOM Node Insertions with JavaScript and CSS&#160;Animations</title><link>http://davidwalsh.name/detect-node-insertion</link> <comments>http://davidwalsh.name/detect-node-insertion#comments</comments> <pubDate>Mon, 07 May 2012 14:20:20 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5425</guid> <description><![CDATA[I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel&#8217;s shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API. This hack uses JavaScript, as you would expect, but another technology you wouldn&#8217;t [...]<p><a
href="http://davidwalsh.name/detect-node-insertion">Detect DOM Node Insertions with JavaScript and CSS&nbsp;Animations</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>I work with an awesome cast of developers at Mozilla, and one of them in <a
href="http://www.backalleycoder.com/">Daniel Buchner</a>.  Daniel&#8217;s shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated <a
href="http://davidwalsh.name/dom-events-javascript">DOM Events API</a>.  This hack uses JavaScript, as you would expect, but another technology you wouldn&#8217;t expect:  CSS animations.  Let me prove to you that it works!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/detect-node-insertion.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;HTML</h2><p>All that&#8217;s required is a parent element with which we&#8217;d like to listen to node insertions within:</p><pre class="html">
&lt;ul id="parentElement"&gt;&lt;/ul&gt;
</pre><p>You can use any selector helper you&#8217;d like, but I&#8217;ve chosen an ID here.</p><h2>The&nbsp;CSS</h2><p>In order to get a handle on node insertion detection, we need to set up a series of keyframe animations which will start when the node is inserted.  The clip property is used since it has no effect on the node itself:</p><pre class="css">
/* set up the keyframes */
@keyframes nodeInserted {  
	from { clip: rect(1px, auto, auto, auto); }
	to { clip: rect(0px, auto, auto, auto); }  
}

@-moz-keyframes nodeInserted {  
	from { clip: rect(1px, auto, auto, auto); }
	to { clip: rect(0px, auto, auto, auto); }
}

@-webkit-keyframes nodeInserted {  
	from { clip: rect(1px, auto, auto, auto); }
	to { clip: rect(0px, auto, auto, auto); }
}

@-ms-keyframes nodeInserted {  
	from { clip: rect(1px, auto, auto, auto); }
	to { clip: rect(0px, auto, auto, auto); }
}

@-o-keyframes nodeInserted {  
	from { clip: rect(1px, auto, auto, auto); }
	to { clip: rect(0px, auto, auto, auto); }
}
</pre><p>With the keyframes created, the animation needs to be applied on the elements you&#8217;d like to listen for.  Note the tiny duration;  that relaxes the animation footprint on the browser.</p><pre class="css">
#parentElement > li {
    animation-duration: 0.001s;
    -o-animation-duration: 0.001s;
    -ms-animation-duration: 0.001s;
    -moz-animation-duration: 0.001s;
    -webkit-animation-duration: 0.001s;
    animation-name: nodeInserted;
    -o-animation-name: nodeInserted;
    -ms-animation-name: nodeInserted;        
    -moz-animation-name: nodeInserted;
    -webkit-animation-name: nodeInserted;
}
</pre><p>Add the animation to the child nodes you are listening for.  When the animation ends, the insertion event will fire!</p><h2>The&nbsp;JavaScript</h2><p>The first step is creating an function which will act as the event listener callback.  Within the function, an initial event.animationName check must be made to ensure it&#8217;s the animation name we want to listen for in this specific case:</p><pre class="js">
var insertListener = function(event){
	if (event.animationName == "nodeInserted") {
		// This is the debug for knowing our listener worked!
		// event.target is the new node!
		console.warn("Another node has been inserted! ", event, event.target);
	}
}
</pre><p>If the animation name matches the desired animation, we know a DOM node has been injected.  Now it&#8217;s time to add the event listener to the parent:</p><pre class="js">
document.addEventListener("animationstart", insertListener, false); // standard + firefox
document.addEventListener("MSAnimationStart", insertListener, false); // IE
document.addEventListener("webkitAnimationStart", insertListener, false); // Chrome + Safari
</pre><p>How awesomely simple is that?!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/detect-node-insertion.php" class="demo">View Demo</a><div
class="clear"></div></div><p>Daniel created this solution to aid in his forthcoming web components initiative, an initiative I&#8217;ll be covering more in depth very soon.  This node insertion hack is useful and uses no framework, so it&#8217;s an incredible mechanism that can be used by anyone.  Well done to Daniel!</p><p><a
href="http://davidwalsh.name/detect-node-insertion">Detect DOM Node Insertions with JavaScript and CSS&nbsp;Animations</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/detect-node-insertion/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>CSS Animations Between Media&#160;Queries</title><link>http://davidwalsh.name/animate-media-queries</link> <comments>http://davidwalsh.name/animate-media-queries#comments</comments> <pubDate>Tue, 10 Apr 2012 23:03:28 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Mobile]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5412</guid> <description><![CDATA[CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very CSS little code. Quite often we add CSS transforms to elements via CSS during :hover, and we also create keyframe-based animations by adding a className, [...]<p><a
href="http://davidwalsh.name/animate-media-queries">CSS Animations Between Media&nbsp;Queries</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>CSS animations are right up there with sliced bread.  CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very CSS little code.  Quite often we add CSS transforms to elements via CSS during :hover, and we also create keyframe-based animations by adding a className, but did you know you can animate elements using media queries as the trigger?  Let&#8217;s have a look!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/animate-media-queries.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;CSS</h2><p>The syntax for creating these animations and transitions is the same between media queries as it is between element states;  the only difference is actually enacting them between the media queries:</p><pre class="css">
/* base state */
#layout { 
	position: relative; 
	width: 900px; 
	border: 1px solid #ccc; 
	height: 200px; 
	
	/* animate width over a given duration */
	-webkit-transition: width 2s;
	-moz-transition: width 2s;
	-ms-transition: width 2s;
}
.child {
	top: 0; 
	bottom: 0;
	width: 290px;
	position: absolute;
	opacity: 1;
	font-size: 20px;
	overflow: hidden;
	-webkit-transform: translate3d(0, 0, 0);
	
	/* animate opacity, left, width over a given duration */
	-webkit-transition: opacity 2s, width 2s, left 2s, font-size 2s, color 2s;
	-moz-transition: opacity 2s, width 2s, left 2s, font-size 2s, color 2s;
	-ms-transition: opacity 2s, width 2s, left 2s, font-size 2s, color 2s;
}
	#child1 { left: 0; background: lightblue; }
	#child2 { left: 300px; background: lightgreen; }
	#child3 { left: 600px; background: lightyellow; }

/* 
	When the client has 860 width or less:
	 	- animate the first two elements to be wider
		- fade out and hide the third element
		- animate the background colors
		- animate the font-size of the block
*/
@media screen and (max-width: 860px) {
	#layout { width: 600px; }
	.child { width: 290px; font-size: 12px; }
	#child1 { left: 0; background: blue; color: #fff; }
	#child2 { left: 300px; background: green; color: #fff; }
	#child3.child { /* hider */ opacity: 0; width: 0; }
}
</pre><p>The creativity is all in the developer&#8217;s hands; animating media queries isn&#8217;t difficult, but the use of them is more interesting.  Some sites used to animate the position of structure elements during window resize, which is nice but how often is that a realistic usage?  A more realistic usage is with mobile devices, animating elements when orientation changes:</p><pre class="css">
/* base mobile styles */
#sidebar {
	-webkit-transition: opacity 2s;
	width: 300px;
	overflow: hidden;
}

/* portrait */
@media screen and (orientation:portrait) { 
	/* portrait-specific styles */ 
	#sidebar {
		opacity: 0;
		width: 0;
	}
}

/* landscape */
@media screen and (orientation:landscape) {
	/* landscape-specific styles */
	#sidebar {
		opacity: 1;
	}
}
</pre><p>The animation above triggers when the device goes from portrait to landscape, and visa versa.  This is incredibly useful when hiding a pane in portrait view and elegantly showing that pane when the user switches to landscape view.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/animate-media-queries.php" class="demo">View Demo</a><div
class="clear"></div></div><p>CSS animations between media queries have traditionally been a delicacy of web design, but there <em>are</em> practical uses.  The best part of them?  There is no JavaScript involved and you can force <a
href="http://davidwalsh.name/translate3d">CSS hardware acceleration</a> if you&#8217;d like.  Take a few moments to look at your mobile site to see if you can add this effect.</p><p><a
href="http://davidwalsh.name/animate-media-queries">CSS Animations Between Media&nbsp;Queries</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/animate-media-queries/feed</wfw:commentRss> <slash:comments>5</slash:comments> <series:name><![CDATA[CSS3 Animations]]></series:name> </item> <item><title>Multiple Backgrounds with&#160;CSS</title><link>http://davidwalsh.name/css-multiple-background</link> <comments>http://davidwalsh.name/css-multiple-background#comments</comments> <pubDate>Wed, 04 Apr 2012 01:43:05 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5411</guid> <description><![CDATA[Anyone that&#8217;s been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came along. Another one of those simple features is multiple background images with [...]<p><a
href="http://davidwalsh.name/css-multiple-background">Multiple Backgrounds with&nbsp;CSS</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Anyone that&#8217;s been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago.  One of those features is the <a
href="http://davidwalsh.name/html5-placeholder">HTML5 placeholder</a>;  we used JavaScript shims for a decade before placeholder came along.  Another one of those simple features is multiple background images with CSS.  Instead we&#8217;d need to nest another element for every additional background image.  Now we a syntax for supporting multiple background images on one element, and here&#8217;s what it looks like.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-multiple-background.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;CSS</h2><p>Multiple backgrounds involved using multiple property assignments with multiple values, separated by a comma:</p><pre class="css">
#multipleBGs {
	background: url(photo1.png),
				url(photo2.png),
				url(photo3.png)
	;
	background-repeat: no-repeat,
					   no-repeat,
					   repeat-y;
						
	background-position: 0 0,
						 30px 70px,
						 right top;
	
	width: 400px; 
	height: 400px;
	border: 1px solid #ccc;
}
</pre><p>Trying to stuff all properties via shorthand within the background property wont work, unfortunately; multiple property declarations must be used.  All of the background properties may be used (background-attachment , background-clip , background-image , background-origin , background-position , background-repeat , background-size), as well as CSS gradients.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/css-multiple-background.php" class="demo">View Demo</a><div
class="clear"></div></div><p>Another awesome CSS feature that we can finally used.  Using multiple CSS backgrounds is an incredible useful tool, preventing the need for nested elements for the sole purpose of formatting.</p><p><a
href="http://davidwalsh.name/css-multiple-background">Multiple Backgrounds with&nbsp;CSS</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/css-multiple-background/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>JavaScript CSS&#160;Helpers</title><link>http://davidwalsh.name/javascript-css</link> <comments>http://davidwalsh.name/javascript-css#comments</comments> <pubDate>Wed, 04 Apr 2012 00:12:44 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5410</guid> <description><![CDATA[I spend a good amount of time looking at JavaScript framework source code. Regardless of which frameworks you have allegiance to, you can learn an awful lot by looking under the hood of widely used code collections. One of many handy snippets can be found within the MooTools source code: functions to camelize and hyphenate [...]<p><a
href="http://davidwalsh.name/javascript-css">JavaScript CSS&nbsp;Helpers</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>I spend a good amount of time looking at JavaScript framework source code.  Regardless of which frameworks you have allegiance to, you can learn an awful lot by looking under the hood of widely used code collections.  One of many handy snippets can be found within the MooTools source code:  functions to camelize and hyphenate strings so that your own min framework can accept either form of CSS setter or getter.  Here are the functions in all of their glory.</p><h2>The&nbsp;JavaScript</h2><p>As you could probably guess, this task is best accomplished with regular expressions:</p><pre class="js">
function camelize(str) {
	return (str + "").replace(/-\D/g, function(match) {
		return match.charAt(1).toUpperCase();
	});
}
camelize("border-bottom-color"); // "borderBottomColor"


function hyphenate(str) {
	return (str + "").replace(/[A-Z]/g, function(match) {
		return "-" + match.toLowerCase();
	});
}
hyphenate("borderBottomColor"); // "border-bottom-color"
</pre><p>A couple of really handy JavaScript String to corresponding String format functions.  Instead of expecting strings in only one format, your mini library can now accept both!</p><p><a
href="http://davidwalsh.name/javascript-css">JavaScript CSS&nbsp;Helpers</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/javascript-css/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>View Browser Repaints in Google Chrome and Mozilla&#160;Firefox</title><link>http://davidwalsh.name/browser-repaint</link> <comments>http://davidwalsh.name/browser-repaint#comments</comments> <pubDate>Thu, 29 Mar 2012 04:21:03 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5406</guid> <description><![CDATA[One goal of super-optimized websites is to prevent browser repaints due to changes in a block&#8217;s style or content. There are numerous ways we intentionally (or unintentionally) trigger block repaints, but the browser does it so quickly we have trouble seeing when and where it happens. Recognizing the importance of allowing developers to micro-optimize their [...]<p><a
href="http://davidwalsh.name/browser-repaint">View Browser Repaints in Google Chrome and Mozilla&nbsp;Firefox</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One goal of super-optimized websites is to prevent browser repaints due to changes in a block&#8217;s style or content.  There are numerous ways we intentionally (or unintentionally) trigger block repaints, but the browser does it so quickly we have trouble seeing when and where it happens.  Recognizing the importance of allowing developers to micro-optimize their pages, the Chrome and Firefox teams have added features to their browsers to allow us to see those repaints.  Here&#8217;s how to do it!</p><h2>Google Chrome &amp;&nbsp;Canary</h2><p>The repaint feature needs to be enabled at startup for Chrome and Canary with the <code>--show-paint-rects</code> config:</p><pre class="shell">
open ./Google\ Chrome\ Canary.app --args --show-paint-rects
</pre><p>Be sure to include <code>--args</code> in your command or you&#8217;ll run into problems opening the browser.  Repaints will display on each site you visit.</p><h2>Mozilla Firefox, Aurora,&nbsp;Nightly</h2><p>To view repaints in Firefox and Aurora browsers, you need to enable an about:config option.  The option name is <code>nglayout.debug.paint_flashing</code>. Turn this option to true and you&#8217;ll quickly see repaints as the happen!</p><p><em>At the time of publish, only nightly exposed this config feature.</em></p><p>I recommend you take a few moments to scope out the repaint frequency on your websites and see what you can do to improve performance!</p><p><a
href="http://davidwalsh.name/browser-repaint">View Browser Repaints in Google Chrome and Mozilla&nbsp;Firefox</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/browser-repaint/feed</wfw:commentRss> <slash:comments>2</slash:comments> <enclosure
url="http://davidwalsh.name/dw-content/ChromeBrowserRepaint.mp4" length="0" type="video/mp4" /> <enclosure
url="http://davidwalsh.name/dw-content/MozillaNightlyRepaint.mp4" length="0" type="video/mp4" /> </item> <item><title>Load CSS Files via AMD with&#160;XStyle</title><link>http://davidwalsh.name/amd-xstyle</link> <comments>http://davidwalsh.name/amd-xstyle#comments</comments> <pubDate>Tue, 27 Mar 2012 15:54:41 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5408</guid> <description><![CDATA[AMD loaders are letting us load just about anything: AMD modules, basic JavaScript files (from any origin), text-based files (HTML templates, for example), and more. Unfortunately most loaders don&#8217;t have CSS loading capabilities, most likely because &#8220;onLoad&#8221;-style events aren&#8217;t provided by all browsers for stylesheets. Luckily my SitePen colleague Kris Zyp has created XStyle, an [...]<p><a
href="http://davidwalsh.name/amd-xstyle">Load CSS Files via AMD with&nbsp;XStyle</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>AMD loaders are letting us load just about anything: AMD modules, basic JavaScript files (from any origin), text-based files (HTML templates, for example), and more.  Unfortunately most loaders don&#8217;t have CSS loading capabilities, most likely because &#8220;onLoad&#8221;-style events aren&#8217;t provided by all browsers for stylesheets.  Luckily my SitePen colleague Kris Zyp has created XStyle, an AMD package available to AMD loaders for reliable stylesheet loading.  Let&#8217;s take a brief look at XStyle!</p><div
class="actions"><a
href="https://github.com/kriszyp/xstyle" rel="nofollow" class="demo">View Demo</a><div
class="clear"></div></div><p>In fairness to XStyle, it&#8217;s more than just an AMD plugin for loading stylesheets.  XStyle provides the capability to:</p><ul><li>Shim and extend CSS</li><li>Load stylesheets and execute callbacks</li><li>Nested @import loading</li></ul><p>Shimming and extending CSS is great but doesn&#8217;t seem to be something I would need often;  loading CSS with JavaScript modules is nice because:</p><ol><li>Loading modules and templates together but needing to add LINK tags manually sucks</li><li>One define() to define a <em>complete</em> widget from JS, to template, and CSS, is ideal;  especially for third party components</li></ol><p>So consider a great JavaScript loader like <a
href="https://github.com/cujojs/curl" rel="nofollow">curl.js</a>.  With curl.js, all you need to do to load a CSS file is:</p><pre class="js">
curl(["css!path/to/file.css"], function() {
    // defineCSS loaded, do stuff!
});
</pre><p>Sweet, right?  With a different loader, you can load your CSS files with other modules by coding:</p><pre class="js">
define(["xstyle!./path/to/file.css"], function(){
    // module starts after css is loaded
});
</pre><p>Outstanding!  With XStyle we can define a <em>complete</em> component, stylesheet and all!</p><div
class="actions"><a
href="https://github.com/kriszyp/xstyle" rel="nofollow" class="demo">View Demo</a><div
class="clear"></div></div><p>XStyle is capable of much more than what I&#8217;ve presented above, but just the ability to load stylesheets with every other piece of a given module is priceless.  Improves organization and speed of coding;  well done Kris!</p><p>&nbsp;</p><p><a
href="http://davidwalsh.name/amd-xstyle">Load CSS Files via AMD with&nbsp;XStyle</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/amd-xstyle/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Google Extension Effect with CSS or jQuery or MooTools&#160;JavaScript</title><link>http://davidwalsh.name/google-extension-effect</link> <comments>http://davidwalsh.name/google-extension-effect#comments</comments> <pubDate>Mon, 19 Mar 2012 13:46:55 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5404</guid> <description><![CDATA[Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane and a detail pane. I&#8217;ve taken a few moments to duplicate [...]<p><a
href="http://davidwalsh.name/google-extension-effect">Google Extension Effect with CSS or jQuery or MooTools&nbsp;JavaScript</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page.  One of the extensions used by Google is a basic margin-top animation to switch between two panes:  a graphic pane and a detail pane.  I&#8217;ve taken a few moments to duplicate that effect with just CSS, and another enhanced version with jQuery and MooTools.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/google-extension-effect.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The Base HTML and&nbsp;CSS</h2><p>I&#8217;m using minimal elements within this code since, theoretically, there would be many of these on a given page and large DOM trees can drastically effect page performance:</p><pre class="html">
&lt;div class="item"&gt;
	&lt;a href="/angry-birds"&gt;
		&lt;div class="item-billboard"&gt;
			&lt;img src="angrybirds.jpg" /&gt;
			&lt;h3&gt;Angry Birds&lt;/h3&gt;
		&lt;/div&gt;
		&lt;div class="item-detail"&gt;
			&lt;p&gt;There's more detail about the item inside this DIV!&lt;/p&gt;
		&lt;/div&gt;
	&lt;/a&gt;
&lt;/div&gt;
</pre><p>Remember that HTML5 says we can wrap block-level elements with A tags, so don&#8217;t hate on me!</p><p>There&#8217;s a fair amount of base CSS we need too:</p><pre class="css">
.item {
	position: relative;
	width: 240px;
	overflow: hidden;
	border: 1px solid #ccc;
}
	.item {
		height: 200px;
	}
	
	.item a {
		text-decoration: none;
		color: #000;
	}
	
	.item-billboard, .item-detail {
		padding: 10px;
		height: 180px;
	}
	
	.item-billboard {
		margin-top: 0;
		background: #fff;
	}
		.item-billboard h3 {
			font-size: 13px;
			font-weight: bold;
			color: #262626;
			font-family: "Open Sans", arial, sans-serif;
		}
	
	.item-detail {
		background: #ececec;
	}
</pre><p>The only restriction with the CSS above is that we&#8217;re explicitly setting a lesser height for the billboard and detail blocks because we&#8217;re adding padding to them.  We <em>could</em> use the full 200px height of the parent for each of those blocks, and wrap the content for each in an additional DIV, allowing for animating <code>margin-top</code> to 100%, but that would add 2 extra nodes per block.</p><h2>The CSS-Only&nbsp;Method</h2><p>Much like my <a
href="http://davidwalsh.name/photo-stack">Google Photo Stack post</a>, the CSS-only version gets us 90% there.  Browser-powered CSS animations give us the ability to animate to the second pane&#8230;</p><pre class="css">
.item-billboard {
	margin-top: 0;
	background: #fff;
	
	/* add animations! */
	-webkit-transition-property: margin-top;
	-webkit-transition-duration: .5s;
	
	-moz-transition-property: margin-top;
	-moz-transition-duration: .5s;
	
	-o-transition-property: margin-top;
	-o-transition-duration: .5s;
	
	-ms-transition-property: margin-top;
	-ms-transition-duration: .5s;
}

/* animate on hover */
.itemCss:hover .item-billboard {
	margin-top: -200px;
}
</pre><p>Since the CSS transition settings are on the .billboard elements, the margin will animate both on hover and then back to its original state during mouseleave.  Unfortunately you&#8217;re out of luck if the client doesn&#8217;t support CSS transitions.  That&#8217;s where we can use MooTools or jQuery to make it happen!</p><h2>The jQuery and MooTools&nbsp;JavaScript</h2><p>Overriding the CSS animation with a bit of JavaScript will allow the same animation in older browsers.  We can jQuery or MooTools to make that happen:</p><pre class="javascript">
// MooTools
window.addEvent("domready", function() {
	$$(".itemJs").addEvents({
		mouseenter: function() {
			var billboard = this.retrieve("billboardElement");
			if(!billboard) {
				billboard = this.getElements(".item-billboard")[0];
				this.store("billboardElement", billboard);
			}
			billboard.tween("margin-top", "-200px");
		},
		mouseleave: function() {
			this.retrieve("billboardElement").tween("margin-top", 0);
		}
	});
});

// jQuery
jQuery(document).ready(function() {
	jQuery(".itemJQuery").bind({
		mouseenter: function() {
			var self = jQuery(this), billboard = self.data("billboardElement");
			if(!billboard) {
				billboard = jQuery(jQuery(".item-billboard", this)[0]);
				self.data("billboardElement", billboard);
			}
			jQuery(billboard).stop().animate({
				"margin-top": "-200px"
			});
		},
		mouseleave: function() {
			jQuery(this).data("billboardElement").stop().animate({
				"margin-top": 0
			});
		}
	});
});
</pre><p>In each case, we lazy-find billboard elements when they&#8217;re hovered over and animate them just as the CSS does.  Simple!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/google-extension-effect.php" class="demo">View Demo</a><div
class="clear"></div></div><p>I love the design of these blocks by Google.  They provide a beautiful default view and allow for more detail when the user shows interest.  I can see this strategy and effect being useful in many situations, as behind it is brilliant.  A business that sells records could use the same sort of effect, for example.  Well done to Google!</p><p><a
href="http://davidwalsh.name/google-extension-effect">Google Extension Effect with CSS or jQuery or MooTools&nbsp;JavaScript</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/google-extension-effect/feed</wfw:commentRss> <slash:comments>7</slash:comments> <series:name><![CDATA[CSS3 Animations]]></series:name> </item> <item><title>Force Hardware Acceleration in WebKit with&#160;translate3d</title><link>http://davidwalsh.name/translate3d</link> <comments>http://davidwalsh.name/translate3d#comments</comments> <pubDate>Mon, 12 Mar 2012 06:27:41 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Mobile]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5402</guid> <description><![CDATA[Ever notice an odd flicker within WebKit-powered desktop and mobile browsers, or simply want to use hardware acceleration of a given device? There&#8217;s a really neat trick you can use to force hardware acceleration! The WebKit&#160;CSS /* warp speed ahead */ .animClass { -webkit-transform: translate3d(0, 0, 0); /* more specific animation properties here */ } [...]<p><a
href="http://davidwalsh.name/translate3d">Force Hardware Acceleration in WebKit with&nbsp;translate3d</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Ever notice an odd flicker within WebKit-powered desktop and mobile browsers, or simply want to use hardware acceleration of a given device?  There&#8217;s a really neat trick you can use to force hardware acceleration!</p><h2>The WebKit&nbsp;CSS</h2><pre class="css">
/* warp speed ahead */
.animClass {
	-webkit-transform: translate3d(0, 0, 0);
	/* more specific animation properties here */
}
</pre><p>The use of <code>translate3d</code> pushes CSS animations into hardware acceleration.  Even if you&#8217;re looking to do a basic 2d translation, use <code>translate3d</code> for more power!  If your animation is still flickering after switching to the transform above, you can use a few little-known CSS properties to try to fix the problem:<pre class="css">
.animClass {
	-webkit-backface-visibility: hidden;
	-webkit-perspective: 1000;
}
</pre><p>There you have it;  more power, less flicker.  Happy animating!</p><p><a
href="http://davidwalsh.name/translate3d">Force Hardware Acceleration in WebKit with&nbsp;translate3d</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/translate3d/feed</wfw:commentRss> <slash:comments>4</slash:comments> <series:name><![CDATA[CSS3 Animations]]></series:name> </item> <item><title>Use Elements as Background Images with&#160;-moz-element</title><link>http://davidwalsh.name/moz-element</link> <comments>http://davidwalsh.name/moz-element#comments</comments> <pubDate>Thu, 16 Feb 2012 15:31:57 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Browsers]]></category> <category><![CDATA[CSS]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5388</guid> <description><![CDATA[We all know that each browser vendor takes the liberty of implementing their own CSS and JavaScript features, and I&#8217;m thankful for that. Mozilla and WebKit have come out with some interesting proprietary CSS properties, and since we all know that cementing standards takes ages more than it should, we should all be thankful. One [...]<p><a
href="http://davidwalsh.name/moz-element">Use Elements as Background Images with&nbsp;-moz-element</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>We all know that each browser vendor takes the liberty of implementing their own CSS and JavaScript features, and I&#8217;m thankful for that. <a
href="http://davidwalsh.name/moz-css">Mozilla</a> and <a
href="http://davidwalsh.name/webkit-css">WebKit</a> have come out with some interesting proprietary CSS properties, and since we all know that cementing standards takes ages more than it should, we should all be thankful.  One interesting CSS property you probably haven&#8217;t heard of is <code>-moz-element</code>, a Mozilla-implemented CSS property that allows developers to use HTML elements as an element background!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/moz-element.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The HTML and&nbsp;CSS</h2><p>Let&#8217;s assume that an HTML element exists within the current page, and it&#8217;s style however a background should be.  It has a CSS gradient, text, and numerous CSS properties:</p><pre class="html">
&lt;div id="mozElementBack" style="border:1px solid #999;width: 200px; height: 100px; color: #fff; background: -moz-linear-gradient(top, #063053, #395873, #5c7c99);"&gt;
	I'm in the background.  
&lt;/div&gt;
</pre><p>I&#8217;ve inlined the styles on the element, but <code>-moz-element</code> will work with styles declared in style tags or external stylesheets.  With the element existing within our page, we can now use it as a &#8220;background&#8221; for another element:</p><pre class="css">
#mySpecialElement {
	/* mozElementBack exists as an element within the page */
	-moz-element(#mozElementBack) repeat;
}
</pre><p>Simply setting the <code>-moz-element</code> to the element ID, theoretically, converts the given element to a background image, allowing for background-repeat capabilities, and so on.  Also note that the background updates as the element content and style updates, so you&#8217;re working with a &#8220;live&#8221; background!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/moz-element.php" class="demo">View Demo</a><div
class="clear"></div></div><p>An incredible CSS property, no?  The ability to use an existing HTML element as a CSS background is wild, but thanks to Mozilla, it&#8217;s entirely possible.  Let me know if you can think of a great, real-life usage of this property.  The advantage I see in <code>-moz-element</code> is that you can include text in backgrounds, as well as elements generated from scripts you don&#8217;t control (social bookmarking JS files, for example).  What an interesting implementation though!</p><p><a
href="http://davidwalsh.name/moz-element">Use Elements as Background Images with&nbsp;-moz-element</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/moz-element/feed</wfw:commentRss> <slash:comments>9</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/60 queries in 0.122 seconds using disk: basic
Object Caching 1436/1528 objects using disk: basic

Served from: davidwalsh.name @ 2012-05-23 22:23:41 -->
