2014-03-31 72 views
1

我正在使用jQuery UI模板编写我的第一个jQuery插件,并试图实例化同一插件的两个实例 - 但具有不同的选项。在一个页面上我的jQuery插件的多个实例

我可以做一些帮助,请!

的Javascript:

(function ($) { 
// ********************************** 
// ***** Start: Private Members ***** 
var pluginName = 'onFancyLinks', 
    version = '1.0'; 
// ***** Fin: Private Members ***** 
// ******************************** 

// ********************************* 
// ***** Start: Public Methods ***** 
var methods = { 
    init: function (options) { 
     //"this" is a jquery object on which this plugin has been invoked. 
     return this.each(function (index) { 
      var $this = $(this); 
      var data = $this.data(pluginName); 
      // If the plugin hasn't been initialized yet 
      if (!data) { 
       var settings = { 
        lineColor: '#fff', 
        lineWidth: 1, 
        wrapperClass: 'fancy-link', 
        linesClass: 'line', 
        transDuration: '0.7s' 
       }; 
       if (options) { 
        $.extend(true, settings, options); 
       } 

       $this.data(pluginName, { 
        target: $this, 
        settings: settings 
       }); 
      } 
     }); 
    }, 
    wrap: function() { 
     return this.each(function() { 
      var $this = $(this), 
       data = $this.data(pluginName), 
       opts = data.settings, 
       //wrapping div 
       wrapper = '<div class="' + opts.wrapperClass + '"></div>', 
       lines = { 
        top: '<div class="' + opts.linesClass + ' line-top">&nbsp;</div>', 
        right: '<div class="' + opts.linesClass + ' line-right">&nbsp;</div>', 
        bottom: '<div class="' + opts.linesClass + ' line-bottom">&nbsp;</div>', 
        left: '<div class="' + opts.linesClass + ' line-left">&nbsp;</div>' 
       }; 

      $this.wrap(wrapper); 
      $('.' + opts.wrapperClass).append(lines.top, lines.right, lines.bottom, lines.left); 

      //setup transition duration of lines animation 
      $('.' + opts.wrapperClass + ' .' + opts.linesClass).css({ 
       'transition-duration': opts.transDuration, 
       backgroundColor: opts.lineColor, 
       borderWidth: opts.lineWidth 
      }); 
     }); 
    } 
}; 
// ***** Fin: Public Methods ***** 
// ******************************* 

// ***************************** 
// ***** Start: Supervisor ***** 
$.fn[pluginName] = function (method) { 
    if (methods[method]) { 
     return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else if (typeof method === 'object' || !method) { 
     return methods.init.apply(this, arguments); 
    } else { 
     $.error('Method ' + method + ' does not exist in jQuery.' + pluginName); 
    } 
}; 
// ***** Fin: Supervisor ***** 
// *************************** 
})(jQuery); 


$(function() { 

    var v1 = $('#flink2').onFancyLinks({ 
     lineColor: '#f00', 
     lineWidth: 2, 
     transDuration: '.4s' 
    }); 

    var v2 = $('#flink').onFancyLinks({ 
     lineColor: '#ff0', 
     lineWidth: 1, 
     transDuration: '.7s' 
    }); 


    v1.onFancyLinks('wrap'); 
    v2.onFancyLinks('wrap'); 

}); 

HTML:

<a id="flink2" href="http://www.google.co.uk">View Google</a> 
<a id="flink" href="http://www.visarc.co.uk">View Visarc Site</a> 

这里是我的小提琴:http://jsfiddle.net/owennicol/xhuxk/14/

我敢肯定,这是一些简单的我失踪...

+0

检查我的答案 - 和补丁版本。作为一个旁注 - 这是一件非常令人印象深刻的工作,也很好地构建了它的问题。如果这里至少有第十个问题看起来像你的问题,那本来会是一个好得多的地方。 ) – raina77ow

+0

@ raina77ow,FYI上面使用的模式最初是以jQuery插件的形式提供的。虽然它没有解释就突然从网站上消失了,但它仍然是一个很棒的插件;举个例子,我仍然在使用它。 –

回答

0

非常漂亮的插件,但它有一个微妙的逻辑错误。以下是关键部分patched version

var $wrapper = $this.wrap(wrapper).parent(); 
$wrapper.append(lines.top, lines.right, lines.bottom, lines.left); 

//setup transition duration of lines animation 
$wrapper.find('.' + opts.linesClass).css({ 
    'transition-duration': opts.transDuration, 
    backgroundColor: opts.lineColor, 
    borderWidth: opts.lineWidth 
}); 

请参阅?您不再分别浏览整个DOM,分别为$('.' + opts.wrapperClass)$('.' + opts.wrapperClass + ' .' + opts.linesClass) - 但是只能在为特定jQuery元素(由插件处理)创建的包装中扫描。

而这正是是错误的原始版本:即使options设定正确(这很容易检查 - 只需添加console.log(options)wrap方法),css是在整个DOM这些线元素应用。

+0

非常感谢,那正是我所错过的。很高兴像你这样的人能够帮助别人,谢谢你,我真的很感激。 – owennicol

相关问题