Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Saturday, February 11, 2012

Differences Between jQuery .bind() vs .live() vs .delegate() vs .on()


Using the Bind Method


The .bind() method registers the type of event and an event handler directly to the DOM element in question. This method has been around the longest and in its day it was a nice abstraction around the various cross-browser issues that existed.

The .bind() method will attach the event handler to all of the anchors that are matched! That is not good. Not only is that expensive to implicitly iterate over all of those items to attach an event handler, but it is also wasteful since it is the same event handler over and over again.

Pros
  • This methods works across various browser implementations.
  • It is pretty easy and quick to wire-up event handlers.
  • The shorthand methods (.click().hover(), etc...) make it even easier to wire-up event handlers.
  • For a simple ID selector, using .bind() not only wires-up quickly, but also when the event fires the event handler is invoked almost immediately.

Cons
  • The method attaches the same event handler to every matched element in the selection.
  • It doesn't work for elements added dynamically that matches the same selector.
  • There are performance concerns when dealing with a large selection.
  • The attachment is done upfront which can have performance issues on page load.

Using the Live Method


The .live() method uses the concept of event delegation to perform its so called "magic". The way you call .live() looks just like how you might call .bind(), which is very convenient. However, under the covers this method works much different. The .live method attaches the event handler to the root level document along with the associated selector and event information. By registering this information on the document it allows one event handler to be used for all events that have bubbled (a.k.a. delegated, propagated) up to it. Once an event has bubbled up to the document jQuery looks at the selector/event metadata to determine which handler it should invoke, if any. This extra work has some impact on performance at the point of user interaction, but the initial register process is fairly speedy. 

The good thing about this code as compared to the .bind() example above is that it is only attaching the event handler once to the document instead of multiple times. This not only is faster, but less wasteful, however, there are many problems with using this method and they are outlined below.

Pros
  • There is only one event handler registered instead of the numerous event handlers that could have been registered with the .bind() method.
  • The upgrade path from .bind() to .live() is very small. All you have to do is replace "bind" to "live".
  • Elements dynamically added to the DOM that match the selector magically work because the real information was registered on the document.
  • You can wire-up event handlers before the document ready event helping you utilize possibly unused time.

Cons
  • This method is deprecated as of jQuery 1.7 and you should start phasing out its use in your code.
  • Chaining is not properly supported using this method.
  • The selection that is made is basically thrown away since it is only used to register the event handler on the document.
  • Using event.stopPropagation() is no longer helpful because the event has already delegated all the way up to the document.
  • Since all selector/event information is attached to the document once an event does occur jQuery has match through its large metadata store using the matchesSelectormethod to determine which event handler to invoke, if any.
  • Your events always delegate all the way up to the document. This can affect performance if your DOM is deep.

Using the Delegate Method


The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored. Just like the .live() method, this technique uses event delegation to work correctly. 

If you skipped over the explanation of the .live() method you might want to go back up and read it as I described some of the internal logic that happen.
The .delegate() method is very powerful. The above code will attach the event handler to the unordered list ("#members") along with the selector/event information. This is much more efficient than the .live() method that always attaches the information to the document. In addition a lot of other problematic issues were resolved by introducing the .delegate() method. See the following outline for a detailed list.

Pros
  • You have the option of choosing where to attach the selector/event information.
  • The selection isn't actually performed up front, but is only used to register onto the root element.
  • Chaining is supported correctly.
  • jQuery still needs to iterate over the selector/event data to determine a match, but since you can choose where the root is the amount of data to sort through can be much smaller.
  • Since this technique uses event delegation, it can work with dynamically added elements to the DOM where the selectors match.
  • As long as you delegate against the document you can also wire-up event handlers before the document ready event.

Cons
  • Changing from a .bind() to a .delegate() method isn't as straight forward.
  • There is still the concern of jQuery having to figure out, using the matchesSelectormethod, which event handler to invoke based on the selector/event information stored at the root element. However, the metadata stored at the root element should be considerably smaller compared to using the .live() method.

Using the On Method


Did you know that the jQuery .bind().live(), and .delegate() methods are just one line pass throughs to the new jQuery 1.7 .on() method? The same is true of the .unbind(),.die(), and .undelegate() methods.

Pros
  • Brings uniformity to the various event binding methods.
  • Simplifies the jQuery code base and removes one level of redirection since the .bind(),.live(), and .delegate() call this method under the covers.
  • Still provides all the goodness of the .delegate() method, while still providing support for the .bind() method if you need it.

Cons
  • Brings confusion because the behavior changes based on how you call the method.

Friday, November 11, 2011

25 Cool jQuery Tooltip Plugins for Interactive Designs


