css实现跑马灯-从左往右缓慢滑动VUE3

 

<template>
  <div class="container">
    <div class="box">
      <!-- 假设lists列表有4个项,设置ul的宽度为400%(相对于box),设置li的宽度为12.5%(相对于ul是12.5%;相对于box是50%),但是li要有8个,其中4个是复制的,用来占位 -->
      <ul
        :style="
          'width:' +
          lists.length * 100 +
          '%;animation-duration:' +
          lists.length * 4 +
          's;'
        "
      >
        <li
          @click="clickItem(item)"
          :style="'width:' + 100 / (lists.length * 2) + '%;'"
          v-for="(item, index) in lists"
          :key="index"
        >
          <div class="content">{{ item.name }}</div>
        </li>
        <li
          :style="'width:' + 100 / (lists.length * 2) + '%;'"
          v-for="(item, index) in lists"
          :key="index"
        >
          <div class="content">{{ item.name }}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  onActivated,
  onMounted,
  getCurrentInstance,
  shallowRef,
} from 'vue';

const lists = [
  { name: '马云阿萨' },
  { name: '雷军的' },
  { name: '王勤啊啊啊啊' },
  { name: '等伦伦' },
];

const clickItem = (item) => {
  console.log(item, '点击换掉');
};
</script>

<style scoped>
.box {
  width: 100%;
  height: 100px;
  background-color: pink;
  overflow: hidden;
}
.box ul {
  animation-name: move;
  /* 在style中动态设置每一个li花费的时间为2s */
  animation-duration: 8s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}
ul li {
  float: left;
  height: 99px;
  display: flex;
  align-items: center;
  border: solid 1px red;
  margin: 10px;
}
ul li .content {
  background-color: rgba(0, 0, 0, 0.2);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
@keyframes move {
  0% {
    transform: translateX(0);
  }
  100% {
    /* 平移自身宽度的50%,ul宽度的50%,剩下的那50%用来在下一次显示时占位 */
    transform: translateX(-50%);
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值