<?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/"
><channel><title>CSSKarma &#187; javascript</title> <atom:link href="http://www.csskarma.com/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" /><link>http://www.csskarma.com/blog</link> <description>display your style</description> <lastBuildDate>Tue, 31 Jan 2012 15:18:44 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Changing History with HTML 5</title><link>http://www.csskarma.com/blog/changing-history-with-html-5/</link> <comments>http://www.csskarma.com/blog/changing-history-with-html-5/#comments</comments> <pubDate>Fri, 19 Aug 2011 13:49:30 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Design]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[api]]></category> <category><![CDATA[history]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[javascript]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=953</guid> <description><![CDATA[Here&#8217;s the demo for those who like to jump right in. The URL is an important piece of user experience in any Web site or application. We like using, what we call &#8220;talking URLs&#8221; so they&#8217;re easy to say and remember, like clearleft.com/is/richardrutter (one of the best all time). But as we build complex ajax [...]]]></description> <content:encoded><![CDATA[<p><strong><a
href="http://www.csskarma.com/lab/html5/history">Here&#8217;s the demo</a></strong> for those who like to jump right in.</p><p>The URL is an important piece of user experience in any Web site or application. We like using, what we call &#8220;talking URLs&#8221; so they&#8217;re easy to say and remember, like <a
href="http://clearleft.com/is/richardrutter/">clearleft.com/is/richardrutter</a> (one of the best all time).</p><p>But as we build complex ajax and javascript-base apps with wild interfaces, we sometimes forget about the URL and just how important it is.</p><p>The HTML 5 History API aims to deal with that problem by letting you access the address bar directly and create <em>pushes</em> to change the URL and inject (same domain) history into the browser.</p><h3>Feature detection</h3><p>Step 1 to using any of the new HTML 5 goodies is testing for support. The History API works where you would expect it to (Gecko &#038; Webkit, not sure about IE9). Testing for support is pretty basic:</p><h4>Regular JS</h4><pre><code>if (typeof history.pushState !== "undefined") {
    <span>//history api is supported</span>
}</code></pre><h4>With modernizr</h4><pre><code>if (Modernizr.history) {
     <span>// history api is supported</span>
}</code></pre><h3>The History Push</h3><pre><code>if (typeof history.pushState !== "undefined") {
     history.pushState([state to track], '[page title]', '[pushed URL]');
}</code></pre><h4>The pieces</h4><p><strong>State</strong>: This data will be passed into the &#8220;popstate&#8221;, which deal with things like when the user click the back button.</p><p><strong>Page title</strong>: This is the title that you see when you click and hold the back button (the menu that pops up) &#8211; not totally supported yet</p><p><strong>Pushed URL</strong>: this is the url you want to display in the address bar. It&#8217;s only limitation is that is has to be in the same domain (security).</p><h4>Worked out history push</h4><pre><code>if (typeof history.pushState !== "undefined") {
     history.pushState(null, 'I love dogs', 'dogsrock.html');
}</code></pre><p><strong><a
href="http://www.csskarma.com/lab/html5/history">In this demo</a></strong>, you can see the URL changing but the page not reloading.</p><p>This is basically a slimmed down version of the <a
href="http://diveintohtml5.org/history.html">Dive in HTML5</a> documentation with some modifications to the history code, I slimmed down the detection a bit.</p><p>As usual, any comments, questions, criticisms are welcome. I left the back button broken so you can see it actually working.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/changing-history-with-html-5/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>HTML 5 Form Validation with Yepnope Fallback</title><link>http://www.csskarma.com/blog/form-validate-yepnope-fallback/</link> <comments>http://www.csskarma.com/blog/form-validate-yepnope-fallback/#comments</comments> <pubDate>Thu, 04 Aug 2011 13:39:03 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[forms]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[performance]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=940</guid> <description><![CDATA[In my last post about creating a proper fallback when using the new HTML 5 form validation I mentioned using modernizr to detect for support, then creating an if statement to call the jQuery validate plugin. One of the commentors mentioned using yepnope for this instead of loading the jQuery validation plugin by default. Since [...]]]></description> <content:encoded><![CDATA[<p>In my <a
href="http://www.csskarma.com/blog/required-input-fallback/" title="Required input fields with JS fallback">last post</a> about creating a proper fallback when using the new HTML 5 form validation I mentioned using modernizr to detect for support, then creating an if statement to call the jQuery validate plugin.</p><p>One of the commentors mentioned using yepnope for this instead of loading the jQuery validation plugin by default. Since modernizr has yepnope now anyway, I thought it was a good idea. And here we are with some ne code to look at.</p><p><a
href="http://www.csskarma.com/lab/_javascript/no_require/">View the demo</a></p><h3>Dependencies</h3><ul><li><a
href="http://www.jquery.com">jQuery</a></li><li><a
href="http://www.modernizr.com/">Modernizr (with yepnope)</a></li><li><a
href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validate plugin</a></li></ul><h3>The HTML</h3><pre><code>&lt;form action="" method="post"&gt;

    &lt;ul&gt;
        &lt;li&gt;
            &lt;label for="name"&gt;Name&lt;/label&gt;
            &lt;input type="text" name="name" id="name" required&gt;
        &lt;/li&gt;
        
        &lt;li&gt;
            &lt;label for="email"&gt;Email&lt;/label&gt;
            &lt;input type="email" name="email" id="email" required&gt;
        &lt;/li&gt;
    
        &lt;li&gt;
            &lt;button type="submit"&gt;Submit&lt;/button&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    
