码迷,mamicode.com
首页 > Web开发 > 详细

Js跑马灯效果 && 在Vue中使用

时间:2017-02-16 22:01:48      阅读:709      评论:0      收藏:0      [点我收藏+]

标签:innerhtml   class   elf   cti   timer   margin   run   tar   ati   

DEMO:

<!DOCTYPE html>
<html>

<head>
  <title>滚动播报</title>
  <meta charset="UTF-8">
  <style>
    .content {
      height: 60px;
      background-color: #2c2c34;
      overflow: hidden;
    }

    .content ul {
      white-space: nowrap;
    }

    .content ul li {
      position: relative;
      font-size: 14px;
      vertical-align: middle;
      line-height: 35px;
      padding: 0 8px;
      white-space: nowrap;
      display: inline-block;
      color: #fff
    }

    #scrollBox {
      overflow: hidden;
    }

    #scrollBox .scrollWrap {
      width: 500%
    }

    .scrollCont {
      float: left;
    }
  </style>
</head>

<body>
  <div class="content">
    <ul>
      <div id="scrollBox">
        <div class="scrollWrap">
          <div id="scrollContOne" class="scrollCont">
            <li>我是第一条数据</li>
            <li>我是第二条数据</li>
            <li>我是第三条数据</li>
            <li>我是第四条数据</li>
            <li>我是第五条数据</li>
            <li>我是第六条数据</li>
            <li>我是第七条数据</li>
            <li>我是第八条数据</li>
          </div>
          <div id="scrollContTwo" class="scrollCont"></div>
        </div>
      </div>
    </ul>
  </div>

  <script>
    let speed = 40

    let scrollBox = document.getElementById("scrollBox");

    let scrollContOne = document.getElementById("scrollContOne");

    let scrollContTwo = document.getElementById("scrollContTwo");

    scrollContTwo.innerHTML = scrollContOne.innerHTML;

    scrollBox.scrollLeft = 0;

    function scrollRadio() {

      if (scrollBox.scrollLeft >= scrollContTwo.offsetWidth) {

        scrollBox.scrollLeft -= scrollContOne.offsetWidth

      } else {

        scrollBox.scrollLeft += 2;

      }

    }

    let MyScrollRadio = setInterval(scrollRadio, speed);

    scrollBox.onmouseover = function() {

      clearInterval(MyScrollRadio)

    };

    scrollBox.onmouseout = function() {

      MyScrollRadio = setInterval(scrollRadio, speed)

    };

  </script>

</body>

</html>

 

在Vue中使用:

<template>

  <div class="content">

    <ul>

      <div id="scrollBox">

        <div class="scrollWrap">

          <div id="scrollContOne" class="scrollCont">

            <li v-for="item in items">

              <a href="{{item}" target="_blank"></a>

            </li>

          </div>

          <div id="scrollContTwo" class="scrollCont"></div>

        </div>

      </div>

    </ul>

  </div>

</template>

<style scoped>

  .content {

    height: 60px;

    background-color: #2c2c34;

    overflow: hidden;

  }

  .content ul {

    white-space: nowrap;

  }

  .content ul li {

    position: relative;

    font-size: 14px;

    vertical-align: middle;

    line-height: 35px;

    padding: 0 8px;

    white-space: nowrap;

    display: inline-block;

  }

  .content ul li a {

    text-decoration: none;

    color:#fff;
  }

  #scrollBox {

    overflow: hidden;

    margin-left: 36px;

  }

  #scrollBox .scrollWrap {

    width: 500%

  }

  .scrollCont {

    float: left;

  }

</style>

<script>

export default {

  data: () => ({

    canScrollTimer: 0

  }),

  methods: {

    run() {

      let speed = 40;

      let scrollBox = document.getElementById("scrollBox");

      let scrollContOne = document.getElementById("scrollContOne");

      let scrollContTwo = document.getElementById("scrollContTwo");

      scrollContTwo.innerHTML = scrollContOne.innerHTML;

      scrollBox.scrollLeft = 0;

      function scrollRadio() {

        if (scrollBox.scrollLeft >= scrollContTwo.offsetWidth) {

          scrollBox.scrollLeft -= scrollContOne.offsetWidth

        } else {

          scrollBox.scrollLeft += 2;

        }
      }

      let MyScrollRadio = setInterval(scrollRadio, speed);

      scrollBox.onmouseover = function() {

        clearInterval(MyScrollRadio)

      };

      scrollBox.onmouseout = function() {

        MyScrollRadio = setInterval(scrollRadio, speed)

      };

    },

    canScroll() {

      let innerHTML = document.getElementById("scrollContOne").innerHTML;

      if (innerHTML != null && innerHTML != ‘‘ && innerHTML.trim().length != 0) {

        clearInterval(this.canScrollTimer);

        this.run();

      } else {

        let self = this;

        this.canScrollTimer = setInterval(function() {

          self.canScroll()

        }, 1000);

      }

    }

  },

  ready() {

    this.canScroll();

  }

}

</script>

Js跑马灯效果 && 在Vue中使用

标签:innerhtml   class   elf   cti   timer   margin   run   tar   ati   

原文地址:http://www.cnblogs.com/Man-Dream-Necessary/p/6407131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!