Vue轮播图三

5 篇文章 0 订阅

使用better-scroll,来做轮播的效果
组件代码:

<template>
  <div class="slide-banner">
    <div class="banner-wrapper">
      <div class="slide-banner-wrapper" ref="slide">
        <div class="slide-banner-content" ref="slideBannerContent">
          <slot></slot>
        </div>
      </div>
      <div class="dots-wrapper">
        <span
          class="dot"
          v-for="num in nums"
          :key="num"
          :class="{'active': currentPageIndex === (num - 1)}"></span>
      </div>
    </div>
    <div class="btn-wrap">
      <button class="next" @click="nextPage">nextPage</button>
      <button class="prev" @click="prePage">prePage</button>
    </div>
  </div>
</template>


<script>
  import BScroll from '@better-scroll/core'
  import Slide from '@better-scroll/slide'
  BScroll.use(Slide)

  export default {
    name:"SliderDefault",
    props:{
      nums:{
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        currentPageIndex: 0,
        childrendiv:null,
      }
    },
    mounted() {
      this.setClass();
      this.init();
    },
    beforeDestroy() {
      this.slide.destroy();
    },
    methods: {
      hasClass(el, className) {
        let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
        return reg.test(el.className)
      },
      addClass(el, className) {
        if (this.hasClass(el, className)) {
          return
        }
        let newClass = el.className.split(' ')
        newClass.push(className)
        el.className = newClass.join(' ')
      },
      setClass(){
        this.childrendiv = this.$refs.slideBannerContent.children;
        for (let i = 0; i < this.childrendiv.length; i++) {
          let child = this.childrendiv[i]
          this.addClass(child, 'slide-page');
        }
      },
      init() {
        this.slide = new BScroll(this.$refs.slide, {
          scrollX: true,
          scrollY: false,
          slide: true,
          momentum: false,
          bounce: false,
          probeType: 3,
          click:true
        })
        this.slide.on('scrollEnd', this._onScrollEnd)

        this.slide.on('slideWillChange', (page) => {
          this.currentPageIndex = page.pageX
        })

          // v2.1.0
        this.slide.on('slidePageChanged', (page) => {
          //console.log('CurrentPage changed to => ', page)
          this.$emit('slidePageChanged');
        })
      },
      _onScrollEnd () {
        this.$emit("onScrollEnd");
        //console.log('CurrentPage => ', this.slide.getCurrentPage())
      },
      nextPage() {
        this.slide.next()
      },
      prePage() {
        this.slide.prev()
      }
    }
  }
</script>

<style scoped lang='scss'>
.slide-banner{
  .banner-wrapper{
    position:relative;
  }
  .slide-banner-wrapper{
    min-height:1px;
    overflow:hidden;
  }
  .slide-banner-content{
    height:200px;
    white-space:nowrap;
    font-size:0;
    .slide-page{
      display:inline-block;
      height:200px;
      width:100%;
      line-height:200px;
      text-align:center;
      font-size:26px;
      a{
        display: block;
        width: 100%;
        height: 100%;
        overflow: hidden;
        text-decoration: none;
        img{
          width: 100%;
          height: 100%;
        }
      }

    }
  }
  .dots-wrapper{
    position:absolute;
    bottom:4px;
    left:50%;
    transform:translateX(-50%);
    .dot{
      display:inline-block;
      margin:0 4px;
      width:8px;
      height:8px;
      border-radius:50%;
      background:#eee;
      &.active{
        width:20px;
        border-radius:5px;
      }
    }
  }
  .btn-wrap{
    margin-top:20px;
    display:flex;
    justify-content:center;
    button{
      margin:0 10px;
      padding:10px;
      color:#fff;
      border-radius:4px;
      background-color:#666;
    }
  }
}
</style>

组件使用代码:

<template>
  <div class="slidertest">
    <SliderDefault :nums="slides.length">
      <div v-for="(value, index) in slides" :key="index">
        <a :href="value.url">
          <img :src="value.src" alt="">
        </a>
      </div>
    </SliderDefault>
  </div>
</template>


<script>
  import SliderDefault from './SliderDefault'
  export default {
    name:"SliderTest",
    data() {
      return {
        nums: 4,
        currentPageIndex: 0,
        slides: [
          {
            src: require('../assets/logo.png'),
            url:"#"
          },
          {
            src: require('../assets/20190726150130.png'),
            url:"#"
          },
          {
            src: require('../assets/logo.png'),
            url:"#"
          },
          {
            src: require('../assets/20190726150130.png'),
            url:"#"
          }
        ]
      }
    },
    components:{
      SliderDefault
    }
  }
</script>

<style scoped lang='scss'>
.slidertest{

}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于Vue张图片轮播图的示例代码: ```html <template> <div class="carousel"> <div class="slider" :style="{ 'transform': 'translateX(' + translateX + 'px)' }"> <div v-for="(image, index) in images" :key="index" class="slide"> <img :src="image.src" :alt="image.alt"> </div> </div> <div class="controls"> <button @click="prevSlide" class="prev"><</button> <button @click="nextSlide" class="next">></button> </div> </div> </template> <script> export default { data() { return { images: [ { src: 'https://via.placeholder.com/800x400/FF5733/FFFFFF?text=Slide+1', alt: 'Slide 1' }, { src: 'https://via.placeholder.com/800x400/C70039/FFFFFF?text=Slide+2', alt: 'Slide 2' }, { src: 'https://via.placeholder.com/800x400/900C3F/FFFFFF?text=Slide+3', alt: 'Slide 3' } ], currentSlide: 0, slideWidth: 800 } }, computed: { translateX() { return -this.currentSlide * this.slideWidth } }, methods: { prevSlide() { this.currentSlide = (this.currentSlide === 0) ? this.images.length - 1 : this.currentSlide - 1 }, nextSlide() { this.currentSlide = (this.currentSlide === this.images.length - 1) ? 0 : this.currentSlide + 1 } } } </script> <style> .carousel { position: relative; width: 800px; height: 400px; margin: 0 auto; overflow: hidden; } .slider { display: flex; width: 2400px; /* 800px * 3 */ transition: transform 0.3s ease-in-out; } .slide { flex: 0 0 800px; margin-right: 20px; } img { width: 100%; } .controls { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; display: flex; justify-content: space-between; } button { background-color: rgba(255, 255, 255, 0.5); border: none; color: #333; font-size: 20px; font-weight: bold; padding: 10px 20px; cursor: pointer; } button:focus { outline: none; } .prev { margin-right: auto; } .next { margin-left: auto; } </style> ``` 在这个示例中,我们使用了Vue的计算属性来根据当前滑块的索引计算出滑块应该被移动的距离。我们还实现了两个方法,prevSlide和nextSlide,它们分别将当前滑块的索引向前或向后移动一个位置。我们在模板中使用v-for指令来遍历图片数组,并为每个图片创建一个div元素和一个img元素。最后,我们使用CSS来对轮播图进行样式设置,包括将所有滑块放在一行、对当前滑块进行高亮显示以及添加控件来切换滑块。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值