加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡水站长网 (https://www.0318zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

怎样用vue实现一个无缝轮播效果?

发布时间:2022-03-02 13:02:48 所属栏目:语言 来源:互联网
导读:怎样用vue实现一个无缝的轮播效果?对于轮播效果的应用场景有很多,我们经常能在网站的头部看到,轮播功能也是比较实用的,但是一些新手在刚接触轮播时,实现的轮播效果有缝隙,效果并不好看,对此下面小编就给大家分享实现无缝轮播效果的代码。 代码 1.子组
      怎样用vue实现一个无缝的轮播效果?对于轮播效果的应用场景有很多,我们经常能在网站的头部看到,轮播功能也是比较实用的,但是一些新手在刚接触轮播时,实现的轮播效果有缝隙,效果并不好看,对此下面小编就给大家分享实现无缝轮播效果的代码。
 
      代码
     1.子组件代码
     代码如下(示例):
 
<template>
  <div>
    <div class="box" @mouseenter="mouse" @mouseleave="mouseleave">
      <ul class="box1">
        <li>
          <img
            :src="n"
            v-for="(n, i) in imgs"
            :key="i"
            alt=""
            :style="{ left: (i - index) * 500 + 'px' }"
            :class="hasAni ? 'animaton' : ''"
          />
        </li>
      </ul>
      <p class="tt" @click="left"><</p>
      <p class="tt1" @click="right">></p>
    </div>
  </div>
</template>
    script代码如下(示例):
 
<script>
export default {
  name: "Lunbo",
  props: ["imgs"],
  data() {
    return {
      // js中使用图片,需要采用require导入
      index: 1,
      hasAni: true,
      istrue: true,
    };
  },
  methods: {
    mouse() {
      clearInterval(this.timer);
    },
    mouseleave() {
      this.timer = setInterval(() => {
        this.index++;
        this.hasAni = true;
        if (this.index == this.imgs.length - 1) {
          setTimeout(() => {
            this.index = 0;
            this.hasAni = false;
          }, 750);
        }
      }, 1500);
    },
    right() {
      if (this.istrue) {
        this.index++;
        this.hasAni = true;
        this.istrue = false;
        if (this.index == this.imgs.length - 1) {
          setTimeout(() => {
            this.index = 1;
            this.hasAni = false;
          }, 750);
        }
        setTimeout(() => {
          this.istrue = true;
        }, 1000);
      }
    },
    left() {
       if (this.istrue) {
        this.index--;
        this.hasAni = true;
        this.istrue = false;
        if (this.index == 0) {
          setTimeout(() => {
            this.index = this.imgs.length - 1;
            this.hasAni = false;
          }, 750);
        }
        setTimeout(() => {
          this.istrue = true;
        }, 1000);
      }
    },
  },
  activated() {
    console.log(1);
    this.timer = setInterval(() => {
      this.index++;
      this.hasAni = true;
      if (this.index == this.imgs.length - 1) {
        setTimeout(() => {
          this.index = 0;
          this.hasAni = false;
        }, 750);
      }
    }, 1500);
  },
  decativated() {
    clearInterval(this.timer);
  },
};
</script>
    css
 
<style scoped>
p {
  width: 30px;
  height: 60px;
  background-color: rgba(46, 139, 86, 0.356);
  line-height: 60px;
  font-size: 24px;
  position: absolute;
  top: 105px;
}
.tt {
  left: 0;
}
.tt1 {
  right: 0;
}
.box {
  width: 500px;
  height: 300px;
  margin: 100px auto;
  position: relative;
  overflow: hidden;
}
.box1 img {
  position: absolute;
  left: 0px;
  top: 0;
  width: 500px;
  height: 300px;
}
.animaton {
  transition: left 0.75s;
}
</style>
    2.父组件代码
 
    父组件
 
<keep-alive>
      <Lunbo :imgs="imgs" />
</keep-alive>
    导入模块
 
import Lunbo from "./components/Lunbo";
    图片数据
 
data() {
    return {
      imgs:[
        require("./assets/6.jpg"),
        require("./assets/1.jpg"),
        require("./assets/2.jpg"),
        require("./assets/3.jpg"),
        require("./assets/4.jpg"),
        require("./assets/5.jpg"),
        require("./assets/6.jpg"),
        require("./assets/1.jpg"),
      ],
    }




(编辑:衡水站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读