轮播图组件-vue2+less

template里面的代码:

<template>
    <div class="box">
        <!--这里是轮播图的图标  -->
        <img  :src="require(`@/assets/image/${dataList[indexOf].picture}`)" @mouseenter="sbxh()" @mouseout="lksb()" alt="">
        <!-- 这里是轮播图的下一张 -->
        <button class="lubo-btn1" @click="btnOne(0)">&lt;</button>
        <!-- 这里是轮播图的下一张 -->
        <button class="lubo-btn2" @click="btnOne(1)">&gt;</button> 
        <ul class="asd">
            <!-- 绑定样式-这里是原点,原点的样式 -->
            <li v-for="(item,index) in dataList" :key="item.id" :class="indexOf == index ?'ative':' ' "></li>
        </ul>
    </div>
</template>

JavaScript的代码

<script>
export default {
    // 这里是轮播的组件名称
    name: "lbtu",
    // 这里是轮播的图的数据
    data() {
        return {
            // 这里是切换下一张和上一张数据,用来判断是那一张图的显示给原点添加样式
         indexOf:0,
        //  这里用来保存定时器的
         time:null,
        }
      
    }, 
    // 父组件的传参的接受
     props:{
        // 这里和父组件的传的 xxx ="qqq"或者:xxx = "qqq" 和这里的xxx一样的名字
            dataList:Array
        },
        methods:{
            // 轮播图的切换
            btnOne(e){
                // 判断传的值为1
                if(e==1){
                    //  如果数组(dataList)的长度 等于 indexOf这个值就执行indexOf=0,如果不为不相等则执行indexOf=indexOf+1
                    this.indexOf = this.dataList.length==this.indexOf? 0:this.indexOf+1
                    // 或者是e==0
                }else if(e==0){
                    // 如果这个 indexOf=0这个值就执行indexOf=this.dataList.length (这个数组的长度为),如果不为不相等则执行indexOf=indexOf-1
                    this.indexOf = this.indexOf==0?this.dataList.length-1:this.indexOf-1
                }
            },
            setert(){
                // 这里要用箭头符号,没有箭头指向这里指向不对,如果用箭头符号走向对了
                this.time=   setInterval(()=>{
                    // 调用轮播图的切换的效果的方法`
            this.btnOne(1);
           },1000)
            },
            // 鼠标悬停
            sbxh(){
                // 清除定时器
                clearInterval(this.time);
                // 定时器的time为空
                this.time ="";
            },
            // 鼠标离开事件
            lksb(){
                // 判断time的长度为空,如果为空则
          if(this.time.length ==0){
            // 调用setert函数
            this.setert();
          }  
        }
        },
        // mounted, 钩子函数:等待vue的页面加载完成,执行这个函数
        mounted(){
          this.setert();
        }  

}
</script>

style里面的代码:

<style lang="less" scoped>
.box {
    width: 1200px;
    height: 400px;
    margin: auto;
    position: relative;
    img{
        width: 1200px;
        height: 400px;
       
    }
    .lubo-btn1,.lubo-btn2{
        position: absolute;
        width: 60px;
        height: 50px;
        z-index: 999;
        border-radius: 10px;
        border: 0px;
        font-size: 20px;
        background-color: rgba(99, 99, 99, 0.5);
    }
    .lubo-btn1 {
        top: 50%;
        left: 20px;
    }
    .lubo-btn2 {
        top: 50%;
       right: 20px;
    }
    .asd {
        width: 100px;
        height: 20px;
       
        display: flex;
        position: absolute;
        bottom: 20px;
        left: 50px;
        justify-content: space-between;
        li {
            list-style: none;
            width: 20px;
            height: 20px;
            background-color: rgba(0, 0, 0, 0.5);
            border-radius: 20px;
           
        }
    }

}
.ative {
    background-color: red !important;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]:根据提供的引用内容,版本3.1.3是vue-awesome-swiper的一个旧版本。引用\[2\]中提到了如何全局引入vue-awesome-swiper,并且需要引入swiper的CSS文件。引用\[3\]中展示了如何在Vue组件中使用vue-awesome-swiper来实现轮播图。根据您的需求,您想要在切换slide时更换app组件的背景。 为了实现这个需求,您可以在vue-awesome-swiper的slide切换事件中,通过修改app组件的背景样式来实现背景的更换。您可以在Vue组件中监听swiper的slideChange事件,并在事件回调函数中修改app组件的背景样式。 下面是一个示例代码,展示了如何实现这个需求: ```javascript <template> <div id="home-swiper"> <swiper ref="homeSwiper" :options="homeSwiperOptions" @slideChange="handleSlideChange"> <swiper-slide>...</swiper-slide> <swiper-slide>...</swiper-slide> </swiper> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; import "swiper/dist/css/swiper.css"; export default { components: { swiper, swiperSlide, }, methods: { handleSlideChange() { // 在这里修改app组件的背景样式 // 例如:this.$root.$el.style.background = "red"; }, }, }; </script> ``` 在handleSlideChange方法中,您可以根据需要修改app组件的背景样式。例如,您可以使用`this.$root.$el.style.background`来修改app组件的背景颜色。请根据您的具体需求进行相应的修改。 希望这个示例能够帮助您实现轮播图并更换app组件的背景。如果您有任何其他问题,请随时提问。 #### 引用[.reference_title] - *1* [【npm install vue-awesome-swiper@3.1.3 -S 】下载成功但是vue-awesome-swiper 用不了](https://blog.csdn.net/Sonnenlicht77/article/details/126951340)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [关于swiper的两种用法(swiper@4.0.7 vue-awesome-swiper@3.1.3)](https://blog.csdn.net/weixin_52259399/article/details/129066576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue-awesome-swiper @3.1.3使用,记录一些bug及解决方法](https://blog.csdn.net/gegegegege12/article/details/121387965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值