jQuery Tooltip
jQuery tooltip is certainly one of the building blocks you should consider using in your web design and development. jQuery tooltips are easy to install and set up and they help you offer extra information on important elements and at the same time make the browsing experience of every visitor interesting and dynamic. Simple and minimalistic design is good for user experience and usability and jQuery tooltips help you achieve this by allowing you to “not show it all” at once. There is a lot of jQuery tooltip scripts available and some are free and some premium. Check out the demos and documentation to make sure you find what suits your needs best.
In this article we are sharing with you jQuery tooltips that you can use in your website to make it look cool and interesting. These are all easy to download scripts and easy to use, if you have the required tweaking know-how.

Wednesday, August 31, 2011

defunkt/jquery-pjax - GitHub

what is it?

pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades.
pjax enhances the browsing experience - nothing more.
You can find a demo on http://pjax.heroku.com/

browser support

pjax only works with browsers that support the history.pushState API.
For a table of supported browsers see: http://caniuse.com/#search=pushstate
To check if pjax is supported, use the $.support.pjax boolean.
When pjax is not supported, $('a').pjax() calls will do nothing (aka links work normally) and $.pjax({url:url}) calls will redirect to the given URL.

Tuesday, July 26, 2011

Chosen

Chosen is a JavaScript plugin that makes long, unwieldy select boxes much more user-friendly. It is currently available in both jQuery and Prototype flavors.

https://github.com/harvesthq/chosen/
http://harvesthq.github.com/chosen/ 

Sunday, June 12, 2011

jVectorMap

jVectorMap is a jQuery plugin employed to show vector maps and visualize data on HTML pages. It uses SVG in all modern browsers like Firefox 3 or 4, Safari, Chrome, Opera, IE9, while legacy support for older versions of IE from 6 to 8 is provided with VML. Using jVectorMap is pretty simple as for any other jQuery plugin.

http://jvectormap.owl-hollow.net

Sunday, February 13, 2011

Isotope

  1. An exquisite jQuery plugin for magical layouts
  2. Reveal & hide items with filtering
  3. Re–order items with sorting
  4. Dynamic, intelligent layouts
  5. Captivating animations
  6. Sort items by just about anything
  7. Powerful methods, simple syntax
  8. Progressively enhanced for CSS3 transforms & transitions
http://isotope.metafizzy.co/

jQuery Splatter Plugin

Splatter is a jQuery plugin which applies a random position, size, and color to elements on a page. The most basic implementation adds randomly colored and positioned asterisks to the element:
simple_example
To see some working examples, check out the demo page.

http://coryschires.com/jquery-splatter-plugin/

The Difference Between jQuery’s .bind(), .live(), and .delegate()

The difference between .bind(), .live(), and .delegate() is not always apparent. Having a clear understanding of all the differences, though, will help us write more concise code and prevent bugs from popping up in our interactive applications.

.bind()


$('a').bind('click', function() { alert("That tickles!") });
This is the most straight forward binding method. jQuery scans the document for all $('a') elements and binds the alert function to each of their click events.

.live()


$('a').live('click', function() { alert("That tickles!") });
jQuery binds the alert function to the $(document) element, along with 'click' and 'a' as parameters. Any time an event bubbles up to the document node, it checks to see if the event was a click and if the target element of that event matches the 'a' CSS selector. If both are true, the function executes.
The live method can also be bound to a specific element (or “context”) other than document, like this:

$('a', $('#container')[0]).live(...);

.delegate()


$('#container').delegate('a', 'click', function() { alert("That tickles!") });
jQuery scans the document for $('#container'), and binds the alert function along with the click event and 'a' CSS selector as parameters. Any time an event bubbles up to $('#container'), it checks to see if the event was a click and if the target element of that event matches the CSS selector. If both checks are true, it executes the function.
Notice this is similar to .live(), except that it binds the handler to the specified element instead of the document root. The astute JS’er might conclude that $('a').live() == $(document).delegate('a'), right? Well, no, not exactly.

http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/?utm_content=backtype-tweetcount&utm_medium=bt.io-twitter&utm_source=netvibes.com

zClip

zClip is a lightweight jQuery "copy to clipboard" plugin built using the popular Zero Clipboard library. This plugin uses an invisible Adobe Flash movie that is fully compatible with Flash Player 10 and below.

http://www.steamdev.com/zclip/

Monday, February 7, 2011

JS Libs Deconstructed

The Deconstructed series is designed to visually and interactively deconstruct the internal code of JavaScript libraries, including jQuery, Prototype and MooTools.
It breaks the physical JavaScript into visual blocks that you can easiliy navigate. Each block opens to reveal its internal code. Clickable hyperlinks allow you to follow program flow.