&lt;/form&gt;</code></pre><h3>The JS</h3><pre><code>(function ($) {

	<span>// set and cache some variables, change var forms to whatever forms you want to validate</span>
	var forms = $('form'),
		formsCount = forms.length,
		items = forms.find('input[required]'),
		count = items.length;

	<span>// check for "required" input support with modernizr</span>
	if (Modernizr.input.required) {

		<span>// do something else, maybe customize the output messages?</span>

	} else {

		<span>// loop though each required input and set the required and type class jQuery validate needs</span>
		for (var i = 0; i < count; i += 1) {
			var obj = items[i];
			$(obj).attr('class', 'required ' + obj.getAttribute('type')).removeAttr('required');
		};

		<span>// after the classes are set, load in the plugin with yepnope , you can do this with Modernizr.load as well</span>
		yepnope([{
			load: 'js/validate.js',
			complete: function () {

				<span>// after the plugin is loaded, call the method on each form listed at the top</span>
				for (var i = 0; i < formsCount; i += 1) {
					$(forms[i]).validate();
				}
			}
		}]);

	} <span>// if required supported</span>

}(jQuery));
</code></pre><p>I&#8217;m not really a fan of script loaders, especially when using them to load a core script file, like jQuery, but I think this is actually a pretty good use of yepnope.</p><p>I don&#8217;t know about you folk(s), but I&#8217;ll be using this moving forward. Any comments/corrections are obviously welcome.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/form-validate-yepnope-fallback/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>CSS Performance and OOCSS</title><link>http://www.csskarma.com/blog/css-performance-and-oocss/</link> <comments>http://www.csskarma.com/blog/css-performance-and-oocss/#comments</comments> <pubDate>Mon, 18 Apr 2011 13:49:11 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Design]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[performance]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=896</guid> <description><![CDATA[I&#8217;m in the process right now of signing on for my first speaking gig in since I moved to Boston (thanks to @joedevon) and it got me thinking about performance in CSS; a topic I really haven&#8217;t address in years since I started using my single-line CSS formatting style. Tip: use spaces rather than tabbing [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;m in the process right now of signing on for my first speaking gig in since I moved to Boston (thanks to <a
href="http://twitter.com/joedevon" rel="friend colleague">@joedevon</a>) and it got me thinking about performance in CSS; a topic I really haven&#8217;t address in years since I started using my <a
href="http://csskarma.com/css/csskarma.css" rel="me">single-line CSS formatting style</a>. <strong>Tip:</strong> use spaces rather than tabbing to maintain the alignment across different software since they all treat the &#8220;tab&#8221; differently.</p><p>But I digress.</p><h3>CSS Performance</h3><p>We&#8217;ve all seen the mass of articles &amp; tutorials about <a
href="http://www.youtube.com/watch?v=mHtdZgou0qU">JavaScript performance</a>, how to <a
href="http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/">optimize jQuery</a>, and the <a
href="http://jsperf.com/browse">JS Performance test suite</a>. The market is flooded, ok, we get it. JavaScript is a beast of the Web that needs to be constantly tamed. It&#8217;s getting better, especially with lightning-fast JS engine , <a
href="http://code.google.com/p/v8/">V8</a> looming around in Chome and NodeJS.</p><p>CSS performance is constantly overshadowed by JS performance (and rightly so), but that doesn&#8217;t mean we shouldn&#8217;t at least take a look at it every now-and-then as a form of checks and balances for our front-end code. After all, something like <a
href="http://developer.yahoo.com/performance/rules.html">90% of the load time for a site happens on the front-end</a> and unless you&#8217;re Google, it&#8217;s probably not <em>all</em> JavaScript (little bit of a big there, sorry fellas).</p><h3>OOCSS</h3><p>Object Oriented CSS goes a little like this:</p><h4>The CSS</h4><pre><code>.rounded{ border-radius:7px; }
.box-shadow{ -moz-box-shadow:0 2px 2px #333;box-shadow:0 2px 0 #333; }
.left{ float:left; }</code></pre><h4>The HTML</h4><pre><code>&lt;div class=&quot;rounded box-shadow left&quot;&gt;&lt;/div&gt; &lt;!--/.rounded .box-shadow .left--&gt;</code></pre><p>You&#8217;re basically just separating out the individual properties you&#8217;ll be using and modifying the HTML to reflect them. It&#8217;s very similar to a framework model in building and styling a site.</p><p>Whether you like it or not, there it is. There&#8217;s a lot of debate over this method, pluses and minuses, loves and hates. The most popular argument against it is that you&#8217;re embedding style information into your HTML and semi-collapses the progressive enhancement model of structure &mdash; presentation &mdash; behavior.</p><p>It&#8217;s tough for me to totally buy into OOCSS for that reason. I stick very closely to progressive enhancement while I&#8217;m building a site and really only stray from that when I&#8217;m styling for a CMS and I need classes like: &#8220;left&#8221;, &#8220;right&#8221;, &#8220;clear&#8221;, etc., which get applied by the user later on.</p><p>So, when styling for use in a CMS, I really do think OOCSS is dead-on. I&#8217;ve yet to fully use it just because I question it&#8217;s maintainability. But from a strictly performance stand point: <strong>Is OOCSS better?</strong> Yea, it&#8217;s better. <a
href="http://www.shauninman.com/archive/2008/05/05/css_qualified_selectors#comment_3940">It does perform better across the board</a> than almost all of the CSS selectors (especially the advanced ones).</p><h3>Lastly, on performance</h3><p>We can all go through ySlow and JSLint to see where we may have some front-end memory leaks but getting down to the granularity of the way we&#8217;re constructing a page (OOCSS vs. non-OOCSS) is tough without running the explicit performance tests.</p><p>As the Web gets more and more advanced and browsers like Chrome pave the way for faster sites, I have no idea what the real answer for CSS performance is, but I do know that <strong>browsers will not help you write better code</strong>. We still need to explore the best practices, be it front-end, back-end or somewhere in the middle.</p><p>I&#8217;d like to get into this in more detail, but I have a group of tourists screaming at eachother in French next to me (trying out a new coffee shop), so I defer to the comments if you&#8217;re at all interesting in the OOCSS debate.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/css-performance-and-oocss/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Quick Tip on Global Scope and jQuery</title><link>http://www.csskarma.com/blog/global-scope-jquery/</link> <comments>http://www.csskarma.com/blog/global-scope-jquery/#comments</comments> <pubDate>Fri, 08 Apr 2011 12:34:29 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Design]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=890</guid> <description><![CDATA[Before launch of a new site, I always take the time to run my JavaScript through a checker. Personally, I use JS Lint. It&#8217;s not the prettiest thing, but it works great to pick up small errors and elements of bad practice. You&#8217;ll get alerts like &#8220;unused variable&#8221;, &#8220;don&#8217;t use eval(), it&#8217;s evil&#8221;, &#8220;implied global&#8221;. [...]]]></description> <content:encoded><![CDATA[<p>Before launch of a new site, I always take the time to run my JavaScript through a checker. Personally, I use <a
href="http://jslint.com/">JS Lint</a>. It&#8217;s not the prettiest thing, but it works great to pick up small errors and elements of bad practice. You&#8217;ll get alerts like &#8220;unused variable&#8221;, &#8220;don&#8217;t use eval(), it&#8217;s evil&#8221;, &#8220;implied global&#8221;.</p><p>I find that it&#8217;s best not to argue with JS Lint, just fix the problem.</p><p>One that I had encountered quite a bit while running jQuery through JS Lint was &#8220;Implied global: $&#8221;.</p><p>Up until a week or two ago, I was just blowing it off, because, I mean, come on, it&#8217;s a $, it kind of is global when using jQuery. For whatever reason I had a quick talk with <a
href="http://twitter.com/akira_x">@akira_x</a> and we figured out what was going on and how to fix it.</p><p>The solution is actually similar to how we build jQuery plugins, but without the real plugin model, just normal site code.</p><h3>Evolution of the the document.ready</h3><p>When jQuery first came out, this is how we would used document.ready as a wrapper for custom jQuery:</p><pre><code>$(document).ready(function() {
// jquery code here
 });</code></pre><p>Then we moved on to the short cut method:</p><pre><code>$(function(){
// jquery code here
});</code></pre><p>Both of these methods are technically fine, but they both leave &#8220;$&#8221; as an implied global. This may not be an issue for most of us since we only use jQuery, so who really cares is $ is global, right?</p><p>We can, however make sure (just in case, because we&#8217;re all good coders) that there are no future library or JavaScript collisions by containing the scope of $ with:</p><pre><code>(function($) {
	// jquery here
}(jQuery));</code></pre><p>This will still cause JS Lint to throw an &#8220;implied global&#8221; alert, but it will be for &#8220;jQuery&#8221; rather than &#8220;$&#8221;. Personally, I feel like it&#8217;s better.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/global-scope-jquery/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Redirection Options</title><link>http://www.csskarma.com/blog/redirection-options/</link> <comments>http://www.csskarma.com/blog/redirection-options/#comments</comments> <pubDate>Wed, 27 Jan 2010 15:40:58 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[beginner]]></category> <category><![CDATA[htaccess]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[meta]]></category> <category><![CDATA[php]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=736</guid> <description><![CDATA[Lately, I&#8217;ve been having to work with a lot of redirects and I was going through different redirection options with a client. So I thought I would do a quick write up about the different types of redirection. I also haven&#8217;t seen a collection of these in one place, so I thought I would put [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/blog/wp-content/uploads/banner_redirection.png" alt="article banner"></p><p>Lately, I&#8217;ve been having to work with a lot of redirects and I was going through different redirection options with a client. So I thought I would do a quick write up about the <strong>different types of redirection</strong>.</p><p>I also haven&#8217;t seen a collection of these in one place, so I thought I would put this together.</p><p>There are a few different types that are commonly use when redirecting users:</p><ul><li>.htaccess / apache</li><li>php header</li><li>javascript</li><li>meta refresh</li></ul><p>The top two are the one&#8217;s you&#8217;ll be using most often, I don&#8217;t really recommend the last two. But they <em>are</em> available, so I think they&#8217;re worth noting. So here they are in the order I would recommend:</p><h3>.htaccess / apache redirects</h3><pre><code>Redirect /old_directory http://example.com/newdirectory</code></pre><p>This redirection is most efficient at the apache level, but if you&#8217;re like me and are on shared hosting, getting into the apache installation probably isn&#8217;t an option. In that case you can put this in an <a
href="http://www.csskarma.com/blog/creating-an-htaccess-template/">.htaccess file</a> in the root directory of your site.</p><p>Using an .htaccess redirection is great for moving entire sites because you can not only redirect a page, but you can redirect entire sites and subdirectories with one line of code.</p><h3>PHP header redirects</h3><pre><code>&lt;?php header('Location: http://example.com/newdirectory/'); ?&gt;</code></pre><p>PHP header redirects are good for single page redirects and if you&#8217;re not comfortable working with the .htaccess file. This code goes at the very top of the page you&#8217;re trying to redirect. If it&#8217;s not there you may get a &#8220;headers already sent&#8221; error.</p><p>If you&#8217;re publishing out static HTML files, you can still use this by adding this code into your .htaccess file:</p><pre><code>AddType application/x-httpd-php .php .html .htm</code></pre><p>This will let you <strong>execute PHP in an HTML file</strong>.</p><h3>JavaScript redirects</h3><pre><code>&lt;script type="text/javascript"&gt;
&lt;!--
window.location = "http://example.com/newdirectory"
//--&gt;
&lt;/script&gt;</code></pre><p>JavaScript redirects are generally not used unless you&#8217;ve exhausted the other two options. If you don&#8217;t have access to the .htaccess file AND you can&#8217;t run server-side script on your page, this is an OK fallback option, but you have to make sure to provide a link to the forwarding page incase a user is browsing without JavaScript.</p><h3>Meta redirects</h3><pre><code>&lt;meta http-equiv="refresh" content="0;URL=http://example.com/newdirectory"&gt;</code></pre><p>As your last option, I don&#8217;t see this in the wild anymore, but it&#8217;s worth mentioning that it does exist and does technically work (I&#8217;m not sure about it&#8217;s validity in HTML 5). You would place this in the document HEAD with the rest of the meta elements.</p><p>content=0 is a second timer for the redirect, I have this one set to 0 so it will redirect immediately.</p><p><strong>Those are the options I&#8217;ve used in the past; did I miss anything? Maybe an ASP redirect? Let me know!</strong></p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/redirection-options/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Conditional Animation Speed in jQuery</title><link>http://www.csskarma.com/blog/conditional-animation-speed/</link> <comments>http://www.csskarma.com/blog/conditional-animation-speed/#comments</comments> <pubDate>Fri, 08 Jan 2010 17:00:43 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Design]]></category> <category><![CDATA[advanced]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[ui]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=694</guid> <description><![CDATA[For years (and by &#8220;years&#8221;, I most likely mean &#8220;the years since I started using jQuery&#8230; maybe 2&#8243;), there&#8217;s been one aspect of jQuery that&#8217;s bugged the crap out of me, the animation speed. View demo We&#8217;ve all probably seen the problem that happens in many drop down menus of varying height. We set slideDown(300) [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/blog/wp-content/uploads/turtle.jpg" alt="Turtle"></p><p>For years (and by &#8220;years&#8221;, I most likely mean &#8220;the years since I started using jQuery&#8230; maybe 2&#8243;), there&#8217;s been one aspect of jQuery that&#8217;s bugged the crap out of me, the animation speed.</p><p
class="demo"><a
href="http://www.csskarma.com/lab/animation_speed/">View demo</a></p><p>We&#8217;ve all probably seen the problem that happens in many drop down menus of varying height. We set <code>slideDown(300)</code> (or whatever you set it to) on all the submenus and leave like that. Inevitably, menus grow and shrink based on the content and we get this weird and mildly annoying behavior of really fast moving tall menus vs. shorter menus that move painfully slow just because there&#8217;s not much content in there.</p><p>This problem stems from where we set the speed of the animation; the value isn&#8217;t actually a speed, it&#8217;s how long it takes (in milliseconds, I think) for the animation to finish. So you can see why you get varying behaviors based on the element height.</p><p>It&#8217;s true, that you can use the built-in speed values like &#8220;fast&#8221; and &#8220;slow&#8221;, but if you want more fine-grained control over your animation, you&#8217;ll have to use a value.</p><h3>Problem Child <a
href="http://us.imdb.com/title/tt0100419/">&#8734;</a></h3><p>This is what our problem menu looks like all collapsed, just 2 clickable headings (<code>h2</code>) that will slide open to expose content (<code>p</code>)</p><p><img
src="http://www.csskarma.com/blog/wp-content/uploads/animation_collapsed.png" alt="animation collapsed"></p><p>Normally our jQuery would look something like this:</p><pre><code>$(function(){

$('h2').toggle(function(){
    <span>/* get the next element */</span>
    var next_element = $(this).next();

    <span>/* open it  if it's closed */</span>
    next_element.slideDown(500);
},function(){
    <span>/* get the next element again */</span>
    var next_element = $(this).next();

    <span>/* close it if it's open */</span>
    next_element.slideUp(500);
});

});</code></pre><p>Note: just example code, I didn&#8217;t check to see if it actually works</p><p>This is our menu fully expanded. As you can see, the first item has a lot more content then the second. Using the code above will make menu #1 move noticeably faster than menu #2; which, to me, creates an odd user experience. Because you&#8217;re like &#8220;Why&#8217;s is moving so slow?&#8221;</p><p><img
src="http://www.csskarma.com/blog/wp-content/uploads/animation_expanded.png" alt="animation expanded" /></p><p>So let&#8217;s see if we can&#8217;t fix that.</p><h3>Ben Healy (Solution) <a
href="http://us.imdb.com/name/nm0000615/">&#8734;</a></h3><p>What we&#8217;re looking to do here is <strong>get the height</strong> of each collapsed item, do some JS math, and set a speed (animation time) value based on the height.</p><h4>jQuery functions we&#8217;ll need</h4><ul><li><a
href="http://docs.jquery.com/Traversing/next">next()</a></li><li><a
href="http://docs.jquery.com/CSS/height">height()</a></li><li><a
href="http://docs.jquery.com/Effects/slideUp">slideDown()</a> / <a
href="http://docs.jquery.com/Effects/slideDown">slideUp()</a></li></ul</p><h4>The Code</h4><pre><code>$(function(){

	<span>/* Variables to set */</span>
	var id = '#heightCheck';
	var click_element = 'h2';
	var height_value = '100';
	var tall_menu_speed = 400;
	var short_menu_speed = 250;

	<span>/* System variables, probably don't change */</span>
	var target = id +' '+ click_element;
	var menu_content = $(target).next();

	<span>/* Hide the element after the click_element, whether it's a
	div, p, ul, whatever you choose to wrap the items in */</span>
	menu_content.hide();

	$(target).toggle(function(){
		<span>/* save the menu items in a variable */</span>
		var this_menu = $(this).next();

		<span>/* get the menu height and save it */</span>
		var menu_height = this_menu.height();

		<span>/* Calculate the animation speed based on the element height */
		/* if the height is greater than the height set above use the
		tall menu height */</span>
		if(menu_height > height_value){
			var speed = tall_menu_speed;

		<span>/* If it's smaller, use the short menu height */</span>
		} else if(menu_height < height_value) {
			var speed = short_menu_speed;
		}

		<span>/* slide the menu down */</span>
		this_menu.slideDown(speed);

	},function(){
		<span>/* this is the same but reversed to close the menu */</span>
		var this_menu = $(this).next();
		var menu_height = $(this).next().height();

		<span>/* Calculate the animation speed based on the element height */</span>
		if(menu_height > height_value){
			var speed = tall_menu_speed;
		} else if(menu_height < height_value) {
			var speed = short_menu_speed;
		}
		this_menu.slideUp(speed);

	});

});</code></pre><p>That should give you a little more control over the menu speed. I'm sure it needs to be tweaked based on the menu you're using, but the general concept is there, and it should work pretty well. I hope it was helpful. Let me know if you have any additions to the code or suggestions to improve it.</p><p
class="demo"><a
href="http://www.csskarma.com/lab/animation_speed/">View demo</a></p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/conditional-animation-speed/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Quick Tip #2  &#8211; Bringing Back Search with jQuery</title><link>http://www.csskarma.com/blog/qt2-bringing-search-back/</link> <comments>http://www.csskarma.com/blog/qt2-bringing-search-back/#comments</comments> <pubDate>Mon, 29 Jun 2009 14:29:05 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=546</guid> <description><![CDATA[This is something I use on all my projects now. It&#8217;s designed for a search box, but can be used with any sort of input field. The great thing about this is that the field value &#8220;Search&#8221; will only come back onBlur if the field is empty (or doesn&#8217;t say &#8220;Search&#8221;). So if you started [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/images/articles/bringing-back-search.jpg" alt="article banner"/></p><p>This is something I use on all my projects now. It&#8217;s designed for a search box, but can be used with any sort of input field.</p><p>The great thing about this is that the field value &#8220;Search&#8221; will only come back onBlur if the field is empty (or doesn&#8217;t say &#8220;Search&#8221;). So if you started typing something it won&#8217;t erase your text, which is something that has always irritated me about search boxes.</p><p>Anyway, I&#8217;ve been sitting on this post for a while, but I really wanted to get it out. So here it is:</p><h5>XHTML</h5><pre><code>&lt;div id=&quot;search&quot;&gt;
&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
   &lt;label for=&quot;field&quot;&gt;search&lt;/label&gt;
   &lt;input type=&quot;text&quot; id=&quot;field&quot;/&gt;
   &lt;button type=&quot;submit&quot;&gt;go&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;<span>&lt;!--/search--&gt;</span></code></pre><h5>jQuery</h5><pre><code>$("#search input").focus( function() {
if ($(this).val()=="Search") {$(this).val("");}
});

