Pages

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

Friday, August 5, 2011

Creating a plugin for jQuery


Learn how to create a plug-in for jQuery from scratch - the basics, options, compatibility and examples.

The Basics
A plugin is written as a method or function.

Creating a jQuery function

Syntax
The function must return this.each (..) to maintain the ability to "chaining" - so that the function can be used with one or several objects jQuery.

jQuery.fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};


Example

jQuery.fn.makeTextRed = function () {
this.each return (function () {
$ (This). Css ('color', 'red');
});
};
/ / Example usage

$ ('# My-div'). MakeTextRed () / / make text in "my-div" red

$ ('P'). MakeTextRed () / / make all red Paragraphs

Creating a jQuery method

Syntax
jQuery.myMethod = function () {
/ / Code here
};
Example
jQuery.sayHelloWorld = function () {

alert ('Hello World');

};
/ / Example usage

$. SayHelloWorld () / / alerts "Hello World"

Options


Let your plugin more flexible and user friendly as possible, using options. The method $. Extend () takes two or more objects as arguments and combines their contents within the first object.

Example
A function that sets the text color (red by default).

jQuery.fn.makeTextColored = function (settings) {
var config = {
'Color': 'red'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

$ (This). Css ('color', config.color);

});

};

Now we can choose to use this function passing the parameter settings or not.

$ ('# My-div'). MakeTextColored () / / make red text (default)
$ ('# My-div'). MakeTextColored ('blue') / / make text blue

Compatibility

Because the variable $ can be used by other plugins, use a technique "disguised" to make your plugin compatible future.

(Function ($) {
$. Fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};
}) (JQuery);

We went to the jQuery function, and we can now use jQuery for any outfit you want. So, instead of the $, you could use any valid JavaScript variable name.

A checklist of the jQuery plugin
  • This is a list of important points to remember when developing a jQuery plugin ( the jQuery.com ).

  • Name your file jquery. [Insert the plugin name]. Js, eg. jquery.debug.js.

  • All new methods are attached to the object jQuery.fn object, and all functions to the jQuery object.

  • For internal methods, this is a reference to the current jQuery object.

  • Any methods or functions that you attach must have a semicolon (;) at the end - otherwise the code will break when compressed.

  • Your method must return the jQuery object, unless it was explicitly noted otherwise.

  • Use this.each to iterate over the current set of matching elements.

  • Always attach the plugin to jQuery instead of $, so that users can use a custom outfit via noConflict ().

Templates jQuery plugin

These are good code templates to begin developing a jQuery plugin.
Template Function

(Function ($) {
$. Fn.myPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

/ / Element-specific code here

});

};

}) (JQuery);

Template Method

(Function ($) {
$. MyPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
/ / Code here
return this;

};

}) (JQuery);

Example: jQuery Slideshow Plugin

I decided to use very simple examples so far that you might be familiar. The following example is a little more complex and can help you find your inspiration.

He uses the setInterval () together with the effects fadeOut () and fadeIn () jQuery to generate the cycle of any images within a HTML element.

Configuration

HTML

<div id="slideshow">
<img src="img/sample-image-1.png" alt="" />
<img src="img/sample-image-2.png" alt="" />
<img src="img/sample-image-3.png" alt="" />
<img src="img/sample-image-4.png" alt="" />
</ Div>

CSS

# Slideshow img {
display: none;
position: absolute;
}

JavaScript

(Function ($) {
$. SimpleSlideShow = function (selector, settings) {
/ / Settings
var config = {
'Delay': 2000,
'FadeSpeed': 500
};
if (settings) {$. extend (config, settings);}
/ / Variables

var obj = $ (selector);

var img = obj.children ('img');

var count = img.length;

var i = 0;
/ / Show first image

img.eq (0). show ();
/ / Run slideshow

setInterval (function () {

img.eq (i). fadeOut (config.fadeSpeed);

i = (i +1 == count)? 0: i +1;

img.eq (i). fadeIn (config.fadeSpeed);

}, Config.delay);
return this;

};

}) (JQuery);


