jQuery UI 템플릿을 사용하여 첫 번째 jQuery 플러그인을 작성하고 있는데 동일한 플러그인의 두 인스턴스를 인스턴스화하려고했지만 다른 옵션이 있습니다.하나의 페이지에 여러 개의 jQuery 플러그인 인스턴스가 있습니다.
나는 약간 도움을 함께 할 수 있었다!
자바 스크립트 :
(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"> </div>',
right: '<div class="' + opts.linesClass + ' line-right"> </div>',
bottom: '<div class="' + opts.linesClass + ' line-bottom"> </div>',
left: '<div class="' + opts.linesClass + ' line-left"> </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/
내 답변 - 및 패치 된 버전을 확인하십시오. sidenote로 - 그것은 작품의 매우 인상적인 조각이고, 멋지게 그것 주위에 질문을 너무 지었다. 여기 적어도 10 번째 질문이 너처럼 보였다면 훨씬 더 나은 곳이었을거야.) – raina77ow
@ raina77ow, FYI 위의 패턴은 원래 jQuery 사이트의 샘플 플러그인 형태로 제공되었습니다. 설명없이 사이트에서 갑자기 사라졌지만 여전히 훌륭한 플러그인입니다. 나는 그것을 위해 아직도 하나를 사용한다. –