背景颜色随着轮播图的更换而切换

效果图:

一. 安装swiper(我的swiper版本是"^4.5.0",)
npm install swiper --save
二.局部引入
import Swiper from “swiper”;
import ‘swiper/dist/css/swiper.min.css’;
三.具体代码如下

<template>
  <div class="content" ref="bgColor">
    <div class="box">
      <div class="swiper-container  swiper-no-swiping"     @mouseover="mouseOver()" @mouseleave="mouseOut()">
        <div class="swiper-wrapper">
          <div v-for='(el,index) in arrItem' class="swiper-slide" :key="index">
            <img class="img" :src="el.imgUrl" >
          </div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination swiper-P1"></div>
        <!-- 如果需要导航按钮 -->
        <div v-show="show" >
          <div class="swiper-button-prev mySwiperBtn"></div>
          <div class="swiper-button-next mySwiperBtn"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  //局部引入swiper
  import Swiper from "swiper";
  import 'swiper/dist/css/swiper.min.css';
  export default {
    name: "HelloWorld",
    data(){
      return{
        show:false,
        arrItem:[
          {
            name:'swiperSlide5',
            imgUrl:'https://img.piaoniu.com/misc/0a96578c4b26ee93e852d29d92fa4c1500a96ed9.png'
          },
          {
            name:'swiperSlide1',
            imgUrl:'https://img.piaoniu.com/misc/45f0ba0246de5530b99efbded68349c5b2e22134.jpg'
          },
          {
            name:'swiperSlide51',
            imgUrl:'https://img.piaoniu.com/misc/89447454b6eed4fafecb0ccc221e969a91d0c9d1.jpg'
          },
          {
            name:'swiperSlide1111115',
            imgUrl:'https://img.piaoniu.com/misc/4e8751950e48f80190e3796e2456fb5c21c548e4.jpg'
          }
        ]

      }
    },
    mounted(){
      const Swiper2= new Swiper('.swiper-container', {
        loop: true,
        autoplay: true,
        pagination: '.swiper-P1',
        autoplay: {
          delay:3000,
          disableOnInteraction: false  //用户操作swiper之后,是否禁止autoplay。默认为true:停止。
        },
        pagination: {
          el: '.swiper-pagination',
          clickable:true  //此参数设置为true时,点击分页器的指示点分页器会控制Swiper切换。
        },
        //slide的切换效果,默认为"slide"(位移切换),可设置为'slide'(普通切换、默认),"fade"(淡入)"cube"(方块)"coverflow"(3d流)"flip"(3d翻转)。
        effect : 'slide',
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      })
      //背景颜色随轮播图片而改变
      const bgColor=this.$refs.bgColor;
      Swiper2.on('slideChangeTransitionEnd', function () {
        if((Swiper2.realIndex)==0){
          bgColor.style.transition="all 0.1s";
          bgColor.style.backgroundColor="#F3BAC1";
        }else if((Swiper2.realIndex)==1){
          bgColor.style.transition="all 0.1s";
          bgColor.style.backgroundColor="#A9282A";
        }else if((Swiper2.realIndex)==2){
          bgColor.style.transition="all 0.1s";
          bgColor.style.backgroundColor="#5496A0";
        }else if((Swiper2.realIndex)==3){
          bgColor.style.transition="all 0.1s";
          bgColor.style.backgroundColor="#E6E6E6";
        }
      });
    },
    methods:{
      //前进后退按钮的显示与隐藏
      mouseOver(){
        this.show=true;
      },
      mouseOut(){
        this.show=false;
      }
    }

  }
</script>
<style scoped>
  .content{
    width:100%;
    background-color: #F3BAC1;
    height: 456px;
  }
  .box{
    width:980px;
    margin:0 auto;
    padding:30px  0px;
  }
  .swiper-container{
    width:980px;
    height:400px;
    display: inline-block;
  }


  .swiper-button-prev, .swiper-container-rtl .swiper-button-next {
    background-image: url('data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%…19.9L22%2C44L0%2C22L0%2C22L0%2C22z'%20fill%3D'%23007aff'%2F%3E%3C%2Fsvg%3E');
    background-size: 20px  20px;
    left: 10px;
    right: auto;
    background-color: black;
    opacity: 0.3;
    width:50px;
    height: 50px;
  }

  .swiper-button-next, .swiper-container-rtl .swiper-button-prev {
    background-image: url('data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%…2L2.9%2C2.1L5%2C0L27%2C22L27%2C22z'%20fill%3D'%23007aff'%2F%3E%3C%2Fsvg%3E');
    right: 10px;
    background-size: 20px  20px;
    left: auto;
    background-color: black;
    opacity: 0.3;
    width:50px;
    height: 50px;
  }
  .img{
    width:980px;
    height:400px;
  }


</style>



//自定义分页器的样式
<style>
  .swiper-pagination-bullet {
    width: 18px;
    height: 18px;
    display: inline-block;
    border-radius: 100%;
    background: #fff;
  }
  .swiper-pagination-bullet-active{
    background-color: pink;
  }
</style>

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现轮播图图片切换背景根据图片颜色渐变的效果,可以使用CSS和JavaScript来实现。下面是一种实现方式的示例代码: HTML部分: ```html <div class="slideshow"> <div class="slide"> <img src="image1.jpg" alt="Image 1"> </div> <div class="slide"> <img src="image2.jpg" alt="Image 2"> </div> <div class="slide"> <img src="image3.jpg" alt="Image 3"> </div> </div> ``` CSS部分: ```css .slideshow { position: relative; } .slide { position: absolute; top: 0; left: 0; opacity: 0; transition: opacity 1s ease-in-out; } .slide.active { opacity: 1; } .slideshow img { width: 100%; height: auto; } .slideshow::before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(to right, rgba(0,0,0,0.8), rgba(0,0,0,0)); z-index: -1; } ``` JavaScript部分: ```javascript const slides = document.querySelectorAll('.slide'); let currentIndex = 0; function changeSlide() { slides[currentIndex].classList.remove('active'); currentIndex = (currentIndex + 1) % slides.length; slides[currentIndex].classList.add('active'); } setInterval(changeSlide, 3000); ``` 上述代码,通过CSS设置了轮播图容器的样式以及背景渐变效果。JavaScript部分实现了切换轮播图的逻辑,并使用setInterval函数定时调用changeSlide函数来切换图片。每次切换图片时,会为当前显示的图片添加active类,从而使其显示出来。 你可以根据实际需要修改CSS样式和JavaScript代码,来适应你的项目需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值