首页 > 其他分享 >uni-app 滚动通知组件的实现

uni-app 滚动通知组件的实现

时间:2023-04-06 18:33:17浏览次数:32  
标签:notice const 通知 app 滚动 currentIndex scrollTop 组件 uni

uni-app 滚动通知组件的实现

一、实现思路

二、使用scroll-view 组件实现

<template>
  <view class="notice">
    <scroll-view
      class="notice-scroll"
      :scroll-y="true"
      :scroll-with-animation="true"
      :scroll-top="scrollTop"
    >
      <view :animation="animationData" class="notice-content">
        <view
          class="notice-item"
          v-for="(item, index) in noticeList"
          :key="index"
          @tap="handleClickNotice(item)"
        >
          <text>{{ item.text }}</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
  export default {
    name: "notice-bar",
    data() {
      return {
        noticeList: [], // 通知列表
        timer: null, // 定时器
        interval: 2000, // 滚动时间间隔
        scrollTop: 0, // 滚动距离
        currentIndex: 0, // 当前通知索引
      };
    },
    props: {
      notices: {
        // 外部传入的通知列表
        type: Array,
        default: [],
      },
    },
    mounted() {
      this.initNoticeList();
    },
    methods: {
      // 初始化通知列表
      initNoticeList() {
        const _this = this;
        _this.noticeList = _this.notices;
        if (_this.noticeList.length > 1) {
          _this.timer = setInterval(() => {
            _this.handleScrollNotice();
          }, _this.interval);
        }
      },
      // 点击通知时触发
      handleClickNotice(item) {
        this.$emit("click", item);
      },
      // 滚动通知
      handleScrollNotice() {
        const len = this.noticeList.length;
        if (this.currentIndex === len - 1) {
          this.currentIndex = 0;
        } else {
          this.currentIndex++;
        }
        this.animateScroll();
      },
      // 动画滚动
      animateScroll() {
        const _this = this;
        const noticeHeight = 30; // 通知高度,根据实际情况调整
        const scrollTop = _this.currentIndex * noticeHeight;
        _this.scrollTop = scrollTop;
      },
    },
    destroyed() {
      if (this.timer) {
        clearInterval(this.timer);
      }
    },
  };
</script>

<style scoped>
  .notice {
    /* 组件高度,根据实际情况调整 */
    height: 60rpx;
    overflow: hidden;
  }

  .notice-scroll {
    width: 100%;
    height: 100%;
  }

  .notice-content {
    display: flex;
    flex-direction: column;
  }

  .notice-item {
    /* 通知高度,根据实际情况调整 */
    height: 60rpx;
    /* 通知行高,根据实际情况调整 */
    line-height: 60rpx;
    padding-left: 20rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>

这个组件实现了一个纵向滚动的通知列表,可以根据传入的通知列表自动滚动显示。组件初始化时会根据通知列表长度判断是否需要滚动,并设置定时器自动滚动。当用户点击某个通知时,会触发组件的 click 事件,并将点击的通知对象作为参数传递给父组件。组件的动画效果使用了 uni-app 的动画系统,通过创建一个动画对象并设置 translateY 属性来实现纵向滚动效果。

三、使用uni.createAnimationAPI 实现

<template>
  <view class="notice">
    <view :animation="animationData" class="notice-content">
      <view
        class="notice-item"
        v-for="(item, index) in noticeList"
        :key="index"
        @tap="handleClickNotice(item)"
      >
        <text>{{ item.text }}</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "notice-bar",
    data() {
      return {
        noticeList: [], // 通知列表
        animation: null, // 动画对象
        animationData: {},
        timer: null, // 定时器
        interval: 2000, // 滚动时间间隔
        scrollTop: 0, // 滚动距离
        currentIndex: 0, // 当前通知索引
      };
    },
    props: {
      notices: {
        // 外部传入的通知列表
        type: Array,
        default: [],
      },
    },
    mounted() {
      this.initNoticeList();

      const _this = this;
      _this.animation = uni.createAnimation({
        duration: 500,
        timingFunction: "ease-out",
      });
    },
    methods: {
      // 初始化通知列表
      initNoticeList() {
        const _this = this;
        _this.noticeList = _this.notices;
        if (_this.noticeList.length > 1) {
          _this.timer = setInterval(() => {
            _this.handleScrollNotice();
          }, _this.interval);
        }
      },
      // 点击通知时触发
      handleClickNotice(item) {
        this.$emit("click", item);
      },
      // 滚动通知
      handleScrollNotice() {
        const len = this.noticeList.length;
        if (this.currentIndex === len - 1) {
          this.currentIndex = 0;
        } else {
          this.currentIndex++;
        }
        this.animateScroll();
      },
      // 动画滚动
      animateScroll() {
        const _this = this;
        const noticeHeight = 30; // 通知高度,根据实际情况调整
        const scrollTop = _this.currentIndex * noticeHeight;

        if (scrollTop === 0) {
          _this.animation.translateY(-scrollTop).step({
            duration: 0,
          });
        } else {
          _this.animation.translateY(-scrollTop).step();
        }
        _this.animationData = _this.animation.export();
      },
    },
    destroyed() {
      if (this.timer) {
        clearInterval(this.timer);
      }
      if (this.animation) {
        this.animation = null;
      }
    },
  };
