码迷,mamicode.com
首页 > Web开发 > 详细

一个不错的jquery插件模版

时间:2015-03-16 23:01:09      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

 

  • 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 () {
    
    });

一个不错的jquery插件模版

标签:

原文地址:http://www.cnblogs.com/liuwenbohhh/p/4342944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!