$("#search input").blur( function() {
if ($(this).val()=="") {$(this).val("Search");}
});</code></pre><p>The word &#8220;Search&#8221; in the jQuery is case sensitive, so watch out for that.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/qt2-bringing-search-back/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>This Week in Links 12/16</title><link>http://www.csskarma.com/blog/this-week-in-links-12/</link> <comments>http://www.csskarma.com/blog/this-week-in-links-12/#comments</comments> <pubDate>Tue, 16 Dec 2008 15:52:53 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[News]]></category> <category><![CDATA[health]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[ui]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=284</guid> <description><![CDATA[10 Tips to Improve Your UI Smashing Magazine, once again, has a great article about user interface. This article gives you some quick and helpful tips on improving your user interface design. A lot of stuff we should all be doing. Anti-RSI This is a Mac program to help prevent RSI (which is basically carpal [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/images/articles/this-week-in-links.jpg" alt="article image"/></p><h5><a
href="http://www.smashingmagazine.com/2008/12/15/10-useful-techniques-to-improve-your-user-interface-designs/" rel="external">10 Tips to Improve Your UI</a></h5><p>Smashing Magazine, once again, has a great article about user interface. This article gives you some quick and helpful tips on improving your user interface design. A lot of stuff we should all be doing.</p><h5><a
href="http://tech.inhelsinki.nl/antirsi/" rel="external">Anti-RSI</a></h5><p>This is a Mac program to help prevent <a
href="http://en.wikipedia.org/wiki/Repetitive_strain_injury" rel="external">RSI</a> (which is basically carpal tunnel). I saw some pictures of people with RSI, it&#8217;s gross and looks very painful. I use Anti-RSI now, and I think you should as well. I&#8217;m not posting the pictures, but if you Google it, I&#8217;m sure they&#8217;ll turn up.</p><h5><a
href="http://www.sitepoint.com/article/helpful-hyperlinks-javascript/" rel="external">Helpful hyperlinks with JS</a></h5><p>An article from the boys over at SitePoint. This bit of JavaScript will pop an icon on the end of your links, letting the user know what they&#8217;re about to click. This can also be done with CSS 2.1, but by using JavaScript we&#8217;re making it available for&#8230; well let&#8217;s just say it&#8230; IE6 users.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/this-week-in-links-12/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>This Week in Links 10/27</title><link>http://www.csskarma.com/blog/this-week-in-links-8/</link> <comments>http://www.csskarma.com/blog/this-week-in-links-8/#comments</comments> <pubDate>Mon, 27 Oct 2008 16:04:50 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[News]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[finances]]></category> <category><![CDATA[font]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[seo]]></category> <category><![CDATA[twitter]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=213</guid> <description><![CDATA[Better CSS Font Stacks A good article on how to jazz up your stacks to try and take advantage of users who have more fonts installed. I&#8217;m all for this, and as long as it&#8217;s done carefully, can get a nice version of progressive enhancement. 5 Terrible SEO Ideas Richard Bradshaw goes over some trendy [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://www.csskarma.com/images/articles/this-week-in-links.jpg" alt="article banner"/></p><h5><a
href="http://unitinteractive.com/blog/2008/06/26/better-css-font-stacks/" rel="external">Better CSS Font Stacks</a></h5><p>A good article on how to jazz up your stacks to try and take advantage of users who have more fonts installed. I&#8217;m all for this, and as long as it&#8217;s done carefully, can get a nice version of progressive enhancement.</p><h5><a
href="http://www.bradshawenterprises.com/blog/2008/10/10/5-terrible-seo-ideas/" rel="external">5 Terrible SEO Ideas</a></h5><p>Richard Bradshaw goes over some trendy (and terrible) things that are common in SEO such as: keyword stuffing, dupe content, link farms, splash pages, and cloaking.</p><h5><a
href="http://jqueryfordesigners.com/fun-with-overflows/" rel="external">Fun with Overflows</a></h5><p>I think this is a <strong>great</strong> idea (<a
href="http://jqueryfordesigners.com/demo/scrollable-timelines.html" rel="external">check out the demo</a>). It creates a very similar interface to that of <a
href="http://www.plurk.com" rel="external">Plurk</a>, the twitter competitor.</p><h5><a
href="http://www.tweetwhatyouspend.com/" rel="external">Tweet What you Spend</a></h5><p>This is a pretty cool service that uses the Twitter API to track spending. I know it can seem kind of geeky (as is reading articles about CSS&#8230;), but tracking your spending is a great way to save money, it gives you a lot of power when you know exactly how your paycheck disappears every month. Provided you don&#8217;t mind sharing your expenses with the <a
href="http://twitter.com/csskarma/" rel="external">Twitterverse</a>, this is a good service and very easy to use.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/this-week-in-links-8/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Active navigation with jQuery</title><link>http://www.csskarma.com/blog/active-navigation-with-jquery/</link> <comments>http://www.csskarma.com/blog/active-navigation-with-jquery/#comments</comments> <pubDate>Wed, 08 Oct 2008 15:46:31 +0000</pubDate> <dc:creator>Tim</dc:creator> <category><![CDATA[Web Development]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[navigation]]></category> <category><![CDATA[ui]]></category><guid
isPermaLink="false">http://www.csskarma.com/blog/?p=155</guid> <description><![CDATA[I was building a site last week and came across some jQuery code for active navigation. It&#8217;s very simple and works real well. I&#8217;m using it on some secondary navigation and still using my body class for the main nav. Active navigation with a body class body.home #nav ul li.navhome{font-weight:700;} So I&#8217;m still doing that, [...]]]></description> <content:encoded><![CDATA[<p>I was building a site last week and came across some jQuery code for active navigation. It&#8217;s very simple and works real well. I&#8217;m using it on some secondary navigation and still using my <a
href="http://www.csskarma.com/blog/your-body-and-you/">body class</a> for the main <acronym
title="navigation">nav</acronym>.</p><h5>Active navigation with a body class</h5><pre><code>body.home #nav ul li.navhome{font-weight:700;}</code></pre><p>So I&#8217;m still doing that, but then for the internal page navigation, rather than loading the CSS up with long declarations for the numerous sub nav links, I used this jQuery:</p><h5>Active vavigation with jQuery</h5><pre><code>$("#nav a").each(function() {
var hreflink = $(this).attr("href");
if (hreflink.toLowerCase()==location.href.toLowerCase()) {
	$(this).parent("li").addClass("selected");
}</code></pre><h4>Breaking it down</h4><p>Let&#8217;s try this line by line for those who want to know what&#8217;s happening here.</p><p>This targets <em>each</em> link in contained in #nav.</p><pre><code>$("#nav a").each(function() {</code></pre><p>This line just saves the href attribute of each #nav a to a variable. It makes the code a little cleaner and easier to work with.</p><pre><code>var hreflink = $(this).attr("href");</code></pre><p>This converts the href of each link in #nav, makes it lowercase (it should be already, but just in case) and checks the current URL of the page and sees if they match. Also making the current page URL lowercase.</p><pre><code>if (hreflink.toLowerCase()==location.href.toLowerCase()) {</code></pre><p>If the current URL matches the href for one of the links it looks to the parent element (in this case it&#8217;s a list item) and adds a class of &#8220;selected&#8221; to it.</p><pre><code>$(this).parent("li").addClass("selected");</code></pre><p>This is just closing the function, I really don&#8217;t know why I didn&#8217;t include it in the above code for the purpose of this post. But no matter.</p><pre><code>}</code></pre><h4>Issues</h4><p>The only problem I ran into with this was that you have to make sure the you use a consistent href for pages.</p><h5>For example</h5><p>If you&#8217;re sub nav contains a link to <a
href="http://www.csskarma.com/contact/">http://www.csskarma.com/contact/</a>, you have to make sure that you don&#8217;t link to <a
href="http://www.csskarma.com/contact/index.php">http://www.csskarma.com/index.php</a>; or the active navigation won&#8217;t work.</p><p>It&#8217;s not a huge deal, but definitely something to be aware of, I just had to go through a site and clean up a bunch of links up because I applied this jQuery pretty late in development. You can probably rework the code to strip out the file name and extension or do it with some PHP, if you&#8217;d like.</p><p>Actually, if anyone does that, let me know and I&#8217;ll update this post with the code.</p> ]]></content:encoded> <wfw:commentRss>http://www.csskarma.com/blog/active-navigation-with-jquery/feed/</wfw:commentRss> <slash:comments>3</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
Database Caching 5/14 queries in 0.127 seconds using disk: basic

Served from: www.csskarma.com @ 2012-02-09 16:27:35 -->
