简单实现轮播图片切换效果(基于vue)

思路: 

一: 将vue的框架封装在function中,在界面刷新时调用,将要轮播的图片存放在data中,还有下面的列表也分别保存在data中的一个数组中,然后每隔一段时间进行自动切换的函数写在methods中,注意函数要调用的话,就要在生命周期函数中调用,不然的话就没有用,点击这里了解生命周期函数详细知识。
二: 认识到这里需要的是setinterval()、而不是setimeout()函数:
etTimeout()和setInterval()经常被用来处理延时和定时任务。setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔指定的毫秒数循环调用函数或表达式,直到clearInterval把它清除。
setTimeout()只执行一次,而setInterval可以多次调用。

三: n的设置:方便我们进行操作,比如循环到第几个,下面的小黑点也相应的变色,并且控制循环播放,当n等于数组的个数时,自动置0,从头开始。
四: html页面写好大的框架后,用v-for来控制,需要注意的是用v-for的时候一定要加上:key,
五: v-show的使用
六: 这里使用了字体库,所以要提前下载字体库。

直接上代码了

<body>
<!--页面容器-->
  <div class="index-content" id="my">
    <div class="banner">
        <img  v-for="(v,i) in img " :key="i" :src="v" v-show="i==n"/>   
        <div class="banner-circle">
            <ul>
                <li  v-for="(v,i) in img " :key="i" :class="i==n ?'selected':''"></li>  
            </ul> 
        </div>
    </div>
  </div>

</body>

js

window.onload = function(){
    new Vue({
        el:"#my",
        data:{
            img:["img/banner1.jpg",
                "img/banner2.jpg",
                "img/banner3.jpg",
                "img/banner4.jpg",
                "img/banner5.jpg"],
            n:2
        },
        methods:{
            fun:function(){
                //setInterval(函数体,时间)
                setInterval(this.play,2000)
            },
            play:function(){
                this.n++;
                if(this.n == this.img.length){
                    this.n = 0;
                }
            }
        },
        mounted:function(){    //生命周期  钩子函数   挂载完成
            this.fun()
        }
    })
}

css

*{
    margin:0;
    padding:0;
}
ul {
    list-style-type:none;
}
body {
    font-size: 14px;
    background: #fff;
    overflow-y:scroll;
    overflow-x:hidden;
}
html,body {
    max-width:720px;
    height:100%;
    margin:0 auto;
}


/*index*/
.index-content .banner {
    position: relative;
}
.index-content .banner .banner-circle {
    position: absolute;
    bottom: 5px;
    left: 0;
    right: 0;
    color: #fff;
}
.index-content .banner .banner-circle li{
    display:inline-block;
    background: rgba(0,0,0,.3);
    border-radius: 50%;
    padding:5px;
    margin:2px;
}
.index-content .banner .banner-circle ul {
    text-align: center;
}
.index-content .banner .banner-circle .selected {
    background: rgba(0,0,0,.8);
}
.index-content .banner img {
    width: 100%;
    margin: 0;
    padding: 0;
}
/*index-category*/
.index-content .index-category {
    margin-top: 5%;
}
.index-content .index-category .category {
    width: 50%;
    float:left;
    text-align:center;
}
.index-content .index-category .category .iconfont {
    font-size: 40px;
    display:inline-block;
    padding: 10%;
    border-radius: 50%;
    color:#fff;
    border: 3px solid #f9f9f9;
    box-shadow: 0px 0px 6px rgba(0,0,0,.5);
}
.index-content .index-category .category .iconfont{
    background: #92d85c;
}
.index-content .index-category .category:nth-child(2) .iconfont{
    background: #f60;
}
.index-content .index-category .category:nth-child(4) .iconfont{
    background: #f00;
}
.index-content .index-category .category label {
    display: block;
    padding: 10% 0;
    color: #999;
}
/*index-category end*/


 

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值