Monday, November 7, 2011

quick hit: a link o' snippets

http://www.catswhocode.com/blog/10-jquery-snippets-for-efficient-developers has some decent little snippets, I liked the age verification one in particular, and the one for seeing if an image had loaded was good as well.

Friday, November 4, 2011

metablogger meta blogger blogspot

So yesterday was the first day I tried making an embedded interactive bit... it wasn't as bad as I had feared (I've used other blogging software that has the WYSIWYG mode kind of running roughshod over the code you're trying to include), weirdly the one gotcha was that none of the parts seemed clickable... firedebug showed a "clickTrap" div around where I was expecting the action to be.  Doria's Design Notes had the fix, however, you need to put this as some CSS in the entry
<style>div.blogger-clickTrap {display: none;}</style>
Not sure what that's all about but the fix seems fine.

Thursday, November 3, 2011

a more pleasing easing

The other day I mentioned my admiration for making the jQuery animation default "swing". Today I'll talk about some of the other options provided by the jQuery Easing plugin.

Used judiciously, animations can make transitions to the state of the page less abrupt. Fiddling with easings gives you more options to add a "physicality" to the objects in question. Together, animation and easings can add a pleasant physicality (or bit of "juiciness" as I've got people saying at work.) to otherwise boring pages. (There's a reason why these kinds of animations and a sense of physicality is deeply baked into iOS...)

Actually, that page itself does a good job of showing usage... but I'm not crazy about the widget they give you to try out the various easings provided-- on that page, "The Clicker" always shows you 2 animations back to back (one for increasing the boxes size and one for putting it back) and so it's difficult to get a feel for each selection in isolation. Also, I think moving objects is a more common form of animation, and in general I prefer not to have to fiddle with select boxes. So I present: The Mover/Grower(tm). Like "The Clicker" it builds the list of easings by querying the module itself... you can set the speed, decide which (or both) of the things you want to animate, and then click to see any easing. (Click in rapid succession to combine.)

Mover:
Grower:

Speed: ms
Demonstrate: move grow both
jQuery Builtins: linear swing
From easing extension:

In terms of which of these to use, if you're in a hurry...our first use case for this was an object flying from off the screen-- "easeOutBounce" had the nicest feel to the landing.  Some of the "In" animations feel a little corny when they start, like a Hannah-Barbara cartoon character getting its feet under it before starting to run, but "easeInOutElastic" is subdued and pleasant.

Some other caveats are that animations can feel choppy if there are too many at once, or if the page is still kind of loading, or if there's a json-ish query or what have you, so want to be careful and minimize other things going on on the page when the thing loads (for example, a second delay before a message notifiation flies in, or launching a JSON request on animation completion, rather than at the same time as the animation started.)

Monday, October 31, 2011

color animation


