实现淡入淡出缩放轮播图效果

问题描述

最近项目中要求实现淡入淡出并且还要有缩放效果,在此记录下代码

代码实现:

html:

    <div id="app">
        <div id="main" :style="{'animation-duration': imgs.length*seconds+'s'}">
            <template v-for="(img, index) in imgs">
                <img :style="{'animation-duration': imgs.length*seconds+'s', 'animation-delay': index*seconds+'s'}" :src="img" :key="index"/>
            </template>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script type="text/javascript" src="index.js"></script>

css:

html,body { margin: 0; padding: 0;}
#app {
    width: 100vw;
    height: 100vh;
}
#main {
    position: relative;
    width: 100%;
    height: 100%;
}

#main {
    animation-timing-function: linear;
    animation-delay: 0s;
    animation-iteration-count: infinite;
    animation-direction: normal;
    animation-fill-mode: none;
    animation-play-state: running;
    animation-name: mainAnimation;
}

img {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    opacity: 0;
    z-index: 0;
    animation-timing-function: linear;
    animation-delay: 0s;
    animation-iteration-count: infinite;
    animation-direction: normal;
    animation-fill-mode: none;
    animation-play-state: running;
    animation-name: imageAnimation;
}
 

@keyframes mainAnimation { 
    0% {
        transform: scale(1) ;
	}
	20% {
	    transform: scale(1.8) ;
	}
    50% {
	    transform: scale(1) ;
	}
    100% { transform: scale(1.8)  }
	100% { transform: scale(1.8)  }
}

@keyframes imageAnimation { 
    0% { opacity: 0.1; }
    8% {
	    opacity: 1;
	}
	20% {
	    opacity: 1;
	}
	30% {
	    opacity: 0.1;
	}
    100% { opacity: 0.1 }
}

vue

new Vue({
    el: "#app",
    data: function() {
        return {
            seconds: 10,
            imgs: ['./img/1.webp', './img/2.webp', './img/3.webp', './img/4.webp']
        }
    },
    mounted: function() {}
})
好的,以下是一个使用 Vue3 实现淡出淡入轮播图的完整实例: ```vue <template> <div class="carousel-container"> <div class="carousel-slide"> <transition-group name="fade" mode="out-in"> <div :key="currentSlide" class="carousel-slide-item"> <img :src="slides[currentSlide]" class="carousel-slide-img"> </div> </transition-group> </div> <div class="carousel-dots"> <span v-for="(slide, index) in slides" :key="index" class="carousel-dot" :class="{ active: currentSlide === index }" @click="jumpToSlide(index)"></span> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Carousel', props: { slides: { type: Array, required: true } }, setup(props) { const currentSlide = ref(0); const jumpToSlide = index => { currentSlide.value = index; }; const nextSlide = () => { currentSlide.value = (currentSlide.value + 1) % props.slides.length; }; setInterval(nextSlide, 5000); return { currentSlide, jumpToSlide }; } }; </script> <style> .carousel-container { position: relative; overflow: hidden; } .carousel-slide { position: relative; height: 500px; } .carousel-slide-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .carousel-slide-img { width: 100%; height: 100%; object-fit: cover; } .carousel-dots { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; justify-content: center; } .carousel-dot { width: 10px; height: 10px; margin: 0 10px; border-radius: 50%; background-color: #fff; cursor: pointer; transition: transform 0.3s ease; } .carousel-dot.active { transform: scale(1.5); } .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } </style> ``` 该组件使用了 `ref` 来创建响应式数据 `currentSlide`,并且在 `setup` 函数中使用了计时器来每隔5秒钟自动切换轮播图。该组件还包括了一个 `jumpToSlide` 方法,用于实现点击小圆点跳转到指定的轮播图。在模板中,使用了 Vue3 的 `<transition-group>` 组件来实现淡入淡出效果。同时,还使用了 CSS3 的 `transform` 和 `transition` 属性来实现小圆点的缩放效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值