Cover by Raffaele Cecco

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Converting into a jQuery Plug-in

Converting the bouncy sprite application into a jQuery plug-in adds functionality that takes advantage of jQuery’s ability to easily search for DOM elements via CSS selectors and return them as a list for further processing. The plug-in will search for any specified elements and attach multiple bouncySprite instances to them via the bouncyBoss object. It will also allow you to change the background color of the elements being attached to and the number of bouncySprite instances being attached.

Although converting the bouncy sprite application into a flexible jQuery plug-in may sound like a substantial job, there’s actually less work involved than you might expect. Because of the modular and self-contained way in which the DHTMLSprite, bouncySprite, and bouncyBoss objects were developed, they will slip unobtrusively into a jQuery plug-in structure.

The initial solitary semicolon may seem odd (it is not a typo), but it protects against problems arising from cases where the code preceding the plug-in does not end with a semicolon as expected. Normally, this is not an issue, as JavaScript will identify the plug-in code after a line break as a new statement. However, if the preceding code and the plug-in were minified, the whitespace—including line breaks—would probably be removed. The plug-in would subsequently fail due to the lack of an identifying break between it and the preceding code.

;    // Initial solitary semicolon.

Here, we define an anonymous function. ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required