Use

To activate the slideshow div # slideshow, we simply call it using the following JavaScript code:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow');
</ Script>

How do we allow the configuration to change the behavior of the slideshow, we could leave 5 seconds between the images and set the duration of the "fade" for 200 ms using:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow', {'delay': 5000, 'fadeSpeed': 200});
</ Script>



Thursday, August 4, 2011

5 sliders content for jQuery


Sliders are powerful tools to highlight content on a website. This article gives you five options jQuery plugins to implement this functionality.

Nivo Slider - Site | Demo


The strong Nivo Slider is a clean HTML for the initial structure of the slider. Only a div element with pictures and links is necessary. The main focus of this slider are images, but the HTML caption feature just enabling the display of any content.

Among the options offered are the speed and direction of the transition and callbakcs to perform actions after the start and loading of the slides. The plugin still has no more and no less than 16 choices of transition from one slide to another. Its compressed version has 15kb.

Coda Slider  - Site | Demo


This plugin has emerged based on the application site Slider Coda . Each element of the slide should be a div element with class "panel".

Coda Slider leaves something to be desired flexibility, offering few options but to display different types of content is most appropriate plugin.

Orbit  - Demo


Originally developed to be a slider images, Orbit also incorporating HTML content just as long as the content div element contains a background set.

Its compressed version is very light, and another differential 3.2kb cool Orbit is the application of a loader marking the transition time between slides.

Anything Slider  - Site | Demo


As the name suggests, the AnythingSlider accepts all and any type of content. In the words of the author, "the plugin is an attempt to gather all of the features found in other plugins of this type."

It is the most adptável plugin and integrates better with the rest of your application oferencendo own hashtags for each slide and the possibility of creating a direct link to a slide.

Moodular  - Demo


The neat thing is Moodular, as well as the name suggests, is modular, core, control and effects. So if you need a simple slider is not necessary to load the control module, for example.

The effects of Moodular module includes the popular "reflection" (the one from Apple) and the control has built in support for touchscreen applications, and keyboard navigation.


Monday, August 1, 2011

Creating a plugin for jQuery


Learn how to create a plug-in for jQuery from scratch - the basics, options, compatibility and examples.

The Basics
A plugin is written as a method or function.

Creating a jQuery function

Syntax
The function must return this.each (..) to maintain the ability to "chaining" - so that the function can be used with one or several objects jQuery.

jQuery.fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};


Example

jQuery.fn.makeTextRed = function () {
this.each return (function () {
$ (This). Css ('color', 'red');
});
};
/ / Example usage

$ ('# My-div'). MakeTextRed () / / make text in "my-div" red

$ ('P'). MakeTextRed () / / make all red Paragraphs

Creating a jQuery method

Syntax
jQuery.myMethod = function () {
/ / Code here
};
Example
jQuery.sayHelloWorld = function () {

alert ('Hello World');

};
/ / Example usage

$. SayHelloWorld () / / alerts "Hello World"

Options


Let your plugin more flexible and user friendly as possible, using options. The method $. Extend () takes two or more objects as arguments and combines their contents within the first object.

Example
A function that sets the text color (red by default).

jQuery.fn.makeTextColored = function (settings) {
var config = {
'Color': 'red'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

$ (This). Css ('color', config.color);

});

};

Now we can choose to use this function passing the parameter settings or not.

$ ('# My-div'). MakeTextColored () / / make red text (default)
$ ('# My-div'). MakeTextColored ('blue') / / make text blue

Compatibility

Because the variable $ can be used by other plugins, use a technique "disguised" to make your plugin compatible future.

(Function ($) {
$. Fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};
}) (JQuery);

We went to the jQuery function, and we can now use jQuery for any outfit you want. So, instead of the $, you could use any valid JavaScript variable name.

