Vue.js轮播图走马灯 (详解)

直接上代码:

轮播图1:

在这里插入图片描述

html

<template>
    <div>
        <div class="back_add">
            <div class="threeImg">
                <div class="Containt">
                    <div class="iconleft"  @click="zuohua">
                        <i class="el-icon-arrow-left"></i>
                    </div>
                    <!-- 'left':calleft + 'px' 是 ul 向左偏移的量 -->
                    <ul :style="{'left':calleft + 'px'}"  v-on:mouseover="stopmove()" v-on:mouseout="move()">
                        <li v-for="(item,index) in superurl" :key="index">
                            <img :src="item.url"/>
                        </li>
                    </ul>
                    <div class="iconright" @click="youhua">
                        <i class="el-icon-arrow-right" ></i>
                    </div>
                    
                </div>
            </div>
           
        </div>
    </div>
</template>

轮播的数据

			//price不是很重要,重要的是url
            calleft:0  //calleft是整个数据的偏移量
            superurl: [
               {
						"url":require("../../assets/shoppingsDetail2.png"),
						"price":"¥1"
					},
					{
						"url":require("../../assets/shoppingsDetail1.png"),
						"price":"¥2"
					},
					{
						"url":require("../../assets/shoppingsDetail3.png"),
						"price":"¥3"
					},
					{
						"url":require("../../assets/shoppingsDetail4.png"),
						"price":"¥4"
					},
					{
						"url":require("../../assets/shoppingsDetail2.png"),
						"price":"¥5"
					},
					{
						"url":require("../../assets/shoppingsDetail1.png"),
						"price":"¥6"
					},
					{
						"url":require("../../assets/shoppingsDetail3.png"),
						"price":"¥7"
					},
					{
						"url":require("../../assets/shoppingsDetail2.png"),
						"price":"¥8"
					}
                 ],

vue.js方法

created() {
        this.move()
    },
    methods: {
        //移动
        move() {
            this.timer = setInterval(this.starmove, 2500)
        },
        //开始移动
        starmove() {
        	// 340是根据自己图片的大小自己设置的,点击一下整体向左平移多少
            this.calleft -= 340;
            if (this.calleft < -1200) {
                this.calleft = 0
            }
        },
        //鼠标悬停时停止移动
        stopmove() {
            clearInterval(this.timer)
        },
        //点击按钮左移
        zuohua() {
            this.calleft -= 340;
            if (this.calleft < -1200) {
                this.calleft = 0
            }
        },
        //点击按钮右移
        youhua() {
            this.calleft += 340;
            if (this.calleft > 0) {
                this.calleft = -1020
            }
        },
    },

css代码

最最最最最重要的一点是:ul需要设置为相对定位,即position:relative

只有这样,整体才会有轮播的效果

<style scoped>
/*超链接图片*/
 
#divAdd {
    background-color: #ededed;
    /*width: 100%;*/
    /*min-width: 1200px;*/
    width: 1000px;
    margin: 0px auto;
}
 
.divAdd-con {
    margin: 0px auto;
    width: 1000px;
    overflow: auto;
    padding: 30px 0px;
}
 
.divAdd-con img {
    float: left;
}
 
.indexrt {
    margin: 0px 40px;
}
 
.divAddleft img {
    float: left;
    margin-bottom: 7px;
}
 
.divAddleft {
    float: left;
    display: inline;
    width: 370px;
}
 
.divAddrt {
    float: right;
    display: inline;
    margin-top: 7px;
}
 
.back_add {
    background-color: #ededed;
}
 
.divAdd-con img {
    border: none;
}
 
 
a:focus,
a:hover {
    color: #23527c;
}
 
 
.threeImg {
    height: 158px;
    background: #ededed;
    border-bottom: 3px solid #4679B2;
    min-width: 1000px;
}
 
.threeImg .Containt ul {
    margin: 0 auto;
    width: 2400px;
    position: absolute;
    left: 0px;
    cursor: pointer;
    height: 100%
}
 
.threeImg .Containt ul li {
    width: 300px;
    margin-right: 40px;
    margin-top: 10px;
    float: left;
}
 
.threeImg .Containt ul li img {
    height: 128px;
    width: 100%;
}
 
.Containt {
    position: relative;
    width: 1000px;
    height: 130px;
    overflow: hidden;
    margin: 0 auto;
}
 
.iconleft {
    position: absolute;
    width: 20px;
    height: 80px;
    top: 10px;
    z-index: 99999;
    padding-top: 48px;
    background-color: #ddd;
    vertical-align: middle;
}
 
.iconright {
    position: relative;
    left: 978px;
    top: 70px;
    background-color: #ddd;
    /*position: absolute;*/
    width: 20px;
    height: 80px;
    top: 10px;
    z-index: 99999;
    padding-top: 48px;
    background-color: #ddd;
    vertical-align: middle;
}
</style>

由于接下来的轮播图是参照elementUI,因此只需要去官网查看相关教程就行

轮播图2: 走马灯效果引用的是elementUI中的样式

在这里插入图片描述

<template>
  <el-carousel :interval="4000" type="card" height="200px">
    <el-carousel-item v-for="item in 6" :key="item">
      <h3>{{ item }}</h3>
    </el-carousel-item>
  </el-carousel>
</template>
 
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>

轮播图3

在这里插入图片描述

<template>
  <el-carousel :interval="5000" arrow="always">
    <el-carousel-item v-for="item in 4" :key="item">
      <h3>{{ item }}</h3>
    </el-carousel-item>
  </el-carousel>
</template>
 
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值