Posts Tagged ‘javascript’
|Conditional Animation Speed in jQuery
Friday, January 8th, 2010

For years (and by “years”, I most likely mean “the years since I started using jQuery… maybe 2″), there’s been one aspect of jQuery that’s bugged the crap out of me, the animation speed.
We’ve all probably seen the problem that happens in many drop down menus of varying height. We set slideDown(300) (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’s not much content in there.
This problem stems from where we set the speed of the animation; the value isn’t actually a speed, it’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.
It’s true, that you can use the built-in speed values like “fast” and “slow”, but if you want more fine-grained control over your animation, you’ll have to use a value.
Problem Child ∞
This is what our problem menu looks like all collapsed, just 2 clickable headings (h2) that will slide open to expose content (p)

Normally our jQuery would look something like this:
$(function(){
$('h2').toggle(function(){
/* get the next element */
var next_element = $(this).next();
/* open it if it's closed */
next_element.slideDown(500);
},function(){
/* get the next element again */
var next_element = $(this).next();
/* close it if it's open */
next_element.slideUp(500);
});
});
Note: just example code, I didn’t check to see if it actually works
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’re like “Why’s is moving so slow?”

So let’s see if we can’t fix that.
Ben Healy (Solution) ∞
What we’re looking to do here is get the height of each collapsed item, do some JS math, and set a speed (animation time) value based on the height.
jQuery functions we’ll need
The Code
$(function(){
/* Variables to set */
var id = '#heightCheck';
var click_element = 'h2';
var height_value = '100';
var tall_menu_speed = 400;
var short_menu_speed = 250;
/* System variables, probably don't change */
var target = id +' '+ click_element;
var menu_content = $(target).next();
/* Hide the element after the click_element, whether it's a
div, p, ul, whatever you choose to wrap the items in */
menu_content.hide();
$(target).toggle(function(){
/* save the menu items in a variable */
var this_menu = $(this).next();
/* get the menu height and save it */
var menu_height = this_menu.height();
/* Calculate the animation speed based on the element height */
/* if the height is greater than the height set above use the
tall menu height */
if(menu_height > height_value){
var speed = tall_menu_speed;
/* If it's smaller, use the short menu height */
} else if(menu_height < height_value) {
var speed = short_menu_speed;
}
/* slide the menu down */
this_menu.slideDown(speed);
},function(){
/* this is the same but reversed to close the menu */
var this_menu = $(this).next();
var menu_height = $(this).next().height();
/* Calculate the animation speed based on the element height */
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);
});
});
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.
Tags: advanced, javascript, jquery, ui
Posted in Web Design | 2 Comments »
Quick Tip #2 – Bringing Back Search with jQuery
Monday, June 29th, 2009

This is something I use on all my projects now. It’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 “Search” will only come back onBlur if the field is empty (or doesn’t say “Search”). So if you started typing something it won’t erase your text, which is something that has always irritated me about search boxes.
Anyway, I’ve been sitting on this post for a while, but I really wanted to get it out. So here it is:
XHTML
<div id="search">
<form method="post" action="">
<label for="field">search</label>
<input type="text" id="field"/>
<button type="submit">go</button>
</form>
</div><!--/search-->
jQuery
$("#search input").focus( function() {
if ($(this).val()=="Search") {$(this).val("");}
});
$("#search input").blur( function() {
if ($(this).val()=="") {$(this).val("Search");}
});
The word “Search” in the jQuery is case sensitive, so watch out for that.
Tags: javascript, jquery
Posted in Web Development | 6 Comments »
This Week in Links 12/16
Tuesday, December 16th, 2008

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 tunnel). I saw some pictures of people with RSI, it’s gross and looks very painful. I use Anti-RSI now, and I think you should as well. I’m not posting the pictures, but if you Google it, I’m sure they’ll turn up.
Helpful hyperlinks with JS
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’re about to click. This can also be done with CSS 2.1, but by using JavaScript we’re making it available for… well let’s just say it… IE6 users.
Tags: health, javascript, ui
Posted in News | No Comments »
This Week in Links 10/27
Monday, October 27th, 2008

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’m all for this, and as long as it’s done carefully, can get a nice version of progressive enhancement.
5 Terrible SEO Ideas
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.
Fun with Overflows
I think this is a great idea (check out the demo). It creates a very similar interface to that of Plurk, the twitter competitor.
Tweet What you Spend
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…), 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’t mind sharing your expenses with the Twitterverse, this is a good service and very easy to use.
Tags: CSS, finances, font, javascript, jquery, seo, twitter
Posted in News, Web Development | 1 Comment »
This Week in Links – 9/4
Thursday, September 4th, 2008
I’d like to try and start sharing some links once a week with everyone since I’m settling into a new job and I don’t want my posts to become few and far between. Just to let you know that I am doing some interesting things right now, but have just run a little short on time. Here are some links that grabbed my attention:
Some sites to be aware of:
Script & Style
Script & Style is a new site by Chris Coyier and David Walsh. The best way I can describe it is like subscribing to the CSS/JS search term RSS feed on Digg, but a little more interesting. There are user-submitted articles, like Digg, but you don’t have to filter out all that Digg-esk junk like articles about hamburger grease and bicycle chains. You just get the stuff you want. I like it, at least subscribe to the RSS feed, it’s well worth the visit.
Google Chrome bugs – Alternate Style Sheets and Max/Min-width
The big news for the week seemed to be about Google’s web browser, Chrome. Everyone seems to love it (I couldn’t install it on my parallel, I’ll try on Vista later on), but there are some bugs reported with it. I guess that’s why Google leaves everything in Beta for a decade.
Animation Jump in JQuery
When I first saw this quick tip from JQuery for Designers I really didn’t see a major need for it. But as I kept seeing it in my news feed it actually grew on me. It creates a nice sliding effect for anchor links, worth poking around with. I really don’t like dumping a ton of JavaScript into my pages, but for a JS light page, or an FAQ, this might be a nice feature.
Using JQuery UI to Control Text Size
I love Nettuts, every week they come out with some really cool tricks. This is one of those things that is a really nice tool to have in your library just in case you need it. I could see this being very useful for reading documents online or just zooming in on an app.
Automatic Awesompersands
YAJQPI I guess, but I kind like this one. It searches through your content to find and restyle ampersands, giving them a hip designer/small coffee shop look by wrapping a span around them (sidenote: are we using spans too much with jQuery?).
JavaScript in Modern Web Design
Web Designer Wall is a pretty popular blog (more so than this one), so I’m sure most have seen this already, but it’s still worth mentioning just in case. Nick La lists out and describes virtually every JavaScript you will commonly find in web design today. If nothing, it’s a great reference list.
Today seemed to be a little JavaScript heavy, but I guess that’s what’s going on right now as we’re all in our JQuery phase.
Tags: CSS, javascript, jquery, links
Posted in News | 1 Comment »




