博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Js跑马灯效果 && 在Vue中使用
阅读量:6821 次
发布时间:2019-06-26

本文共 4104 字,大约阅读时间需要 13 分钟。

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

  }),

   vuex: {

            getters: {
                scrollLists: state => state.scrollLists
            }
        },

  watch:{

   scrollLists:{

    deep:true,

    handler(v,ov){

      if(v.length){

          this.run();

        }

      }

    } 

  },

  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)

      };

    }

  },

  ready() {

    //接口调用

  }

}

</script>

转载于:https://www.cnblogs.com/Man-Dream-Necessary/p/6407131.html

你可能感兴趣的文章
有趣的Linux命令行功效
查看>>
解决PotPalyer不能拖放播放
查看>>
Linux安装mysql5.7
查看>>
HIVE常用操作以及函数
查看>>
【优达学城测评】SQL 支持许的数据类型(3)
查看>>
PHP CURL CURLOPT参数说明(curl_setopt)
查看>>
Learning NodeJs(1)
查看>>
怎么解决mysql远程连接报10038的错误
查看>>
js 父窗口可以找到子窗口的元素
查看>>
从FB10.3升级到11.0后几个问题的解决
查看>>
django使用MySql的基本步骤
查看>>
笔记《Java并发编程实战》[2]
查看>>
fpdf基本用法
查看>>
Linux下使用pure-ftpd建立匿名ftp访问
查看>>
PhalApi:[1.11] 快速入门: 接口开发示例 源码 图文
查看>>
分享插件
查看>>
HTML 页面中Buton 按钮提交,一个很坑的问题
查看>>
kitchen测试salt-formulas
查看>>
拿Nginx 部署你的静态网页
查看>>
23种设计模式
查看>>