vue简单实现无缝滚动

效果

在这里插入图片描述

实现思路

在这里插入图片描述
在vue中如何复制一份列表出来呢且不能丢失绑定的事件,很简单使用slot插槽,使用两个插槽我们就拥有了两个列表

<div class="listScroll" ref="box">
    <slot></slot>
    <slot></slot>
  </div>

组件完整代码

<template>
  <div class="listScroll" ref="box">
    <slot></slot>
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "listScroll",
  created() {},
  mounted() {
    //在盒子内容高度小于可视高度时不滚动
    if (this.boxHeight < this.ele0.clientHeight) {
      this.start(this.height);
      this.setEvet();
    } else {
      this.isScroll = false;
    }
  },
  props: {
    speed: {
      default: 1,
      type: Number,
    },
  },
  computed: {
    //第一个slot
    ele0() {
      return this.$refs.box.children[0];
    },
    //第二个slot
    ele1() {
      return this.$refs.box.children[1];
    },
    //盒子的可视高度
    boxHeight() {
      return this.$refs.box.clientHeight;
    },
  },
  data() {
    return {
      height: 0,
      isScroll: true,
    };
  },
  methods: {
    //鼠标移入停止滚动 移出继续滚动
    setEvet() {
      this.$refs.box.onmouseenter = () => {
        this.isScroll = false;
        // this.height = 0;
      };
      this.$refs.box.onmouseleave = () => {
        this.isScroll = true;
        this.$nextTick(() => {
          this.start(this.height);
        });
      };
    },
    //滚动方法
    start(height) {
      this.ele0.style = `transform:translateY(-${height}px);`;
      this.ele1.style = `height:${this.boxHeight}px;transform:translateY(-${height}px);overflow: hidden;`;
      if (height >= this.ele0.clientHeight) {
        this.height = 0;
      } else {
        this.height += this.speed;
      }
      if (!this.isScroll) return;
      window.requestAnimationFrame(() => {
        this.start(this.height);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.listScroll {
  overflow: hidden;
}
.hover {
  overflow-y: auto;
}
.hide {
  display: none;
}
</style>

使用

<template>
  <div class="scroll">
    <list-scroll class="box" :speed="1">
      <div class="list">
        <div class="item" v-for="item in list" :key="item.xh">
          <span>{{ item.xh }}</span
          ><span>{{ item.label }}</span>
        </div>
      </div>
    </list-scroll>
  </div>
</template>

<script>
import ListScroll from "@/components/listScroll";
export default {
  name: "scroll",
  components: { ListScroll },
  data() {
    return {
      list: new Array(10)
        .fill(1)
        .map((s, i) => ({ xh: i + 1, label: "hello wrold" })),
    };
  },
};
</script>

<style lang="less" scoped>
.box {
  height: 300px;
}
.list {
  padding: 0 10px;
  width: 300px;
  .item {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
    cursor: pointer;
    &:hover {
      background-color: #95a5a6;
    }
  }
}
</style>

至此一个简单的无缝滚动就完成了(vue2和vue3通用)
  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue.js是一种流行的JavaScript框架,用于构建用户界面。要实现无缝滚动效果,可以使用Vue.js的transition组件和CSS动画来实现。 首先,你需要在Vue组件中定义一个包含滚动内容的容器,并设置其样式为`overflow: hidden`,以隐藏超出容器的内容。然后,在容器内部创建一个滚动元素,设置其样式为`white-space: nowrap`,以使内容水平排列。 接下来,你可以使用Vue的transition组件来添加过渡效果。在滚动元素上添加一个`v-if`指令,根据条件来决定是否显示滚动元素。当需要滚动时,将滚动元素的内容复制一份,并将其追加到滚动元素的末尾,形成一个无限循环的效果。 然后,你可以使用CSS动画来实现滚动效果。通过给滚动元素添加一个`transform`属性,并使用`translateX`函数来实现水平位移。同时,使用`animation`属性来定义动画的持续时间、缓动函数等。 最后,在Vue组件中使用定时器或者其他方式来触发滚动效果。可以通过改滚动元素的`transform`属性的值来实现滚动效果。 下面是一个简单的示例代码: ```html <template> <div class="scroll-container"> <div class="scroll-content" v-if="isScrolling"> {{ content }} </div> </div> </template> <script> export default { data() { return { content: '这是滚动的内容', isScrolling: true }; }, mounted() { setInterval(() => { // 每隔一段时间改变滚动元素的transform属性值,实现滚动效果 }, 3000); } }; </script> <style> .scroll-container { width: 300px; height: 200px; overflow: hidden; } .scroll-content { white-space: nowrap; animation: scrollAnimation 10s linear infinite; } @keyframes scrollAnimation { 0% { transform: translateX(0); } 100% { transform: translateX(-100%); } } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值