如何在猫头鹰旋转木马中添加随机动画效果
How to add random animation effect in owl carousel?
这是我对猫头鹰转盘的代码和效果
$(document).ready(function(){
$(".owl-carousel").owlCarousel({
autoplay:true,
autoplayTimeout:2000,
autoplayHoverPause:true,
dots: true,
merge:false,
loop:true,
items:1,
animateOut: 'bounce',
animateIn: 'zoomIn',
});
});
指示在这里只使用一个动画
animateOut: 'bounce',
animateIn: 'zoomIn',
我想在这里使用随机动画。我该怎么做?
试试这个:
function getRandomAnimation(){
var animationList = ['bounce', 'zoomIn'];
return animationList[Math.floor(Math.random() * animationList.length)];
}
$(document).ready(function(){
let props = {
autoplay:true,
autoplayTimeout:2000,
autoplayHoverPause:true,
dots: true,
merge:false,
loop:true,
items:1
};
props['animateOut'] = getRandomAnimation();
props['animateIn'] = getRandomAnimation();
$(".owl-carousel").owlCarousel(props);
});
@ykaragol的答案确实创建了随机属性,但仅在第一次加载时,像我一样,如果你也希望每次滑块更改时都有随机动画,那么在owl carousel库文件中进行以下更改(注意:我使用的是owl carousel v2.1.0,其他版本的代码可能不同):
找到this.core.settings.animateIn
,它应该在第一个的两个位置,代码如下:
var left,
clear = $.proxy(this.clear, this),
previous = this.core.$stage.children().eq(this.previous),
next = this.core.$stage.children().eq(this.next),
incoming = this.core.settings.animateIn,
outgoing = this.core.settings.animateOut;
继续,然后添加这个代码:
if(incoming.constructor == Array){
incoming = incoming[Math.floor(Math.random() * incoming.length)];
}
if(outgoing.constructor == Array){
outgoing = outgoing[Math.floor(Math.random() * outgoing.length)];
}
现在在文件中再次找到this.core.settings.animateIn
,它将围绕这样的代码:
$(e.target).css( { 'left': '' } )
.removeClass('animated owl-animated-out owl-animated-in')
.removeClass(this.core.settings.animateIn);
.removeClass(this.core.settings.animateOut);
this.core.onTransitionEnd();
将其更改为:
var incoming = this.core.settings.animateIn;
var outgoing = this.core.settings.animateOut
if(incoming.constructor == Array){
for (var i = incoming.length - 1; i >= 0; i--) {
$(e.target).removeClass(incoming[i]);
}
}else{
$(e.target).removeClass(this.core.settings.animateIn);
}
if(outgoing.constructor == Array){
for (var i = outgoing.length - 1; i >= 0; i--) {
$(e.target).removeClass(outgoing[i]);
}
}else{
$(e.target).removeClass(this.core.settings.animateOut);
}
$(e.target).css( { 'left': '' } )
.removeClass('animated owl-animated-out owl-animated-in');
this.core.onTransitionEnd();
现在,这应该只需通过传递一个动画数组来动画属性,如下所示:
$owlSty1.owlCarousel({
animateOut: ['slideOutDown', 'zoomOut'],
animateIn: ['flipInX', 'zoomIn'],
loop: true,
nav: false,
items: 1,
dots: true,
responsive: false,
autoplay: true,
autoplayTimeout: 6000,
rtl: directionRTL
});
相关文章:
- 如何在猫头鹰旋转木马中添加随机动画效果
- 如何使用动画css来显示随机赞美
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 按随机顺序对元素进行动画处理
- 尝试制作带有动画的随机字符串生成器
- CSS 关键帧动画的随机“起点”
- jQuery - 被赋予随机 ID 的动画对象
- 随机为图像分配一个值,然后传递动画
- 连续随机调整图像大小的动画效果
- javascript动画ScrollTop随机工作
- 使用javascript/jquery随机化CSS3动画时间
- 使用setTimeout函数&随机函数.以设置类元素的动画
- 如何在动画完成后随机改变位置
- jQuery动画,使png出现在屏幕上的随机点
- 使用原型继承模式的随机移动动画
- Kineticjs:画布中图像的随机移动动画
- 圆圈动画随机颜色
- CSS随机动画
- 动画随机平铺背景
- 改变css背景与jquery的动画随机背景