Monday, November 21, 2011

produce your own features in JQuery


This message is part of the learning jQuery. When I was new to learn jQuery, I was faced with a lot of problems by creating its own jQuery functions. Because I want to create a function in one place and use it throughout the call directly to JqueryObject.function (). When we got the experience, then I started to write your own functions and use jQuery in my projects.

Syntax of how we can create a new function is as below.
jQuery.fn.myownfunction = function() {
var currentObject = $(this) ; //currentObject holds the current object.
};

Example function for centering an element is shown below.

jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / $(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / $(window).scrollLeft() + "px");
return this;
}

How to call it?
$("#popupDiv").center();

SPONSORS:

No comments:

Post a Comment