使用vue3写轮播图

刚开始上手Vue3,上午图片不切换,下午给存图片的数组和变量都加了响应式,终于可以实现图片切换了。

上代码,初级学习者,大佬勿喷

<template>
<!-- v-for="item in bannerList" :key="item" -->
  <div class="pic" >
    <img :src="bannerList[showNumber].url" alt="">
  </div>
</template>

<script>
import { onMounted,ref,reactive } from '@vue/runtime-core'
  export default {
    name: 'App',
    setup() {
      //变量
      let showNumber = ref(0)
      const bannerList = reactive([
        {
          url: require("./assets/1.jpg")
        },
        {
          url: require("./assets/2.jpg")
        },
        {
          url: require("./assets/3.jpg")
        },
        {
          url: require("./assets/4.jpg")
        },
        {
          url: require("./assets/5.jpg")
        },
      ])
      //生命周期
      onMounted(()=>{
        setTimeout(()=>{
          if(showNumber.value<4){
            showNumber.value++
            }
            else{
              showNumber.value = 0
            }
            console.log(bannerList[showNumber.value])
        },2000)
      })
      return {
        showNumber,
        bannerList
      }
    }
  }
</script>

<style scoped lang="less">
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    
  }
</style>

如果有更好的方式欢迎评论区交流

-----------------------------------------------------better me---------------------------------------------------------------

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Vue3轮播图组件,同时带有图片注释的示例代码: ```html <template> <div class="carousel-container"> <div class="carousel"> <img v-for="(image, index) in images" :key="index" :src="image.src" :alt="image.title" :class="{ active: index === currentSlide }" @click="showCaption(index)"> <div class="caption" :class="{ active: showCaptionBox }"> <p>{{ images[currentSlide].title }}</p> </div> </div> <div class="carousel-nav"> <button @click="prevSlide"><</button> <button @click="nextSlide">></button> </div> </div> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'Carousel', props: { images: { type: Array, required: true, default: () => [], }, }, setup(props) { const currentSlide = ref(0); const showCaptionBox = ref(false); const prevSlide = () => { currentSlide.value = (currentSlide.value + props.images.length - 1) % props.images.length; }; const nextSlide = () => { currentSlide.value = (currentSlide.value + 1) % props.images.length; }; const showCaption = (index) => { currentSlide.value = index; showCaptionBox.value = true; }; return { currentSlide, showCaptionBox, prevSlide, nextSlide, showCaption, }; }, }); </script> <style> .carousel-container { position: relative; width: 100%; height: 400px; } .carousel { position: relative; width: 100%; height: 100%; overflow: hidden; } .carousel img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s ease-in-out; } .carousel img.active { opacity: 1; } .carousel .caption { position: absolute; bottom: 0; left: 0; width: 100%; background: rgba(0, 0, 0, 0.5); color: #fff; font-size: 16px; text-align: center; padding: 20px; opacity: 0; transform: translateY(100%); transition: all 0.5s ease-in-out; } .carousel .caption.active { opacity: 1; transform: translateY(0%); } .carousel-nav { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; } .carousel-nav button { background: #fff; border: none; padding: 10px; font-size: 20px; margin: 0 10px; cursor: pointer; } </style> ``` 此组件支持传入图片数组,每张图片包含 `src` 和 `title` 两个属性。当用户点击某张图片时,会显示该图片的标题注释。用户可以通过轮播图上的左右箭头切换图片,轮播图支持循环播放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值