Wednesday, February 2, 2011

Smooth Div Scroll

Smooth Div Scroll is a jQuery plugin that scrolls content horizontally left or right. Apart from many of the other scrolling plugins that have been written for jQuery, Smooth Div Scroll does not limit the scrolling to distinct steps. As the name of the plugin hints, scrolling is smooth. There are no visible buttons or links since the scrolling is done using hotspots within the scrollable area or via autoscrolling. Unobtrusive and smooth is the key here.

Table of contents

  1. What is Smooth Div Scroll?
  2. Quick demo
  3. Basic demo (use as template)
  4. More examples!
  5. Options
  6. Altering options after initialization
  7. Public methods
  8. Callbacks
  9. The CSS
  10. Dependencies
  11. Download
  12. Help and support
  13. Newsletter
  14. Version history
  15. To-do (the next version)
  16. Copyright, license and all that
  17. About me
  18. Nice sites that use Smooth Div Scroll
  19. Please give this plugin a rating
http://www.smoothdivscroll.com/

Thursday, January 20, 2011

Signature Pad

A jQuery plugin for assisting in the creation of an HTML5 canvas based signature pad. Records the drawn signature in JSON for later regeneration.

The Signature Pad jQuery plugin will transform an HTML form into a signature pad. The Signature Pad has two modes: TypeIt and DrawIt. In TypeIt mode the user’s signature is automatically generated as HTML text, styled with @font-face, from the input field where they type their name. In DrawIt mode the user is able to draw their signature on the canvas element.
The drawn signature is written out to a hidden input field as a JSON array using JSON.strinify(). Since the signature is saved as JSON it can be submitted as part of the form and kept on file. Using the JSON array, the signature can then be regenerated into the canvas element for display.
Obviously, Signature Pad has a couple extra dependencies: Douglas Crockford’s JSON2 and Google’s Explorer Canvas. (Both scripts are included in the downloadable package.)
Signature Pad tries to maintain a certain level of progressive enhancement, while still giving developers enough control. There is very little generated HTML. The HTML in the examples has some elements that should be hidden by default (including canvas). Signature Pad will trigger the display of certain items if the browser supports Javascript and canvas.
Signature Pad works with both mouse and touch devices.
Get the source code on GitHub:
http://github.com/thomasjbradley/signature-pad/

Signature Pad is able to create multiple instances on a single page by selecting multiple items with jQuery. The limitation lies in the API return; Signature Pad will only return the API for the last selected element. If you would like to store the API for multiple elements they must be initialised with separate jQuery calls.

http://thomasjbradley.ca/lab/signature-pad

Wednesday, January 19, 2011

Embedded help system

Introduction

Embedded Help System is "providing help where help is needed" concept that can be easily integrated into web interface. The point is to offer help to user in their working interface and actual situation.

Jquery plugin

Jquery E-Help is plugin for procedural("How to ...") web user interface help and it's easy to integrate into any web interface that supports Jquery. Useful for all web applications, CMS and E-commerce systems.
Try jquery.eHelp here and be free to download, use and modify...

Contribute

Contribute to embedded help concept and develop more useful scripts that can be easily integrated into web interface.

http://embedded-help.net/home

Tuesday, January 11, 2011

NETEYE Activity Indicator

NETEYE Activity Indicator

A jQuery plugin that renders a translucent activity indicator (spinner) using SVG or VML.

Features

  • Lightweight script
  • No images required
  • No external CSS
  • Resolution independent
  • Alpha transparency
  • Highly configurable appearance
  • Works in all major browsers
  • Uses feature detection
  • Degrades gracefully

Supported Browsers

The plugin has been successfully tested in the following browsers:
  • Firefox 2.0
  • Safari 3.2.1
  • Internet Explorer 6.0
  • Opera 10.6
Of course newer versions of the various browsers are also supported.

Dependencies

The plugin requires jQuery v1.4.2 (or higher). Besides that, no other files are required, especially no style-sheets or images.

http://neteye.github.com/activity-indicator.html

Thursday, December 30, 2010

jQuery custom content scroller

Custom scrollbar plugin utilizing jquery UI that’s fully customizable with CSS. It features vertical/horizontal scrolling, mouse-wheel support (via Brandon Aaron jquery mouse-wheel plugin), scroll easing and adjustable scrollbar height/width.




malihu custom scrollbar plugin
http://manos.malihu.gr/jquery-custom-content-scroller

Sunday, December 5, 2010

rotate3Di - Flip HTML content in 3D