As a developer quickly learns, jQuery offers a nice toolkit to fiddle with CSS on the fly. In particular, you can use animate() to trivially make rough transitions smooth: 
$(".something").animate({"property-name":numericvalue);


The default easing (how it changes from the first state to the next) is called "swing", and gives a nice effect that implies the object has a bit of momentum. (It's a subtle touch of designer-influenced genius that they made the default "swing" rather than the nerdy, more-engineer-friendly linear...) More on easings later...

One seeming gap is that out of the box you can't use the animate() function on colors...  but now, "there's a plugin for that", Bitstorm.org's jQuery Color Animation.

Put a reference to it atop your file:
<script src="/path/to/jquery.animate-colors.js" type="text/javascript"></script>
and then you're free to do something like
$(".userProfileHolder .yoopTab.messages").animate({"background-color":"#ffffff"});

A few small caveats:

  • with the version I'm using, you need to use r/g/b hex values, not the human readable colors like "white"
  • you need to make sure the object your modifying has a base color set (preferably in hex colors too.)
  • if you're changing, say, the background-color property, you may be implicitly setting the background color (or rather semi-explicitly leaving out the background color property) with the "background" shortcut property, which might cause problems. In today's case we were using a "background" property to show a transparent PNG, and then letting the parent's bckground color shine through. I broke up the "background" shortcut property into its individual parts and THEN set the background-color explicitly.
That's it though-- it's might handy and makes for some nice looking transitions than just toggling from one color to another


Monday, October 24, 2011

FancyBox: prettier dialogs


So, one easy way of getting pretty (prettier than jqModal, anyway) modal dialog boxes is FancyBox. I've seen its distinctive "phillip's head" close button all over the place, so I'm guessing it's pretty popular.

Like jqModal, you put the content somewhere on your page, and then there are 2 functions, one for telling the library what should be in a dialog, and another for launching it.

FancyBox's boilerplate is similar to jqModal, with 2 exceptions:

  1. You call the "make this a dialog" on the link that refers to the div, not the div itself. I thought this was a bit counter-intuitive.
  2. If you use jqModal's jqmWindow class it default would tend to hide the content before it's ready, but with fancybox we tend to put the dialogs inside a div which is then hidden.

Here's the code:


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.4.css" />
<script src="jquery-1.4.3.min.js"></script>
<script src="fancybox/jquery.fancybox-1.3.4.js"></script>
<script>
$(document).ready(function() {
    $("#fancyLaunch").fancybox();
});
</script>
</head>
<body>
<a href="#fancyDialog" id="fancyLaunch">Show Dialog</a>
<div style="display:none;">
  <div class="jqmWindow" id="fancyDialog">
    HELLO AGAIN
  </div>
</div>
</body>
</html>

So pretty much the same.

Disclaimer, I did establish that it is FancyBox (probably it's modal screen) that is interfering with other animations we have on the page, that FancyBox interferes in a way jqModal doesn't. But for simple tasks, FancyBox has the edge because it's less work to make it pretty.

jqModal: flexible dialogs

A common task is putting up a "modal dialog", a box that appears over the rest of the website.

We've been having some trouble at work that might be related to the modal kit fancybox. (More on that later.) Meanwhile I've started investigating jqModal jqModal, "Minimalistic Modaling with jQuery". Besides a possible timing issue we're seeing with fancybox, I appreciate how jqModal really strips things down to the basics, while still providing some sensible look and feel defaults. Here's the example boilerplate I came up with, just grabbing the js and the css from that link:



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="jqModal.css" />
<script src="jquery.min.js"></script>
<script src="jqModal.js"></script>
<script>
$(document).ready(function() {
  $("#dialog").jqm();
});
</script>
</head>
<body>
<input type="button" onClick='$("#dialog").jqmShow()' value="show">
<div class="jqmWindow" id="dialog">
<a style="float:right;" class="jqmClose">X</a>
  HELLO AGAIN
</div>
</body>
</html>

The trick is putting the content in the div, and then blessing it with ".jqm()". Although there is a facility for turning any link into a dialog launcher, I like that the emphasis is on the programmatic launch, just calling .jqmShow() on that dialog. I did use the link-blessing pattern for the close, however.

Easy, and flexible.

easy rounded corners

So, probably old news to most developers here...

IE should be ashamed at not supporting any standards for CSS rounded corners up 'til the very recent IE9.  Rounded corners looks better, designers are always asking for it, and faking 'em is generally a pain in the ass, involving polluting simple design with gratuitous graphics and/or ungainly structure.

The easiest solution to this is the jQuery Corner Plugin. In its simplest form you include the library:
<script src="/path/to/jquery.corner.js"></script>
and then something like this when the page loads:
document.ready(function(){
  $(".selector_for_class").corner();
});
Unfortunately, you might get a small flash of square corners 'til the document ready kicks in, unless of course you're hiding items with a loading type screen. 

The defaults for rounded-ness are well chosen, but if you want many more options you can check out the demo page. 

The library uses native rounded corners where available (there were a few different standards, alas) and graphics for earlier versions of IE. In practice you sometimes get into trouble on older IE if the div is over a complex background.

Still, easy-peasy, and 9 times out of 10 it looks just great.