Running CSS animations when the element is shown

Not a unique idea at all, many existing libraries out there, but I wanted a very basic jQuery plugin that I controlled entirely with CSS animations and classes being applied when the user scrolled to (or past) the element, along with a few key features: chaining animations to run in sequence, customisable delays and customisable visibility offsets.

Some code snippets from the example page. The get started, all you need to add to your page is:

then you can add the following classes to your page and the animations will work when the user scrolls to them:

All delays are relative to the first element shown with a data-animate-chain attribute, so when this first element is shown, the timeouts are set up for all other elements with the same data-animate-chain attributes.

The CSS is pretty straightforward and some basic example animations are defined in animations.css

The simplified example above controls the basis for the plugin, first all elements with an animate-on-show class are set up with a 1s animation. Then the child classes, in this case fade-in are defined with an initial state, and how it should end once the animation is applied (.animated). The plugin essentially just applies the animated class to each element that is deemed visible and CSS does the rest. The additional chaining happens at the same time along with accounting for delays.

There are some global settings you can define, including vertical adjustments for some classes. For example slide-in-from-below:

is configured with an initial state of 400px below the target end location, this means we have to adjust our offset by 400. The $.AnimateOnShow.settings.custom object handles this:

If you’re adding in other animations that adjust the vertical position, be sure to add these to your version of the plugin or change in your page’s script via $.AnimateOnShow.settings.custom['your-classname-here'] = <adjustment>.

Hope this saves someone some time if they want to do something similar!

I’ve made it a fairly customisable package although it does currently rely on jQuery as the site for which this was originally developed already had it.

There’s a demo page that includes download links and you are free to use this code however you see fit!

Can you make your scripts smaller?

Minifying your JavaScript is something most people are familiar with and have to do fairly regularly, whether is reducing your page load time on PC or keeping the files within the mobile cache limits.

Having recently seen UglifyJS and having used some of the more obvious tricks (!0 === true, !1 === false, etc) myself, I wondered if there was a way you could shrink down what’s transferred even more…

As a proof of concept I’ve knocked up a (hopefully fairly) simple perl script that will wrap your code in a closure and replace a lot of the internal indexes that are used with global symbols, which when compressed with YUI/Uglify should reduce filesize overall.
Continue reading

Sonic the Hedgehog ‘construction cheat’ in JavaScript

I clearly spent a little too long thinking about this, but it was fun nonetheless!

I showed my 5-year-old nephew the construction cheat in Sonic the Hedgehog the other weekend and he thoroughly enjoyed printing rocks everywhere on the screen, so I wondered if it would be possible to do something similar in JavaScript, since people love using the Konami code for easter eggs, why not Sonic?

I opted for native javascript, not using any libraries for easy portability.

I’ve encountered a few issues on some sites that already capture keypresses, but for the most part it’s worked ok. I’ve only tested this on Chrome and Firefox (Mac, latest versions) but it should work fine on Windows. No idea about IE… Probably not, but I did try to use cross-browser methods where applicable!

The commented source is here and the script is enabled on the blog so you should be able to use it anywhere.

Have fun!

Oh, here are some sprites for good measure:

WebDAV file upload bookmarklet

I recently had a discussion with a co-worker as to whether or not it would be possible to upload files to a WebDAV server via AJAX and in doing so discovered it is indeed totally possibly to do so and so I went on a little bit of a mission and made a pretty basic interface for apache that replaced the standard directory listing and have also made a bookmarklet.

