Vue轮播图

* {
    margin: 0;
    padding: 0;
}
ul li {
    list-style: none;
    float: left;
    width: 3rem;
    height: 4rem;
    line-height: 4rem;
    text-align: center;
    cursor: pointer;
    color: rgba(255,255,255,.8);
    font-size: 1.6rem;
}
.banner {
    max-width: 50rem;
    margin: 0 auto;
    position: relative;
    margin-top: 6rem;
}
.banner .item{
    display: flex;
    justify-content: center;
    align-items: center;
}
.banner img {
    height: 40rem;
    width: auto;
    display: block;
}
.banner .page {
    background: rgba(0,0,0,.5);
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: center;
}
.current {
    color: #ff6700;
}

Vue.component('carousel', {
    props:{
      imglist: Array
    },
    data: () => {
        return {
            currentIndex: 0,   //默认显示图片
            timer: null    //定时器
        }
    },
    template: `
    <div class="banner">
        <div class="item">
            <img :src="imglist[currentIndex]">
        </div>
        <div class="page" v-if="imglist.length > 1">
            <ul>
                <li @click="gotoPage(prevIndex)">&lt;</li>
                <li v-for="(item,index) in imglist" @click="gotoPage(index)" :class="{'current':currentIndex == index}">{{index+1}}</li>
                <li @click="gotoPage(nextIndex)">&gt;</li>
            </ul>
        </div>
    </div>
    `,
    computed: {
        //上一张
        prevIndex() {
          if(this.currentIndex == 0) {
            return this.imglist.length - 1;
          }else{
            return this.currentIndex - 1;
          }
        },
        //下一张
        nextIndex() {
          if(this.currentIndex == this.imglist.length - 1) {
            return 0;
          }else {
            return this.currentIndex + 1;
          }
        }
      },
    methods: {
        gotoPage(index) {
          this.currentIndex = index;
        },
        //定时器
        runInv() {
            this.timer = setInterval(() => {
            this.gotoPage(this.nextIndex)
            }, 1000)
        }
      },
    created(){
    },
    mounted(){
        // this.runInv();
    },
    updated(){
        // console.log('updated');
    }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一款流行的JavaScript框架,它可以帮助你快速构建交互式的Web应用程序。如果你想在Vue.js中实现轮播图,可以考虑使用Vue.js的插件或者组件库。 以下是使用Vue.js实现轮播图的一些步骤: 1. 安装Vue.js轮播图组件库 你可以使用npm或yarn等包管理工具来安装Vue.js轮播图组件库,例如: ``` npm install vue npm install vue-awesome-swiper --save ``` 其中,vue-awesome-swiper是一个基于Swiper封装的Vue.js组件库,可以方便地实现轮播图效果。 2. 引入轮播图组件 在Vue.js应用程序中,你可以通过import语句引入轮播图组件,并注册为全局或局部组件。例如: ```javascript import Vue from 'vue' import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesomeSwiper) ``` 3. 使用轮播图组件 在Vue.js模板中,你可以使用轮播图组件,并设置一些参数和样式。例如: ```html <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> </div> <div class="swiper-pagination"></div> </div> </template> <script> export default { data() { return { swiperOptions: { pagination: { el: '.swiper-pagination' }, autoplay: true } } }, mounted() { this.$nextTick(() => { new Swiper('.swiper-container', this.swiperOptions) }) } } </script> <style> .swiper-container { width: 100%; height: 300px; } .swiper-slide { text-align: center; font-size: 18px; background: #eee; } </style> ``` 在上述代码中,我们使用了swiper-container、swiper-wrapper、swiper-slide和swiper-pagination等样式来实现轮播图效果。同时,我们还在mounted钩子函数中使用了Swiper构造函数来初始化轮播图。 希望以上内容对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值