gpt4 book ai didi

javascript - 不用递归制作 slider

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:10 26 4
gpt4 key购买 nike

给定以下 jsFiddle,如何在不构建堆栈的情况下实现与我所做的相同的效果?

http://jsfiddle.net/YWMcy/1/

我试过这样做:

jQuery(document).ready(function () {
'use strict';
(function ($) {

function validateOptions(options) {
if (typeof(options.delay) == typeof(0)) {
$.error('Delay value must an integer.');
return false;
} else if (options.delay < 0) {
$.error('Delay value must be greater than zero.');
return false;
}

if (typeof(options.direction) == typeof('')) {
$.error('Direction value must be a string.');
return false;
} else if (!(options.direction in ['left', 'right', 'up', 'down'])) {
$.error('Direction value must be "left", "right", "up", or "down".');
return false;
}

if (typeof(options.easing) == typeof('')) {
$.error('Easing value must be a string.');
return false;
}

if (typeof(options.selector) == typeof('')) {
$.error('Selector value must be a string.');
return false;
}

if (options.transition < 0) {
$.error('Transition value must be greater than zero.');
return false;
}
return true;
}

var methods = {
init: function (options) {

return this.each(function () {

var settings = {
delay: 5000,
direction: 'left',
easing: 'swing',
selector: '*',
transition: 3000
};

if (options) {
$.extend(settings, options);
}

$(this).css({
overflow: 'hidden',
position: 'relative'
});

var styles = {
left: 0,
position: 'absolute',
top: 0
};

switch (settings.direction) {
case 'left':
styles.left = $(this).width() + 'px';
break;
case 'right':
styles.left = -$(this).width() + 'px';
break;
case 'up':
styles.top = $(this).height() + 'px';
break;
case 'down':
styles.top = -$(this).height() + 'px';
break;
default:
jQuery.error('Direction ' + settings.direction + ' is not valid for jQuery.fn.cycle');
break;
}

$(this).children(settings.selector).css(styles).first().css({
left: 0,
top: 0
});

if ($(this).children(settings.selector).length > 1) {
$(this).cycle('slide', settings);
}
});
},

slide: function (options) {
return this.each(function () {

var settings = {
delay: 5000,
direction: 'left',
easing: 'swing',
selector: '*',
transition: 3000
}, animation, property, value;

if (options) {
$.extend(settings, options);
}

switch (settings.direction) {
case 'left':
animation = {left: '-=' + $(this).width()};
property = 'left';
value = $(this).width();
break;
case 'right':
animation = {left: '+=' + $(this).width()};
property = 'left';
value = -$(this).width();
break;
case 'up':
animation = {top: '-=' + $(this).height()};
property = 'top';
value = $(this).height();
break;
case 'down':
animation = {top: '+=' + $(this).height()};
property = 'top';
value = -$(this).height();
break;
default:
jQuery.error('Direction ' + settings.direction + ' is not valid for jQuery.fn.cycle');
break;
}

$(this).children(settings.selector + ':first-child').each(function () {
$(this).delay(settings.delay);
$(this).animate(
animation,
settings.transition,
settings.easing,
function () {
$(this).css(property, value);
}
);
});

$(this).append($(this).children(settings.selector + ':first-child').detach());

$(this).children(settings.selector + ':first-child').each(function () {
$(this).delay(settings.delay);
$(this).animate(
animation,
settings.transition,
settings.easing,
function () {
$(this).parent().cycle('slide', settings);
}
);
});
});
}
};

jQuery.fn.cycle = function (method, options) {
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 on jQuery.fn.cycle');
}
};
}(jQuery));

jQuery('.slider').cycle();

});

但是 each() 方法不考虑在循环期间添加的节点。

最佳答案

您可以通过 setInterval() 启动您的 _cycle() 函数,以定期更新 slider :

setInterval(function() {
_cycle2(slider, transition_duration, easing);
}, delay_duration);

请注意,我将您原来的 _cycle() 函数重命名为 _cycle2(),并删除了 delay_duration 参数。你可以see a working demo here .

关于javascript - 不用递归制作 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7211815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com