Here is the demo code to create simple plugin in jquery :-
$.fn.greenify = function () {
this.css("background-color", "green");
this.css("height", "100px");
this.css("width", "100px");
this.css("position", "relative");
this.css("float", "left");
return this;
};
Above code is used to create one plugin function what can be used anywhere in same project or in different project and can be used in following way:
$(document).ready(function () {
$("div div").greenify().addClass("greeifiyclass");
});
Here is one complete demo that shows different way to create plugin :
<script type="text/javascript">
(function ($) {
//user defined functions
$.fn.greenify = function () {
this.css("background-color", "green");
this.css("height", "100px");
this.css("width", "100px");
this.css("position", "relative");
this.css("float", "left");
return this;
};
$.fn.yellowfy = function () {
this.css("background-color", "yellow");
};
var i = 0;
$.fn.looplogic = function () {
if (i == 0) {
this.css("background-color", "yellow");
this.css("width", "180px");
this.css("height", "100px")
i = 1;
}
else {
this.css("background-color", "red");
this.css("width", "180px");
this.css("height", "100px")
i = 0;
}
};
function apply() {
$(".change").looplogic();
};
//apply user defined functions to respective elements
$(document).ready(function () {
$("div div").greenify().addClass("greeifiyclass");
$("a").yellowfy();
setInterval(apply, 1000);
$("div").click(function () {
location.href = "http://solvecode.blogspot.in";
});
});
}(jQuery));
</script>
HTML CODE
<div>
<div></div>
<div id="change" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div1" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div2" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div3" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div4" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div5" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div6" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div7" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div8" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div9" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div32" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div10" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div11" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div12" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div13" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div14" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div15" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div16" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div17" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div18" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div19" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div31" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div20" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div21" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div22" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div23" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div24" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div25" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div26" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div27" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div28" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div29" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div30" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div33" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div34" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div35" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div36" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div37" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div38" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div39" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div40" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div41" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div42" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div43" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div44" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div45" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div46" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div47" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div48" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div49" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div50" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div51" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div52" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div53" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div54" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div55" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div56" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div57" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div58" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div59" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div60" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div61" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div62" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div63" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div64" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div65" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div66" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div67" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div68" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div69" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div70" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div71" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div72" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div73" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div74" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div75" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div76" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div77" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div78" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div79" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div80" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div81" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div82" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div83" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div84" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div85" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div86" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div87" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div88" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div89" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div90" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div91" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div92" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div93" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div94" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div95" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div96" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div97" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div98" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<a href="http://solvecode.blogspot.in">click me</a>
$.fn.greenify = function () {
this.css("background-color", "green");
this.css("height", "100px");
this.css("width", "100px");
this.css("position", "relative");
this.css("float", "left");
return this;
};
Above code is used to create one plugin function what can be used anywhere in same project or in different project and can be used in following way:
$(document).ready(function () {
$("div div").greenify().addClass("greeifiyclass");
});
Here is one complete demo that shows different way to create plugin :
<script type="text/javascript">
(function ($) {
//user defined functions
$.fn.greenify = function () {
this.css("background-color", "green");
this.css("height", "100px");
this.css("width", "100px");
this.css("position", "relative");
this.css("float", "left");
return this;
};
$.fn.yellowfy = function () {
this.css("background-color", "yellow");
};
var i = 0;
$.fn.looplogic = function () {
if (i == 0) {
this.css("background-color", "yellow");
this.css("width", "180px");
this.css("height", "100px")
i = 1;
}
else {
this.css("background-color", "red");
this.css("width", "180px");
this.css("height", "100px")
i = 0;
}
};
function apply() {
$(".change").looplogic();
};
//apply user defined functions to respective elements
$(document).ready(function () {
$("div div").greenify().addClass("greeifiyclass");
$("a").yellowfy();
setInterval(apply, 1000);
$("div").click(function () {
location.href = "http://solvecode.blogspot.in";
});
});
}(jQuery));
</script>
HTML CODE
<div>
<div></div>
<div id="change" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div1" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div2" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div3" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div4" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div5" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div6" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div7" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div8" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div9" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div32" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div10" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div11" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div12" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div13" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div14" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div15" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div16" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div17" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div18" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div19" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div31" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div20" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div21" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div22" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div23" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div24" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div25" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div26" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div27" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div28" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div29" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div30" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div33" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div34" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div35" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div36" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div37" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div38" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div39" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div40" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div41" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div42" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div43" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div44" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div45" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div46" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div47" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div48" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div49" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div50" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div51" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div52" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div53" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div54" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div55" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div56" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div57" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div58" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div59" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div60" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div61" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div62" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div63" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div64" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div65" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div66" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div67" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div68" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div69" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div70" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div71" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div72" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div73" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div74" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div75" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div76" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div77" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div78" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div79" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div80" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div81" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div82" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div83" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div84" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div85" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div86" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div87" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<div>
<div></div>
<div id="Div88" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div89" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div90" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div91" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div92" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
</div>
<div>
<div id="Div93" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div94" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div95" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div96" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div97" class="change">SOLVECODE.BLOGSPOT.IN</div>
<div></div>
<div id="Div98" class="change">SOLVECODE.BLOGSPOT.IN</div>
</div>
<a href="http://solvecode.blogspot.in">click me</a>
You can see demo here CLICK HERE
Comments
Post a Comment