There’s no demo I’m afraid, however the bookmarklet should work on just about any WebDAV server (at least, it worked on mine…), but you won’t have pretty code highlighting :(.

I’ve set up a github repository but you can use the bookmarklet directly.

Note: This will almost certainly not work in IE and I’ve only tested Firefox and Chrome (latest versions).

EDIT: minor update and moved to GitHub pages instead of using raw which caused problems with the CSS on Firefox.

Pop quiz!

An interesting item from stackoverflow via /r/javascript!

Why does parseInt(1/0, 18) == NaN, but parseInt(1/0, 19) == 18 and parseInt(1/0, 25) == 185011843?

Javascript apparently has the division by zero problem solved, due to (x > 0)/0 being equal to Infinity, an object (constant?) in JS in the same vein as NaN. It interacts with NaN as well, in that Infinity - Infinity or Infinity / Infinity == NaN, but Infinity + Infinity or Infinity * Infinity == Infinity. Also there’s -Infinity, just for fun.

When parseInt()ing the value infinity, internally it must run .toString() to get the value which returns "Infinity". Base 19 has the numbers 0-9 and then goes on to the letters a-i (there’s a table in the stackoverflow article) of which the first letter of "Infinity" matches, returning 18, when you up the base to 25, all the letters up to n are parsed turning the ‘number’ "Infinity" into 185011843. Interestingly that also means that parseInt(Infinity/Infinity, 25) == 14648!

Mousetrap – excellent keyboard shortcuts

Just discovered a great little library called mousetrap.

It enables you to have complex, chained if required, keyboard events bound in a very simple, powerful way with no reliance on an underlying library.

Example usage:

Find out more, test and download it here, or access the git repository here.

Better timeouts and intervals with Javascript

window.setTimeout and window.setInterval don’t ever seem to have been given any love for a long time. (At least from my limited end-user point of view). I’ve toyed in the past with a better queuing system for them and having the ability to run on clear (especially for intervals), but I’ve actually knocked up a basic script that allows object-oriented timeout events. It still uses the native implementations outside, and I’m sure that this has probably already been done 100 times, but I still wanted to have a go.

This script extends the return from window.setTimeout and window.setInterval to be an object itself that you can stop() and restart(), preserving the function for further use.

Please note: I’ve only really tested this in Chrome! I can’t see why it wouldn’t work in other browsers, I guess maybe some of these would be protected, but I’m not sure… If that’s the case, it wouldn’t be too difficult to utilise the alternative new Timeout(function(){ }, 400); syntax…

There’s a bit more (not much!) of an explanation here and the code is available here. There are no prerequisites for this code, should run on plain old Javascript.

Edit: Minor update to the script to include the object in the callback.

jQuery Parallax: In action

A lot of the traffic I get to this blog is related to the jQuery Parallax script I wrote quite some time ago. Looking at the code now, I don’t think I’d change too much about it, perhaps use .on('mousemouse', ...) instead of .mousemove(), but mostly it seems to serve it’s function quite well. Since the only real functions being called are .mousemove() and .css() it should still be working with the latest versions of jQuery and unless they decide to majorly change the API, it should continue to work too!

Since my example was on a fairly empty page, I thought I’d link to a couple of sites that are using it in the real world:

If you’re using the script and think your sites is a good example of utilising the parallax effect, please leave me a message!

Tool: Fix broken unicode characters

So in the past I’ve had many a nightmare receiving garbled unicode characters in an email or in a poorly encoded database field, so I’ve written a basic tool that works with some of the most common Western European characters and will correctly convert them (optionally to their closest matching standard ASCII character).

I am going to try and work on a full set (that can be incrementally loaded), but this has been sufficient for my needs so far.

Use it here!

Edit: I’ve updated the Tool to allow for various functions, input via literals (\x0d or \u02f4), returning to multibyte in the text area and other fixes. The same link will work.

This link was incredibly helpful with my investigation and testing!

Bookmarklet: Reload CSS files without reloading the page

Working on styling the contents of popup windows can be frustrating and laborious. But not if you can just reload the CSS…

This bookmarklet will reload all the associated stylesheets in the current document. It looks for all <link/> elements with rel="stylesheet", removes from the DOM and re-adds them appending a ?_=1234567890 (or &_ depending on the URL, where the numbers represent the JS timestamp) into the <head/>.

Hopefully this will help someone suffering similar pain to myself!

Reload CSS (Add the link to the left to your bookmarks to use!)

Please note: I haven’t tested this in IE, but surely it’ll work… Right?

Continue reading