</script>

<style scoped>
  .notice {
    /* 组件高度,根据实际情况调整 */
    height: 60rpx;
    overflow: hidden;
  }

  .notice-scroll {
    width: 100%;
    height: 100%;
  }

  .notice-content {
    display: flex;
    flex-direction: column;
  }

  .notice-item {
    /* 通知高度,根据实际情况调整 */
    height: 60rpx;
    /* 通知行高,根据实际情况调整 */
    line-height: 60rpx;
    padding-left: 20rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>

标签:notice,const,通知,app,滚动,currentIndex,scrollTop,组件,uni
From: https://www.cnblogs.com/yuzhihui/p/17293064.html

相关文章

  • 【C#】Npoi.Mapper 具体操作方法
    前言    我们在日常开发中对Excel的操作可能会比较频繁,好多功能都会涉及到Excel的操作。在.NetCore中大家可能使用Npoi比较多,这款软件功能也十分强大,而且接近原始编程。但是直接使用Npoi大部分时候我们可能都会自己封装一下,毕竟根据二八原则,我们百分之八十的场景可能都是进......
  • 半天时间写完一个案例,循序渐进的掌握uni-app,使用uni-app完成一个简单项目——新闻列表
    一、创建项目uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、Web(响应式)、以及各种小程序(微信/支付宝/百度/头条/飞书/QQ/快手/钉钉/淘宝)、快应用等多个平台。uni-app在手,做啥都不愁。即使不跨端,uni-app也是更好的小程序开发框架(......
  • 简洁的看小说app
    一、软件的下载1.关注大佬的微信公众号[开源阅读软件]1.1,如下图,点击软件下载,会弹出下载链接,使用浏览器打开下载链接进行下载安装下载地址1:https://www.coolapk.com/apk/256030下载地址2:https://github.com/gedoor/legado/releases下载地址3:https://yd.mgz6.cc ......
  • uniapp 如何接入 airwallex的支付
    在uni-app中接入Airwallex的支付,需要进行如下步骤:在你的uni-app项目中引入AirwallexSDK。importairwallexfrom'airwallex-payment-widgets';初始化Airwallex支付控件。constpaymentMethodConfig={env:'staging',//环境,包括“staging”和“production”......
  • 2023.04.06 - vue组件中动态指定监听的值
    业务场景:高拍仪给出的视频信息API回调里会不断返回图像数据。因为有主副摄像图像信息,并且两个图像信息会二选一展示在DOM容器里。所以就是二对一的关系。//主摄像数据letpriPic:string='';//副摄像数据letsubPic:string='';//展示在容器的数据=主摄像数据||副摄像......
  • 小程序原生 转 uni-app
    1.下载miniprogram-to-uniappnpminstallminiprogram-to-uniapp-g 2.项目下打开终端wtu-i"你的小程序项目路径"  如:wtu-i "G:wx_project"  回车后:在项目根目录出现一个后缀为_uni的目录--就是转换后的uni-app项目 ......
  • 可视化组件g2之分组箱型图、柱形图、散点图简单使用
    <!--引入G2文件--><scriptsrc="./plugins/g2.v5.min.js"></script><style> .container{  display:flex; } .div{  height:500px; }</style><!--创建图表容器--><divclass="container"> ......
  • 二次封装ui组件,如何做到属性,作用域插槽以及 实例方法的穿透使用
    A页面:在使用二次封装的组件<MyInputref='inputRef'v-model='data'placeholder='xxxx'><template#prepend>......</template><template#append>......</template></MyInput>......
  • 2023.04.06 - 使用mixin动态混入,对vue组件中的数据做兼容经验总结(xp)
    业务场景:在一个高拍仪的硬件设备中,厂家给了两套不同的API,分别支持winXP和winXP以上版本的系统,而这两套API的实现方式截然不同,一套使用的是http通信,一套是使用scoket通信,方法的调用自然也是不同。我需要在同一组件兼容这两套代码。这种需求下很明显,我除了修改组件里的函数方法,......
  • 直播平台软件开发,简单易修改的弹框组件
    直播平台软件开发,简单易修改的弹框组件弹窗组件适用框架vue,uniapp使用再uniapp框架中可简单修改标签与尺寸单位后使用px与rpx <!--vue--><template><divv-show="ishide"@touchmove.stop.prevent><!--遮罩--><divclass="mask":style="maskStyle">......