A checklist of the jQuery plugin
  • This is a list of important points to remember when developing a jQuery plugin ( the jQuery.com ).

  • Name your file jquery. [Insert the plugin name]. Js, eg. jquery.debug.js.

  • All new methods are attached to the object jQuery.fn object, and all functions to the jQuery object.

  • For internal methods, this is a reference to the current jQuery object.

  • Any methods or functions that you attach must have a semicolon (;) at the end - otherwise the code will break when compressed.

  • Your method must return the jQuery object, unless it was explicitly noted otherwise.

  • Use this.each to iterate over the current set of matching elements.

  • Always attach the plugin to jQuery instead of $, so that users can use a custom outfit via noConflict ().

Templates jQuery plugin

These are good code templates to begin developing a jQuery plugin.
Template Function

(Function ($) {
$. Fn.myPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

/ / Element-specific code here

});

};

}) (JQuery);

Template Method

(Function ($) {
$. MyPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
/ / Code here
return this;

};

}) (JQuery);

Example: jQuery Slideshow Plugin

I decided to use very simple examples so far that you might be familiar. The following example is a little more complex and can help you find your inspiration.

He uses the setInterval () together with the effects fadeOut () and fadeIn () jQuery to generate the cycle of any images within a HTML element.

Configuration

HTML

<div id="slideshow">
<img src="img/sample-image-1.png" alt="" />
<img src="img/sample-image-2.png" alt="" />
<img src="img/sample-image-3.png" alt="" />
<img src="img/sample-image-4.png" alt="" />
</ Div>

CSS

# Slideshow img {
display: none;
position: absolute;
}

JavaScript

(Function ($) {
$. SimpleSlideShow = function (selector, settings) {
/ / Settings
var config = {
'Delay': 2000,
'FadeSpeed': 500
};
if (settings) {$. extend (config, settings);}
/ / Variables

var obj = $ (selector);

var img = obj.children ('img');

var count = img.length;

var i = 0;
/ / Show first image

img.eq (0). show ();
/ / Run slideshow

setInterval (function () {

img.eq (i). fadeOut (config.fadeSpeed);

i = (i +1 == count)? 0: i +1;

img.eq (i). fadeIn (config.fadeSpeed);

}, Config.delay);
return this;

};

}) (JQuery);


Use

To activate the slideshow div # slideshow, we simply call it using the following JavaScript code:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow');
</ Script>

How do we allow the configuration to change the behavior of the slideshow, we could leave 5 seconds between the images and set the duration of the "fade" for 200 ms using:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow', {'delay': 5000, 'fadeSpeed': 200});
</ Script>



Sunday, July 31, 2011

5 sliders content for jQuery


Sliders are powerful tools to highlight content on a website. This article gives you five options jQuery plugins to implement this functionality.

Nivo Slider - Site | Demo


The strong Nivo Slider is a clean HTML for the initial structure of the slider. Only a div element with pictures and links is necessary. The main focus of this slider are images, but the HTML caption feature just enabling the display of any content.

Among the options offered are the speed and direction of the transition and callbakcs to perform actions after the start and loading of the slides. The plugin still has no more and no less than 16 choices of transition from one slide to another. Its compressed version has 15kb.

Coda Slider  - Site | Demo


This plugin has emerged based on the application site Slider Coda . Each element of the slide should be a div element with class "panel".

Coda Slider leaves something to be desired flexibility, offering few options but to display different types of content is most appropriate plugin.

Orbit  - Demo


Originally developed to be a slider images, Orbit also incorporating HTML content just as long as the content div element contains a background set.

Its compressed version is very light, and another differential 3.2kb cool Orbit is the application of a loader marking the transition time between slides.

Anything Slider  - Site | Demo


As the name suggests, the AnythingSlider accepts all and any type of content. In the words of the author, "the plugin is an attempt to gather all of the features found in other plugins of this type."

It is the most adptável plugin and integrates better with the rest of your application oferencendo own hashtags for each slide and the possibility of creating a direct link to a slide.

Moodular  - Demo


The neat thing is Moodular, as well as the name suggests, is modular, core, control and effects. So if you need a simple slider is not necessary to load the control module, for example.

The effects of Moodular module includes the popular "reflection" (the one from Apple) and the control has built in support for touchscreen applications, and keyboard navigation.