vue轮播图中间大两头小

<template>
  <div v-if="items.length" class="full-page-slide-wrapper">
    <swiper :options="swiperOption" ref="mySwiper">
      <!-- slides -->
      <template v-for="item in items">
        <swiper-slide v-if="item.bannerImageUrl" :key="item.pid">
          <img @click="toTopic(item)" :src="item.bannerImageUrl" alt>
        </swiper-slide>
      </template>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script type="text/ecmascript-6">
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import { getFirstConfig } from '@/services/main.service'
const COMPONENT_NAME = 'pull-page-slide'
export default {
  name: COMPONENT_NAME,
  data () {
    return {
      items: [],
      defaultIndex: 0,
      swiperOption: {
        direction: 'horizontal',
        loop: true,
        autoplay: 5000,
        slidesPerView: 'auto',
        centeredSlides: true,
        spaceBetween: 10,
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
          bulletActiveClass: 'slide_dot_active',
          bulletClass: 'slide_dot'
        }
      }
    }
  },
  mounted () {
    this.getConfig()
  },
  components: {
    swiper,
    swiperSlide
  },
  methods: {
    handleClick () {

    },
    toTopic (item) {
      if (item.topicId) {
        this.$router.push({ name: 'topic.detail', params: { id: item.topicId } })
      }
    },
    async getConfig () {
      let { datas } = await getFirstConfig().catch(e => console.error(e))
      this.items = datas
    }
  }
}
</script>
<style lang="scss">
.full-page-slide-wrapper {
  width: 100%;
  height: 140px;
  background: white;
  box-sizing: content-box;
  padding-top: 15px;
  margin-top: 10px;
  position: relative;
  overflow: hidden;
  .swiper-container {
    width: 100%;
    height: 100%;
    .swiper-wrapper {
      display: flex;
      align-items: center;
    }
    .swiper-slide {
      width: calc(100% - 50px);
      border-radius: 5px;
    }
    .swiper-slide-prev {
      height: 90% !important;
    }
    .swiper-slide-next {
      height: 90% !important;
    }
  }
  img {
    object-fit: fill;
    height: 100%;
    width: 100%;
    border-radius: 5px;
  }
  .slide_dot {
    display: inline-block;
    margin: 5px;
    width: 3px;
    height: 3px;
    background-color: #f2f2f2;
    border-radius: 50%;
    opacity: 0.5;
  }
  .swiper-pagination {
    bottom: 0;
  }
  .slide_dot_active {
    display: inline-block;
    width: 7px;
    height: 3px;
    border-radius: 5px;
    background: white;
    opacity: 1;
  }
}
</style>
图例

1347866-20190417103204428-865023272.png

转载于:https://www.cnblogs.com/raind/p/10721932.html

实现Vue2中间大、两边小的轮播图可以通过以下步骤实现: 1. 首先,在Vue组件引入所需的轮播图组件库,例如swiper或vue-awesome-swiper库,然后在组件的data选项定义轮播图的数据和样式相关属性。 2. 在模板使用v-for指令遍历轮播图数据,生成轮播图的每一项。同时,可以使用v-bind指令绑定样式属性。 3. 对于轮播图中间大、两边小的效果,可以使用CSS的transform和z-index属性来实现。添加样式类,设置中间大图的z-index属性较高,使用transform:scale()属性对中间大图进行缩放。 4. 根据实际需求,可以使用定时器或者Vue的watch属性来实现自动轮播功能。 5. 可以为轮播图添加上一页和下一页的按钮,通过点击触发事件更新轮播图的当前项。 示例代码如下: ```html <template> <div> <div class="swiper-container"> <div class="swiper-wrapper"> <div v-for="(item, index) in carouselData" :key="index" :class="{ 'carousel-item': true, 'center-item': activeIndex === index }" > <img :src="item.img" alt=""> </div> </div> <div class="swiper-button-prev" @click="prev"></div> <div class="swiper-button-next" @click="next"></div> </div> </div> </template> <script> import Swiper from 'swiper' export default { data() { return { carouselData: [ { img: 'img1.jpg' }, { img: 'img2.jpg' }, { img: 'img3.jpg' }, { img: 'img4.jpg' }, { img: 'img5.jpg' } ], activeIndex: 2 // 初始中间项索引 } }, mounted() { // 初始化Swiper实例 new Swiper('.swiper-container', { navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' } }) }, methods: { prev() { this.activeIndex = (this.activeIndex - 1 + this.carouselData.length) % this.carouselData.length }, next() { this.activeIndex = (this.activeIndex + 1) % this.carouselData.length } } } </script> <style> .carousel-item { display: flex; align-items: center; justify-content: center; position: relative; } .carousel-item img { width: 200px; height: 200px; object-fit: cover; } .center-item { transform: scale(1.2); z-index: 10; } .swiper-button-prev, .swiper-button-next { color: #000; font-size: 30px; cursor: pointer; } .swiper-button-prev { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); } .swiper-button-next { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); } </style> ``` 以上代码实现了一个简单的Vue2轮播图组件,通过v-for遍历数据,并使用样式类控制中间大、两边小的效果。使用Swiper库进行轮播图的初始化和导航按钮功能。中间项通过activeIndex属性控制,点击上一页和下一页按钮会更新activeIndex的值,从而实现轮播切换。 该实现方式可以根据具体需求进行自定义调整和扩展,例如增加切换动画效果、自定义轮播速度等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值