This code snippet for Create JQuery Plugin.
(function ($)
{
$.fn.changebg = function (options)
{
var settings = $.extend(
{
speed: "1000",
color: "#556b2f",
backgroundColor: "white",
width: "200px"
}, options);
return this.css(
{
color: settings.color,
backgroundColor: settings.backgroundColor,
width: settings.width
});
};
$.fn.moveright = function (options)
{
var settings = $.extend(
{
position: "200px"
}, options);
return this.css(
{
'margin-left': settings.position,
'-webkit-transition': 'margin-left 2s',
transition: 'margin-left 2s'
});
return this;
};
}(jQuery));
Save The above as "
myplugin.js",
<html>
<head>
<title>My jQuery Plugin</title>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="myplugin.js"></script>
<script>
$(document)
.ready(function ()
{
$(".animate")
.click(function ()
{
$(this)
.changebg(
{
backgroundColor: "green",
color: "white"
})
.moveright(
{
position: "300px"
})
});
$(".animate1")
.click(function ()
{
$(this)
.moveright(
{
position: "500px"
})
});
});
</script>
<style type="text/css">
.animate {
height: 100px;
width: 100px;
background-color: red;
position: relative;
}
.animate1 {
height: 200px;
width: 200px;
background-color: yellow;
position: relative;
}
</style>
</head>
<body>
<div class="animate"> my Plugin Animation </div>
<div class="animate1"> my Plugin Animation </div>
</div>
</body>
</html>