Hiv*_*ve7 2 javascript jquery setinterval
我有一个 jQuery 幻灯片插件,我正在制作它,虽然它有一个setInterval()内部没有被调用,但如果我移动setInterval()它外部的内容,那么它虽然只运行一次,但它可以工作。
var gap = 3;
var duration = 0.5;
(function ($) {
$.fn.slideshow = function () {
return this.each(function () {
g = gap * 1000;
d = duration * 1000;
$(this).children().css({
'position': 'absolute',
'display': 'none'
});
$(this).children().eq(0).css({
'display': 'block'
});
setInterval(function () {
slide();
}, g);
function slide() {
$(this)
.children()
.eq(0)
.fadeOut(d)
.next()
.fadeIn()
.end()
.appendTo($(this).children().eq(0).parent());
}
});
};
})(jQuery);
$('.slideshow').slideshow();
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class='slideshow'>
<a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的插件的小提琴:
问题是函数this内部slider没有指向你认为它指向的对象。
setInterval($.proxy(function () {
slide.call(this);
}, this), g);
Run Code Online (Sandbox Code Playgroud)
演示:小提琴
或更好
setInterval($.proxy(slide, this), g);
Run Code Online (Sandbox Code Playgroud)
演示:小提琴
| 归档时间: |
|
| 查看次数: |
887 次 |
| 最近记录: |