美文网首页Vue
Vue<背景跟随轮播图切换效果>

Vue<背景跟随轮播图切换效果>

作者: 誰在花里胡哨 | 来源:发表于2019-09-26 18:26 被阅读0次
    效果图:
    banner.gif
    此文章用到的是vue+swiper实现的效果
    代码如下:
    <template>
      <div class="overall">
        <div class="vessel">
          <img class="bck" :src="list[activeIndex].img" alt />
          <div class="swiper-container">
            <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="(i,index) in list" :key="index">
                <span>BANNER{{index+1}}</span>
                <img :src="i.img" alt style="width:100%" />
              </div>
            </div>
            <div class="swiper-pagination"></div>
            <!--分页器。如果放置在swiper-container外面,需要自定义样式。-->
            <div class="swiper-button-prev swiper-button-white"></div>
            <!--左箭头。如果放置在swiper-container外面,需要自定义样式。-->
            <div class="swiper-button-next swiper-button-white"></div>
            <!--右箭头。如果放置在swiper-container外面,需要自定义样式。-->
          </div>
        </div>
      </div>
    </template>
    
    <script>
    import Swiper from "swiper";
    export default {
      data() {
        return {
          activeIndex: 0,
          list: [
            {
              img: require("@/assets/img/bck.jpg")
            },
            {
              img: require("@/assets/img/bck1.jpeg")
            },
            {
              img: require("@/assets/img/bck2.jpg")
            }
          ]
        };
      },
      mounted() {
        let _this = this;
        var mySwiper = new Swiper(".swiper-container", {
          effect: "fade", //切换效果
          fadeEffect: {
            crossFade: true
          },
          loop: true, //循环播放
          autoplay: true, //开启自动播放
          //显示底部小圆点
          pagination: {
            el: ".swiper-pagination"
          },
          //显示左右切换
          navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev"
          },
          autoplay: {
            delay: 4000 //4秒切换一次
          },
          on: {
            //当切换时触发的方法
            slideChange: function() {
              // this.activeIndex 当前轮播图下标
              if (this.activeIndex > _this.list.length) {
                _this.activeIndex = 0;
              } else {
                _this.activeIndex = this.activeIndex - 1;
              }
            }
          }
        });
      }
    };
    </script>
    
    <style lang="scss" scoped>
    .overall {
      overflow: hidden;
    }
    .vessel {
      width: 800px;
      height: 400px;
      overflow: hidden;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .bck {
      filter: blur(10px);
      width: 100%;
      transform: scale(1.1);
    }
    img {
      display: block;
      position: absolute;
      left: 0;
      top: 0;
    }
    .swiper-container {
      width: 550px;
      height: 300px;
      transition: 0.5s;
      box-shadow: 2px 2px 5px rgb(73, 73, 73);
      cursor: pointer;
      &:hover {
        box-shadow: 0px 0px 5px rgb(73, 73, 73);
      }
    }
    .swiper-slide {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      color: white;
      font-weight: bold;
      font-size: 2rem;
      span {
        position: relative;
        z-index: 5;
      }
    }
    .swiper-button-prev,
    .swiper-button-next {
      opacity: 0;
      transition: 0.5s;
      // margin:0 1rem;
    }
    .swiper-container:hover .swiper-button-prev {
      opacity: 1;
      left: 40px;
    }
    .swiper-container:hover .swiper-button-next {
      opacity: 1;
      right: 40px;
    }
    </style>
    

    相关文章

      网友评论

        本文标题:Vue<背景跟随轮播图切换效果>

        本文链接:https://www.haomeiwen.com/subject/enenuctx.html