消息框左右滚动

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <div class="box-outer">
        <div class="before" v-show="isShow"></div>
        <div class="after" v-show="isShow"></div>
        <div
          ref="sbox"
          class="box vue-loop-scroll-box"
          @mouseenter="stopScroll"
          @mouseleave="leaveScroll"
        >
          <div ref="sitem1" class="vue-loop-scroll-item1">
            <span>
              111111111111111111
            </span>
            <span>
              2222222
            </span>
            <span>
              3333333
            </span>
          </div>
          <div class="vue-loop-scroll-item2"></div>
        </div>
      </div>
    </div>
  </body>
</html>
<style>
  .box-outer {
    position: relative;
    width: 500px;
    overflow: hidden;
    white-space: nowrap;
    background-color: #e5e5e5;
  }
  .box {
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  .box div{display: inline-block;}
  .box-outer .before {
    z-index: 1;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    content: "";
    width: 100px;
    background: linear-gradient(
      to right,
      rgba(255, 255, 255, 1),
      rgba(255, 255, 255, 0.1)
    );
  }
  .box-outer .after {
    z-index: 1;
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    content: "";
    width: 100px;
    background: linear-gradient(
      to right,
      rgba(255, 255, 255, 0.1),
      rgba(255, 255, 255, 1)
    );
  }

  ul {
    display: inline-block;
  }
</style>

<script>
  var app = new Vue({
    el: "#app",
    data: {
      speed: 50,
      index: 0,
      isShow: false,
    },
    mounted() {
      this.startScroll();
    },
    methods: {
      //开始滚动
      startScroll() {
        this.$nextTick((res) => {
          var sbox = document.getElementsByClassName("vue-loop-scroll-box")[
            this.index
          ];

          var sitem1 = document.getElementsByClassName("vue-loop-scroll-item1")[
            this.index
          ];
          //文字太少的时候不滚动
          if (this.$refs.sbox.offsetWidth > this.$refs.sitem1.offsetWidth) {
            this.isShow = false;
            return;
          }
          this.isShow = true;
          var sitem2 = document.getElementsByClassName("vue-loop-scroll-item2")[
            this.index
          ];
          sitem2.innerHTML = sitem1.innerHTML;
          var that = this;
          this.timer1 = setInterval(function () {
            that.scrollLeft(sbox, sitem1);
          }, this.speed);
        });
      },
      //向左滚动
      scrollLeft(sbox, sitem1) {
        if (sbox.scrollLeft >= sitem1.offsetWidth) {
          sbox.scrollLeft = 0;
        } else {
          sbox.scrollLeft++;
        }
      },
      //鼠标移入停止
      stopScroll() {
        clearInterval(this.timer1);
      },

      //鼠标移开
      leaveScroll() {
        this.startScroll();
      },
    },
  });
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值