vue轮播图代码 仿小米商城轮播图

news/2024/7/19 13:48:19 标签: js, vue, html

自己写的代码,仿小米商城轮播图,下面是整个代码,可以直接在首页注册组件使用,样式我使用了sass

结构代码

<template>
    <div class="box">
      <div class="minbox" :style="`width:${width}px;height:${height}px`" @mouseenter="cutIn" @mouseleave="layOff">
      //左前进按钮
        <button class="left" @click="left" :flag="flag">
          <i class="el-icon-arrow-left"></i>
        </button>
        //右前进按钮
        <button class="right" @click="right" :flag="flag">
          <i class="el-icon-arrow-right"></i>
        </button>
        //图片
        <div class="banner" ref="banner" >
          <img
            :src="item"
            v-for="(item,index) of imgSrc"
            :key="index"
            :show="waiting.includes(index)"
          />
        </div>
      </div>
      //底部切换横杠
      <p>
        <span @mousemove="qiehuan(index)" v-for="(item,index) of imgSrc.length" :key="index">
          <a href="javascript:void(0)" :show="show == index"></a>
        </span>
      </p>
    </div>
</template>

业务代码

<script>
export default {
  props: {
  //图片路径
    imgSrc: {
      type: Array,
      default:[
        "http://39.100.7.70:81/public/imgs/cms_1.jpg",
        "http://39.100.7.70:81/public/imgs/cms_2.jpg",
        "http://39.100.7.70:81/public/imgs/cms_3.jpg",
        "http://39.100.7.70:81/public/imgs/cms_4.jpg"
      ]
    },
    //图片切换时间
    num: {
      type: Number,
      default:1000,
    },
    //宽
    width: {
      type: String,
      default:'1225'
    },
    //高
    height: {
      type: String,
      default:'460'
    }
  },
  data() {
    return {
    //定时器
      timer: "",
      //底部横杠样式改变
      show: 0,
      //做动画图片索引
      waiting: [1],
      //两边按钮显示
      flag: false
    };
  },
  methods: {
  //鼠标淡入轮播图
    cutIn() {
      this.flag = true;
      clearInterval(this.timer);
    },
    //淡出
    layOff() {
      clearInterval(this.timer);
      this.flag = false;
      this.bannerTime();
    },
    //定时器
    bannerTime() {
      this.timer = setInterval(() => {
        if (this.show == this.imgSrc.length - 1) {
          this.show = 0;
        } else {
          this.show++;
        }
      }, this.num);
    },
    alterLocation(showIndex) {
      const banner = this.$refs.banner;
      banner.children.forEach((item, index) => {
        item.style.transform = `translateX(${(index - showIndex) *
          this.width}px)`;
        if (showIndex - 1 < 0 && index == banner.children.length - 1) {
          item.style.transform = `translateX(-${this.width}px)`;
        }
        if (showIndex + 1 > banner.children.length - 1 && index == 0) {
          item.style.transform = `translateX(${this.width}px)`;
        }
      });
    },
    qiehuan(i) {
      clearInterval(this.timer);
      this.show = i;
      this.bannerTime();
    },
    left() {
      clearInterval(this.timer);
      if (this.show == 0) {
        this.show = this.imgSrc.length - 1;
      } else {
        this.show--;
      }
      this.bannerTime();
    },
    right() {
      clearInterval(this.timer);
      if (this.show == this.imgSrc.length - 1) {
        this.show = 0;
      } else {
        this.show++;
      }
      this.bannerTime();
    }
  },
  watch: {
    show(value, oldVal) {
      this.waiting.splice(0, this.imgSrc.length - 1, oldVal, value);
      this.alterLocation(value);
    }
  },
  mounted() {
    this.alterLocation(0);
    this.bannerTime();
  }
};
</script>

样式代码

