!83 轮播图组件新增过渡动画效果,修复autoplay为false时仍然自动播放的问题

Merge pull request !83 from 0o张不歪o0/next
This commit is contained in:
就眠儀式 2022-06-22 16:44:59 +00:00 committed by Gitee
commit a3866fc972
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 150 additions and 27 deletions

View File

@ -38,6 +38,7 @@
background-color: #f8f8f8; background-color: #f8f8f8;
transition-duration: 0.3s; transition-duration: 0.3s;
-webkit-transition-duration: 0.3s; -webkit-transition-duration: 0.3s;
overflow: hidden;
} }
.layui-carousel-updown > * { .layui-carousel-updown > * {
@ -205,6 +206,7 @@
right: 20px; right: 20px;
width: auto; width: auto;
height: auto; height: auto;
transform: translateY(-50%);
} }
.layui-carousel[lay-anim="updown"] .layui-carousel-ind ul { .layui-carousel[lay-anim="updown"] .layui-carousel-ind ul {

View File

@ -53,6 +53,15 @@ const active = computed({
}, },
}); });
const anim = computed({
get() {
return props.anim;
},
set() {
},
});
const emit = defineEmits(["update:modelValue", "change"]); const emit = defineEmits(["update:modelValue", "change"]);
const change = function (id: any) { const change = function (id: any) {
@ -85,6 +94,7 @@ watch(
provide("active", active); provide("active", active);
provide("slotsChange", slotsChange); provide("slotsChange", slotsChange);
provide("anim", anim);
const sub = () => { const sub = () => {
for (var i = 0; i < childrens.value.length; i++) { for (var i = 0; i < childrens.value.length; i++) {
@ -128,6 +138,7 @@ const autoplay = () => {
watch( watch(
() => props.autoplay, () => props.autoplay,
() => { () => {
if(props.autoplay)
setInterval(autoplay, props.interval); setInterval(autoplay, props.interval);
}, },
{ immediate: true } { immediate: true }

View File

@ -5,7 +5,7 @@ export default {
</script> </script>
<script setup lang="ts"> <script setup lang="ts">
import { inject, Ref } from "vue"; import { inject, Ref, computed, ref, VNodeChild } from "vue";
const props = defineProps<{ const props = defineProps<{
id: string; id: string;
@ -14,10 +14,90 @@ const props = defineProps<{
const active = inject("active"); const active = inject("active");
const slotsChange: Ref<boolean> = inject("slotsChange") as Ref<boolean>; const slotsChange: Ref<boolean> = inject("slotsChange") as Ref<boolean>;
slotsChange.value = !slotsChange.value; slotsChange.value = !slotsChange.value;
const anim = inject("anim");
const item = ref();
const getStyle = computed<any>(() => {
if (item.value) {
let allChild = item.value.parentNode.children;
let allChildNum = allChild.length;
//activeIndex
let activeIndex,
currentIndex = 0;
for (let index = 0; index < allChild.length; index++) {
const element = allChild[index];
// @ts-ignore
if (element.__vnode.props["data-id"] === active.value) {
activeIndex = index;
}
if (element.__vnode.props["data-id"] === props.id) {
currentIndex = index;
}
}
// @ts-ignore
let prevIndex = activeIndex > 0 ? activeIndex - 1 : allChildNum - 1;
// @ts-ignore
let nextIndex = activeIndex + 1 < allChildNum ? activeIndex + 1 : 0;
// @ts-ignore
let animation = anim.value;
//
if (activeIndex === currentIndex) {
if (animation === "updown") {
return {
transform: "translateY(0)",
};
} else if (animation.includes("fade")) {
return {
opacity: 1,
};
} else {
return {
transform: "translateX(0)",
};
}
}
if (prevIndex === currentIndex) {
if (animation === "updown") {
return {
transform: "translateY(-100%)",
};
} else if (animation.includes("fade")) {
return {
opacity: 0,
};
} else {
return {
transform: "translateX(-100%)",
};
}
}
if (nextIndex === currentIndex) {
if (animation === "updown") {
return {
transform: "translateY(100%)",
};
} else if (animation.includes("fade")) {
return {
opacity: 0,
};
} else {
return {
transform: "translateX(100%)",
};
}
}
return {
display: "none",
};
}
});
</script> </script>
<template> <template>
<li v-if="active === id"> <li ref="item" :style="getStyle" :data-id="id">
<slot></slot> <slot></slot>
</li> </li>
</template> </template>

View File

@ -9,16 +9,16 @@
<template> <template>
<lay-carousel v-model="active1"> <lay-carousel v-model="active1">
<lay-carousel-item id="1"> <lay-carousel-item id="1">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目一</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#009688;">条目一</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="2"> <lay-carousel-item id="2">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目二</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#5FB878;">条目二</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="3"> <lay-carousel-item id="3">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目三</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FFB800;">条目三</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="4"> <lay-carousel-item id="4">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目四</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FF5722;">条目四</div>
</lay-carousel-item> </lay-carousel-item>
</lay-carousel> </lay-carousel>
</template> </template>
@ -40,26 +40,56 @@ export default {
::: :::
::: title 不同方向 ::: title 不同方向与不同切换动画
::: :::
::: demo ::: demo 通过 `anim` 属性来控制切换的放向与动画,支持 `default`左右切换(默认)、`updown`上线切换、`fade`渐隐渐显切换
<template> <template>
<lay-carousel v-model="active2" anim="updown"> <div style="display:flex;justify-content: space-around;flex-wrap:wrap">
<lay-carousel v-model="activeAnmi1" anim="updown" style="display:inline-block;width:32%" :autoplay="true">
<lay-carousel-item id="1"> <lay-carousel-item id="1">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目一</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#009688;">条目一</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="2"> <lay-carousel-item id="2">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目二</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#5FB878;">条目二</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="3"> <lay-carousel-item id="3">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目三</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FFB800;">条目三</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="4"> <lay-carousel-item id="4">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目四</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FF5722;">条目四</div>
</lay-carousel-item> </lay-carousel-item>
</lay-carousel> </lay-carousel>
<lay-carousel v-model="activeAnmi2" style="width:32%">
<lay-carousel-item id="1">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#009688;">条目一</div>
</lay-carousel-item>
<lay-carousel-item id="2">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#5FB878;">条目二</div>
</lay-carousel-item>
<lay-carousel-item id="3">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FFB800;">条目三</div>
</lay-carousel-item>
<lay-carousel-item id="4">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FF5722;">条目四</div>
</lay-carousel-item>
</lay-carousel>
<lay-carousel v-model="activeAnmi3" anim="fade" style="width:32%;">
<lay-carousel-item id="1">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#009688;">条目一</div>
</lay-carousel-item>
<lay-carousel-item id="2">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#5FB878;">条目二</div>
</lay-carousel-item>
<lay-carousel-item id="3">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FFB800;">条目三</div>
</lay-carousel-item>
<lay-carousel-item id="4">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FF5722;">条目四</div>
</lay-carousel-item>
</lay-carousel>
</div>
</template> </template>
<script> <script>
@ -67,11 +97,11 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const activeAnmi1 = ref("1")
const active2 = ref("1") const activeAnmi2 = ref("1")
const activeAnmi3 = ref("1")
return { return {
active2 activeAnmi1,activeAnmi2,activeAnmi3,activeAnmi4
} }
} }
} }
@ -79,7 +109,7 @@ export default {
::: :::
::: title 控制位置 ::: title 控制位置
::: :::
::: demo ::: demo
@ -87,16 +117,16 @@ export default {
<template> <template>
<lay-carousel v-model="active3" indicator="outside"> <lay-carousel v-model="active3" indicator="outside">
<lay-carousel-item id="1"> <lay-carousel-item id="1">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目一</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#009688;">条目一</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="2"> <lay-carousel-item id="2">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目二</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#5FB878;">条目二</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="3"> <lay-carousel-item id="3">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目三</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FFB800;">条目三</div>
</lay-carousel-item> </lay-carousel-item>
<lay-carousel-item id="4"> <lay-carousel-item id="4">
<div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#79C48C;">条目四</div> <div style="color: white;text-align: center;width:100%;height:300px;line-height:300px;background-color:#FF5722;">条目四</div>
</lay-carousel-item> </lay-carousel-item>
</lay-carousel> </lay-carousel>
</template> </template>
@ -140,10 +170,10 @@ export default {
const active4 = ref("1") const active4 = ref("1")
const arrays = ref([ const arrays = ref([
{id: "1", text: ""}, {id: "1", text: "1"},
{id: "2", text: ""}, {id: "2", text: "2"},
{id: "3", text: ""}, {id: "3", text: "3"},
{id: "4", text: ""} {id: "4", text: "4"}
]) ])
return { return {
@ -165,7 +195,7 @@ export default {
| 属性 | 描述 | 类型 |类型 |可选值 | | 属性 | 描述 | 类型 |类型 |可选值 |
| --------- | ------------ |--------------| --------------- | -------------------------| | --------- | ------------ |--------------| --------------- | -------------------------|
| v-model | 当前激活项 | `number` | -- | -- | | v-model | 当前激活项 | `number` | -- | -- |
| anim | 切换方向 | `string` | `default` | `default` `updown` | | anim | 切换方向 | `string` | `default` | `default` `updown` `fade` |
| indicator | 控制器位置 | `string` | `inside` |`inside` `outside` `none` | | indicator | 控制器位置 | `string` | `inside` |`inside` `outside` `none` |
| arrow | 切换按钮状态 | `string` | `hover` |`hover` `always` `none` | | arrow | 切换按钮状态 | `string` | `hover` |`hover` `always` `none` |
| autoplay | 自动播放 | `boolean` | `true` | `true` `false` | | autoplay | 自动播放 | `boolean` | `true` | `true` `false` |