vue插件--滚动信息插件

基础要求

知道如何使用vue-cli
熟悉vue1.0、了解sass

准备工作

使用vue-cli安装vue1.0 webpack框架
安装node-sass、sass-loader

插件编写

滚动信息插件主要分为三部分,一是滚动信息样式,二是滚动动画的实现,三是信息列表的处理

样式编写

show-notice固定高度,并且设置overflow: hidden;只有notice-list与show-notice重叠的部分才会显示出来。noticePosition设置notice-list位置,可以通过noticePosition控制显示信息。

<template>
  <div class="show-notice">
    <div class="notice-list" :style="{transform: 'translateY(-'+noticePosition+'px) translateZ(0px)'}" v-el:roll>
      <p v-for="notice in notices" track-by="$index">{{notice}}</p>
    </div>
  </div>
</template>
<style lang="scss" scoped>
  $notice-height: 30px;
  .show-notice{
    height: $notice-height;
    overflow: hidden;
    vertical-align: middle;
    .notice-list p{
      margin: 0;
      height: $notice-height;
      line-height: $notice-height;
    }
  }
</style>

实现滚动动画

信息滚动流程为:停止=>滚动=>停止,当滚动到最后一条的信息时,又滚动回第一条信息,循环往复。因为滚动动画的效果应该是一致的,所以最后一条信息滚动回第一条信息的效果也应该和其他信息滚动的效果一致,为了实现这样的效果,我们要对信息列表进行处理,将列表的第一条信息插入到列表最后的位置上。当列表滚动到最后位置后,直接将列表位置设置为开始位置。

 data () {
    return {
      noticePosition: 0 // 列表位置
    }
  },
  compiled () {
    let destination = 30
    setInterval(() => {
      if (destination / 30 < this.notices.length) {
        this.move(destination, 500)
        destination += 30
      } else { // 列表到底
        this.noticePosition = 0 // 设置列表为开始位置
        destination = 30
        this.move(destination, 500)
        destination += 30
      }
    }, 2500)
  },
  methods: {
    move (destination, duration) { // 实现滚动动画
      let speed = ((destination - this.noticePosition) * 1000) / (duration * 60)
      let count = 0
      let step = () => {
        this.noticePosition += speed
        count++
        console.log(this.noticePosition)
        rAF(() => {
          if (this.noticePosition < destination) {
            step()
          } else {
            this.noticePosition = destination
          }
        })
      }
      step()
    }
  }

处理信息列表

之前提到要实现循环滚动效果,我们需要对信息列表进行处理,处理很简单,将列表的第一条信息插入到列表最后的位置上就可以了,我们在props中实现:

props: {
    notices: {
      type: Array,
      required: true,
      coerce (data) {
        data.push(data[0])
        return data
      }
    }
  },

其他

git:https://github.com/x007xyz/vue-roll-notice.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • “别跑!”“别跑那么快啊!”完全跟不上他,怎麽冲他喊都没有用。手上的棍子往地上一敲他就老实了。两只招风耳怂怂的耷拉...
    默默不吃肉阅读 1,937评论 2 1
  • 年年春草绿,年年秋风起。一年又一年,二十有三忆。现在的我没有了那种真正畅享的笑容,过多的都是所谓的礼仪之笑,也不知...
    念江子阅读 3,523评论 0 2
  • 《冥想五分钟等于熟睡一小时》一书中告诉了我们冥想的好处。 什么时候适合冥想了,冥想其实可以无处不在只...
    诚思心梦阅读 1,488评论 0 2
  • 那时的她是最好的她,后来的我是最好的我,可是最好的我们之间,隔了一整个青春。怎么奔跑也跨不过的青春,只好伸出手道别...
    行走的包小姐阅读 4,585评论 1 7
  • 依然很清楚的讽刺这个世界的不公 也是新的一个角色 有些开心 有些激动 更多的是幸福,真真切切的幸福。 现实让我成长...
    ivcc阅读 2,490评论 0 6