轮播图,用vue来写一个简单的轮播图

轮播图,用vue来写一个简单的轮播图

写的很简单,就是一个小练习,哈哈哈,下面的几张图分别是轮播图的第一张,中间图,最后一张的效果图。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

使用了vue 中的属性绑定 v-bind ,v-show 以及 事件监听 v-on 指令。

思路:
1.vue实例中声明一个data属性imgArr,用于存放每张轮播图的地址,
2.同时定义一个默认起始索引index=0,
3.然后给两边的箭头监听点击事件,
4.最后通过v-show判断一下第一张和最后一张就不再显示左右箭头即可。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue练习</title>
    <style>
        body{
            background-color: #eee;
        }
        #loopImg{
            width: 500px;
            height: 300px;
            margin: 25vh auto;
            border-radius: 8px;
            position: relative;
        }
        .image-box>img{
            height: 300px;
            width: 300px;
            border-radius: 10px;
            z-index: -1;
            left: 100px;
        }
        span>img{
            width: 95px;
            position: absolute;
        }
        .left{
            left: 0;
            top: 100px;
        }
        .right{
            right: 0;
            top: 100px;
        }
    </style>
</head>
<body>
    <div id="loopImg">
    	<!-- 左箭头 -->
        <span><img src="./link/left.png"  class="left" @click="prev" v-show="index!=0"></span>
        <span class="image-box">
            <img v-bind:src="imgArr[index]" >
        </span>
        <!-- 右箭头 -->
        <span ><img src="./link/right.png"  class="right" @click="next" v-show="index<imgArr.length-1"></span>
    </div>

     <!-- 开发环境版本,包含了有帮助的命令行警告 -->
     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

     
     <script>
         let app = new Vue({
             el: '#loopImg',
             data: {
             	 // 图片url 数组
                 imgArr:[
                    "/image/ys01.jpg",
                    "/image/ys02.jpg",
                    "/image/ys03.jpg",
                    "/image/ys04.jpg",
                    "/image/ys05.jpg",
                    "/image/ys06.jpg",
                    "/image/ys07.jpg",
                    "/image/ys08.jpg",
                    "/image/ys09.jpg",
                    "/image/ys10.jpg",
                    "/image/ys11.jpg",
                    "/image/ys12.jpg",
                    "/image/ys13.jpg",
                 ],
                 index:0  // 图片数组索引
             },
             methods: {
           		 // 前一张图片
                 prev:function(){
                    this.index--
                 },
                 // 下一张图片
                 next:function(){
                    this.index++
                 }
             }
         })
     </script>
</body>
</html>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单Vue2轮播图实现: 1. 首先在HTML中创建一个轮播图容器,包含图片和导航按钮: ``` <template> <div class="slider"> <div class="slider-container"> <img v-for="(image, index) in images" :key="index" :src="image.src" :class="{ active: index === activeIndex }"> </div> <div class="slider-navigation"> <button v-for="(image, index) in images" :key="index" @click="setActiveIndex(index)" :class="{ active: index === activeIndex }"></button> </div> </div> </template> ``` 2. 在Vue的data中定义轮播图所需的数据和方法: ``` <script> export default { data() { return { activeIndex: 0, // 当前显示的图片索引 images: [ // 图片列表 { src: 'https://picsum.photos/800/400?random=1' }, { src: 'https://picsum.photos/800/400?random=2' }, { src: 'https://picsum.photos/800/400?random=3' }, { src: 'https://picsum.photos/800/400?random=4' } ] } }, methods: { setActiveIndex(index) { // 设置当前显示的图片索引 this.activeIndex = index }, next() { // 切换至下一张图片 this.activeIndex++ if (this.activeIndex >= this.images.length) { this.activeIndex = 0 } }, prev() { // 切换至上一张图片 this.activeIndex-- if (this.activeIndex < 0) { this.activeIndex = this.images.length - 1 } } }, mounted() { // 自动轮播 setInterval(() => { this.next() }, 3000) } } </script> ``` 3. 在CSS中定义轮播图样式: ``` <style> .slider { position: relative; overflow: hidden; } .slider-container { display: flex; transition: transform 0.5s ease; } .slider-container img { width: 100%; height: auto; object-fit: cover; } .slider-navigation { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; } .slider-navigation button { width: 16px; height: 16px; border-radius: 50%; border: none; margin: 0 10px; background-color: #ccc; cursor: pointer; transition: background-color 0.2s ease; } .slider-navigation button.active { background-color: #333; } </style> ``` 这样就完成了一个简单Vue2轮播图实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀死一只知更鸟debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值