Rotate3Di is a jQuery Effect Plugin that makes it possible to do an isometric 3D flip or 3D rotation of any HTML content. It also enables custom 3D rotation animations. CSS3 Transforms are used to create this visual "3D" isometric effect. Supported browsers are WebKit, Safari, Chrome, Firefox 3.5+, IE9 (Platform Preview 7+), and probably Opera. The plugin's functionality includes: setting or animating HTML content to an arbitrary isometric rotation angle, as well as flipping, unflipping, or toggling the flip state of an object.

http://www.zachstronaut.com/projects/rotate3di/

Sunday, November 28, 2010

jQuery’s Data Method – How and Why to Use It

jQuery’s data method gives us the ability to associate arbitrary data with DOM nodes and JavaScript objects. This makes our code more concise and clean. As of jQuery 1.4.3 we also have the ability to use the method on regular JavaScript objects and listen for changes, which opens the doors to some quite interesting applications.

Behind the Scenes

Internally, jQuery creates an empty object (called $.cache for the curious), which is used to store the values you set via the data method. Each DOM element you add data to, is assigned a unique ID which is used as a key in the $.cache object.
jQuery does not store only user-created data in that cache. It also stores internal information and the event handling functions that you attach with live(), bind() and delegate(). Having centralized data storage makes jQuery’s codebase much more robust and bug free, something that we all can benefit from.

To Wrap it Up

The data method is just one of jQuery’s numerous utilities, which make the life of the web developer easier. Combined with the rest of the library’s abilities, it adds up to a solid foundation we can build upon.

http://tutorialzine.com/2010/11/jquery-data-method/

Sunday, November 7, 2010

Snippet :: jQuery Syntax Highlighter


Snippet is a jQuery syntax highlighting plugin built on top of the SHJS script found on SourceForge. Snippet provides a quick and easy way of highlighting source code passages in HTML documents.

Features

  • Natively supports 15 popular languages:
    • C
    • C++
    • C#
    • CSS
    • Flex
    • HTML
    • Java
    • JavaScript
    • JavaScript with DOM
    • Perl
    • PHP
    • Python
    • Ruby
    • SQL
    • XML
  • Can be extended to support:
    • Bison
    • ChangeLog
    • Desktop Files
    • Diff
    • GLSL
    • Haxe
    • Java prop files
    • LaTex
    • LDAP files
    • Log files
    • LSM files
    • M4
    • Makefile
    • Objective Caml
    • Oracle SQL
    • Pascal
    • Prolog
    • RPM spec files
    • S-Lang
    • Scala
    • Shell
    • Standard ML
    • Tcl
    • Xorg config files
  • 39 unique color schemes 
http://steamdev.com/snippet/

    Tuesday, October 5, 2010

    Microsoft Commits Code to jQuery!

    This week on Web Camps TV, Stephen Walther from the ASP.NET Team joins James Senior (@jsenior) to discuss an exciting announcement: Microsoft is about to make the first contributions to the jQuery open source project!

    In the past 6 months, we've been busy working on three features:
    1. Templating
    2. Data-Linking
    3. Globalization
    In this video, we show demos of the contributions in action and how/when they will be available for developers to use!

    Find out more at www.jquery.com and be sure to sign up for a freeWeb Camp to learn more about the jQuery contributions.

    http://channel9.msdn.com/Shows/Web+Camps+TV/Web-Camps-TV-5-Microsoft-Commits-Code-to-jQuery

    RequireJS

    RequireJS loads plain JavaScript files as well as more defined modules. It is optimized for in-browser use, including in a Web Worker, but it can be used in other JavaScript environments, like Rhino and Node. It implements the CommonJS Transport/C proposal API.
    RequireJS uses plain script tags to load modules/files, so it should allow for easy debugging. It can be used simply to load existing JavaScript files, so you can add it to your existing project without having to re-write your JavaScript files.
    RequireJS includes an optimization tool you can run as part of your packaging steps for deploying your code. The optimization tool can combine and minify your JavaScript files to allow for better performance.
    If the JavaScript file defines a JavaScript module via require.def(), then there are other benefits RequireJS can offer: better CommonJS support and loading multiple versions of a module in a page. RequireJS also allows for module modifiers and has a plugin system that supports features like i18n string bundles, text file dependencies, and JSONP service dependencies.
    RequireJS does not have any dependencies on a JavaScript framework. It is tri-licensed -- BSD, MIT, and GPL.
    The standard require.js file is around 4.9KB when minified via Closure Compiler and gzipped. require.js can be built without some features, with the smallest option (just dependency tracking and simple module loading) weighing in at 3.9KB minified, gzipped.
    RequireJS works in IE 6+, Firefox 2+, Safari 3.2+, Chrome 3+, and Opera 10+.

    http://requirejs.org/