【发布时间】:2012-05-21 00:49:41
【问题描述】:
我发现了这个很棒的小 jquery 图像滑块 sn-p:
var x = 2;
// function to switch the images.
function slideSwitch() {
var m = 5;
x += 1;
if (x > m) {
x = 1
}
$(".slideshow ul li:nth-child(n)").animate({opacity:0});
$(".slideshow ul li:nth-child(" + (x) + ")").animate({opacity:1});
}
$(document).ready(function() {
setInterval( "slideSwitch()", 5000 );
});
并且一直在尝试将其转换为插件以便能够多次使用它,但我没有任何运气......请帮助。
我对插件的尝试:(不起作用)
(function ($) {
$.fn.slidr = function (opts) {
var def = {
imgs : 4,
sid : 'slidr'
},
opts = $.extend({}, def, opts);
var nxt = 2;
this.each(function () {
var nxt += 1;
if (nxt > opts.imgs) {
nxt = 1;
}
$('.'+ opts.sid + ' ul li:nth-child(n)').animate({opacity:0});
$('.'+ opts.sid + ' ul li:nth-child(' + (nxt) + ')').animate({opacity:1});
});
return this;
}
})(jQuery);
$(document).ready(function() {
setInterval( $('#content').slidr(), 2000);
});
原始sn-p:http://jsfiddle.net/8T7nX 插件尝试不起作用:http://jsfiddle.net/8T7nX/1
【问题讨论】:
-
一个 JSFiddle 示例在这里会有所帮助。
-
这个
setInterval( $('#content').slidr(), 2000)不起作用。你需要一个匿名函数setInterval( function() { $('#content').slidr() }, 2000) -
@Will Demaine:原始 sn-p:jsfiddle.net/8T7nX ...插件尝试不起作用:jsfiddle.net/8T7nX/1
-
@RoKo:谢谢,但原来的 sn-p 工作正常,我只是想把它变成一个插件,以便更好地多用。
标签: jquery jquery-plugins slider