pageplugin.js
(function ($) {
$.PagePlugin = function (obj, opt) {
var options = $.extend({}, $.PagePlugin.defaults),
docOffset,
_ua = navigator.userAgent.toLowerCase(),
is_msie = /msie/.test(_ua),
ie6mode = /msie [1-6]\./.test(_ua);
if (typeof (obj) !== ‘object‘) {
obj = $(obj)[0];
}
if (typeof (opt) !== ‘object‘) {
opt = {};
}
function setOptions(opt) {
if (typeof (opt) !== ‘object‘) opt = {};
options = $.extend(options, opt);
}
//自定义方法,需要在下面api声明
function setImage(src, callback) {
if (typeof (callback) == ‘function‘) {
callback.call(api);
}
}
setOptions(opt);
var $orig = $(obj);
//调用内部方法
ScoreManager.getBo();
ScoreManager.getCo();
//scoreManager,相当于自定义类
var ScoreManager = (function () {
function getScore1(){
alert("key1");
}
function getScore2(){
alert("key2");
}
return {
getBo: getScore1,
getCo: getScore2
};
}());
//定义api
var api = {
setImage: setImage,
focus:ScoreManager.getBo,
getBounds: function () {
return [1, 2];
}
};
$orig.data(‘PagePlugin‘, api);
return api;
}
//设置默认属性
$.PagePlugin.defaults = {
marginT: 0,
marginL: 0
};
}(jQuery));
如何使用?index.js
var api = null;
//这里"#page_plugins"我定义为传入控件的内容,可能为id,亦可能class。
api = $.PagePlugin("#page_plugins", {
marginT: 10,
marginL: 20
});
api.getBounds();
api.setImage("h1", function () {
});
原文:http://www.cnblogs.com/liuwenbohhh/p/4342944.html