<style lang="scss" scoped>
.box {
  position: relative;
  background: #fff;
  .minbox {
      position: relative;
      margin: 0px auto;
    .banner {
        width: 100%;
        height: 100%;
      white-space: nowrap;
      overflow: hidden;
      position: relative;
      > img {
        width: 100%;
        height: 100%;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 0;
        &[show] {
          transition: transform 0.5s;
          z-index: 2;
        }
      }
    }
  }
  p {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    span {
      display: inline-block;
      padding: 12px 4px;
      a {
        display: inline-block;
        width: 30px;
        height: 2px;
        background: #ccc;
        opacity: 0.5;
        &[show] {
          opacity: 1;
        }
      }
    }
  }
  .left,
  .right {
    width: 40px;
    height: 40px;
    outline: none;
    background: #ccc;
    border: none;
    position: absolute;
    background-color: rgba(31, 45, 61, 0.2);
    top: 50%;
    transform: translateY(-50%);
    z-index: 20;
    border-radius: 50%;
    text-align: center;
    &:hover {
      background-color: rgba(31, 45, 61, 0.5);
    }
    i {
      color: #fff;
    }
  }
  .left {
    left: 5px;
    opacity: 0;
    transition: left 0.2s, opacity 0.2s;
    &[flag] {
      left: 20px;
      opacity: 1;
    }
  }
  .right {
    right: 5px;
    opacity: 0;
    transition: right 0.2s, opacity 0.2s;
    &[flag] {
      right: 20px;
      opacity: 1;
    }
  }
}
</style>

http://www.niftyadmin.cn/n/1834204.html

相关文章

django 分页

HTML代码 <div id"upload_div3"> <div style"height: 350px;width: 100%;"> <table id"tab" class"table table-bordered "> <thead> <tr> <th><input id"inputname" class"in…

微信小程序使用阿里矢量图自定义icon

在微信小程序中使用icon 1.在阿里矢量图官网选好图标加入我的项目&#xff0c;在项目中点击Font class和查看在线链接&#xff0c; 2.然后会生成链接地址&#xff0c;点击进入 3.复制代码 4.在微信开发者工具中创建样式文件将代码复制进去&#xff0c;将.iconfont 更改成 [cl…

巴尔的摩古老的圣保罗教堂上的一首诗

你要对自己诚实尤其不要无情装有情对爱情不要玩世不恭在这干旱,没有希望的土地上它是一片四季常青的绿洲.你要吸取流水年华的经验从容地向青春时光告别你要培养自己的精神力量以抗衡突如其来的不幸的打击但你千万不要用想象使自己苦恼,忧伤有很多恐惧产生于疲劳和孤独除去有益于…

DLUTOJ1216

题目大意是&#xff1a;给出 $N$ 个正整数&#xff0c;其中至多有一个数只出现一次&#xff0c;其余的数都出现了两次。判断是否有某个数只出现一次&#xff0c;若有输出这个数&#xff0c;否则输出“-1”。 $1\le N\le 5000000$ 这道题的正解是用位运算中的异或 XOR &#xff…

vsftp FTP服务器外网访问设置

引用&#xff1a; linux中VSFTP无法从外网访问问题&#xff01; http://blog.csdn.net/zbulrush/article/details/841978 原文&#xff1a; FTP协议有两种工作方式&#xff1a;PORT方式和PASV方式&#xff0c;中文意思为主动式和被动式。 Port模式&#xff1a; ftp server:tcp…

样条之埃特金(Aitken)逐步插值函数

核心代码&#xff1a; // // 埃特金逐步插值 // static float GetValueAitken(const void* valuesPtr, int stride, int n, float t, float eps) { int i,j,k,m,l;float z,xx[10],yy[10];// 初值z 0.0f;// 特例处理if (n < 1) {return(z);}if (n 1) { z YfGetFloatValue(…

html5 ajax 文件上传

http://html5demos.com/dnd-upload 看这个例子看了一会儿...这个是支持拖拽的上传。 下面代码是一个简单的ajax的文件上传&#xff1a; 1 function match(url,rs)2 {3 var formData new FormData();4 var room_type $("#room_type option:selected").val()…

POJ 1063 Flip and Shift 最详细的解题报告

题目来源&#xff1a;Flip and Shift 题目大意&#xff1a;一个椭圆形的环形容器中有黑色和白色两种盘子&#xff0c;问你是否可以将黑色的盘子连续的放在一起。你可以有以下两种操作&#xff1a; 1、顺时针旋转所有的盘子 2、顺时针旋转3个盘子 解题思路&#xff1a;第一种操作…