weex自动来回滚动公告组件

项目中最近又个业务,要写一个自动滚动的组件,本想找一个现成的组件,但是找不到,然后就开始造轮子了

<!--
  组件名 Notice  通知

  属性:
    data: String  文字内容
    url:String 要跳转到哪个页面

-->
<template>
  <div class="notice">
  <!-- 左侧图片 -->
    <image class="notice-icon" :src="notice"></image>
    <scroller
            ref="scroller"
            class="notice-left"
            scroll-direction="horizontal"
            :offset-accuracy="1"
            :scrollToBegin="true"
            :show-scrollbar="false"
            @scroll="handleScroll"
    >
      <text ref="scrollStart"></text>
      <text ref="scrollText" id="scrolltext" class="notice-text">{{ data.replace(/[\n\r]/g,' ') }}</text>
    </scroller>
    <!-- 滚动区域遮罩层,使用scroller为了防止用户手动滑动,因为手动滑动在android上有问题 -->
    <scroller class="notice-mask"></scroller>
    <div class="notice-right">
      <text class="notice-right-text" @click="toNoticeDetail">详情>></text>
    </div>
  </div>
</template>
<script>
  import { navigator } from '../../utils'
  import { notice } from '../../images/myIcon.js'
  import modal from '../../utils/modal'

  const dom = weex.requireModule('dom') || {}
  export default {
    name: 'Notice',
    props: {
      data: {
        type: String,
        default: ''
      },
      url: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        notice,
        time: 60, // 每隔多少ms滚动一次
        scrollLeft: 0,  // 滚动偏移量
        isIos: false, // 是否是IOS
        maxScroll: 10, //  滚动元素内容的宽度,默认100, 否则无法自动滚动
        isScroll: false,  // 是否触发滚动事件
        toRight: true // true:从左往右滚动,false:从右往左滚动
      }
    },
    mounted () {
      // 判断是否是iOS
      this.isIos = weex.config.env.platform.toLowerCase().indexOf('ios') > -1
      this.autoScroll()
    },
    methods: {
      /**
       * 跳转事件
       */
      toNoticeDetail () {
        navigator.push({
          url: this.url
        })
      },
      /**
       * 自动滚动
       */
      autoScroll () {
        // 获取scroller
        let el = this.$refs.scrollStart
        const timer = setInterval(() => {
          // 获取公告内容元素
          const el1 = this.$refs.scrollText
          dom.getComponentRect(el1, function (ops) {
            // 获取内容的宽度,如果小于 550 则不用滚动
            if (ops.size.width < 550) {
              clearInterval(timer)
            }
          })
          // 如果是向右侧滚动,且 maxScroll 不为10(10是默认初始值),且 scrollLeft + 520 > maxScroll 则将 offset 设为 scrollLeft + 520
          // 应该是550,即 scroll 标签的宽度,设置 520 是当滚动到最右侧的时候,暂停一会儿
          if (this.toRight && this.maxScroll !== 10 && this.scrollLeft + 520 > this.maxScroll) {
            dom.scrollToElement(el, {offset: this.scrollLeft += 520})
          }
          // this.$emit('width', this.maxScroll)
          // 如果滚动的偏移量小于等于元素的宽度,即最大滚动区域maxScroll,则设为向左滚动
          if (this.toRight && this.scrollLeft <= this.maxScroll) {
            this.toRight = true
            // 如果偏移量为0,则暂停2s
            if (this.scrollLeft === 0) {
              setTimeout(() => {
                dom.scrollToElement(el, {offset: this.scrollLeft += 1})
              }, 1000)
            } else {
              // 否则偏移量直接 + 1
              dom.scrollToElement(el, {offset: this.scrollLeft += 1})
            }
          } else {
            // 否则,如果是向右滚动,且 maxScroll 不为 10,即滚动完毕,则把 scrollLeft - 520
            if (this.toRight && this.maxScroll !== 10) {
              dom.scrollToElement(el, {offset: this.scrollLeft -= 520})
            }
            // 方向设为向左滚动
            this.toRight = false
            if (this.scrollLeft <= 0) {
              setTimeout(() => {
                this.toRight = true
              }, 1000)
            } else {
              // 否则偏移量直接 + 1
              dom.scrollToElement(el, {offset: this.scrollLeft -= 1})
            }
          }
        }, this.time)
      },
      handleScroll (e) {
        // 如果是IOS,maxScroll + 20,ios 和 android 差 19
        if (this.isIos) {
          this.maxScroll = parseInt(e.contentSize.width + 1) + 20
        } else {
          this.maxScroll = parseInt(e.contentSize.width + 1)
        }
      }
    }
  }
</script>

<style scoped>
  .notice {
    width: 750px;
    height: 67px;
    background-color: rgba(254, 236, 191, 1);
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
  .notice-mask {
    position: absolute;
    top: 0;
    left: 70px;
    width: 560px;
    height: 67px;
  }

  .notice-left {
    width: 550px;
    height: 67px;
    flex-direction: row;
    align-items: center;
  }

  .notice-text {
    color: #2E2E2E;
    font-size: 24px;
    color: rgba(234, 138, 48, 1);
  }

  .notice-right-text {
    color: #f78c3d;
    font-size: 24px;
    padding-right: 20px;
    color: rgba(68, 104, 136, 1);
  }

  .notice-icon {
    width: 30px;
    height: 30px;
    margin-left: 20px;
    margin-right: 10px;
  }
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文章是我最近在公司的一场内部分享的内容。我有个习惯就是每次分享都会先将要分享的内容写成文章。所以这个文集也是用来...
    Awey阅读 9,483评论 4 67
  • 首先为什么需要组件化? 下面我列举了一些比较常见的问题 业务模块划分不清楚,各模块之间耦合度很大,难以维护例如我们...
    CrystalZhu阅读 1,023评论 0 2
  • 插花为什么光看教材的花型图会睡着,因为人家是设计图,是理科好吗。所以才要跟师。老师引进门先,修行靠自己。 ...
    食有真香阅读 625评论 0 0
  • 我国历史悠久,历史上的美女达人数不胜数。她们是怎样保护自己的皮肤容颜娇美的呢?下面我就给大家介绍一下这些古代...
    A甲骨文之音阅读 283评论 0 2
  • “局”的两个概念: 明局:就是你一眼看上去的那样一个态势,那样一种关系。 暗局:在这些明摆着的格局背后,或者反过来...
    芳芳F阅读 2,289评论 0 0