用这个把头发拉出来。我正在为jQuery编写一个轻量级的滑块插件(我不打算发布,只是为了保持这个网站构建的轻量级)。
我希望能够更新设置,例如什么时候自动播放,什么时候不播放。看起来很简单,但似乎没有。
$.fn.slider = function(options) {
var settings = $.extend({
autoplay: 4000,
pause: false
}, options);
console.log(settings.pause);
this.each(function() {
if(!$(this).data("init")) {
var $slider = $(this);
var $slide = $(".slide", $slider);
var $active = $slide.first().addClass("active");
var interval;
var animation = function(obj) {
$slide.removeClass("active");
obj.addClass("active");
}
var autoplay = function() {
interval = setInterval(function() {
$slider.trigger("next");
}, settings.autoplay);
}
autoplay();
$slider.on("next", function() {
$active = $active.next(".slide").length ? $active.next(".slide") : $slide.first();
animation($active);
});
$slider.on("prev", function() {
$active = $active.prev(".slide").length ? $active.prev(".slide") : $slide.last();
animation($active);
});
$slider.on("mouseover", function() {
console.log(settings.pause);
if(settings.pause) clearInterval(interval);
});
$slider.on("mouseout", function() {
clearInterval(interval);
autoplay();
});
$slider.data("init", true);
};
});
}
我正在通过$(".intro [slider]").slider({pause: true});
更新代码但是,第一个console.log报告为true,但是在悬停时发生的是报告错误?
我不明白为什么它存储变量。