动手吧,vue移动端消息滚动组件

先看效果图:

1、模板部分

<transition name="fade-sport"><div class="v-message-roll" v-show="visible"><svg class="v-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7405"><pathd="M594.917478 144.398738c0-43.803645-37.123502-79.478146-82.916455-79.478146-45.699832 0-82.918501 35.585473-82.918501 79.478146 0 4.472871 0.38681 8.860808 1.12973 13.133112-114.497731 38.254256-208.430076 155.73083-208.430076 294.718325l0 109.423155c0 0 0 157.590178-40.387849 158.963455-24.082488 0-42.528606 17.792225-42.528606 39.74112 0 22.098297 18.557658 39.737026 41.452087 39.737026l663.36643 0c23.004947 0 41.451064-17.791202 41.451064-39.737026 0-22.103414-18.557658-39.74112-41.451064-39.74112-41.46539 0-41.46539-157.854191-41.46539-157.854191L802.218848 452.263477c0-139.166573-87.828324-256.691243-208.408587-294.828842C594.538855 153.190985 594.917478 148.838863 594.917478 144.398738zM636.377752 839.789535c-0.12382 65.903989-55.286164 119.28885-124.377752 119.28885-68.616774 0-124.254955-53.163827-124.378775-119.28885L636.377752 839.789535z"fill="#f9a60a"p-id="7406"></path></svg><div class="v-content"><ul class="v-content-list" ref="listRef" @touchstart="touchstart" @touchend="touchend"><li class="v-content-item" ref="itemsRef" v-for="(item, index) in contentComputed" :key="index">{{ item }}</li></ul></div><svgv-if="showCloseIcon"@click="close"class="v-icon"viewBox="0 0 1024 1024"version="1.1"xmlns="http://www.w3.org/2000/svg"p-id="8743"><pathd="M576 512l277.333333 277.333333-64 64-277.333333-277.333333L234.666667 853.333333 170.666667 789.333333l277.333333-277.333333L170.666667 234.666667 234.666667 170.666667l277.333333 277.333333L789.333333 170.666667 853.333333 234.666667 576 512z"fill="#f9a60a"p-id="8744"></path></svg></div></transition>

2、css部分

.v-message-roll {width: 100%;display: flex;align-items: center;justify-content: space-between;transition: opacity 0.2s;.v-icon {width: 20px;height: 20px;flex-shrink: 0;vertical-align: middle;fill: currentColor;overflow: hidden;}.v-content {flex: 1;width: 0;.v-content-list {width: 100%;overflow-x: auto;white-space: nowrap;&::-webkit-scrollbar {display: none;}.v-content-item {display: inline-block;color: #db8412;}}}
}.fade-sport-enter,
.fade-sport-leave {opacity: 0;
}

3、js部分

const SPEED = {FAST: 0.6,MIDDLE: 0.4,SLOW: 0.3,
};
export default {data() {return {visible: true,animationFrameTimer: null,touchTimeout: null,refreshRateTimer: null,};},props: {content: [String, Array],touchStop: {type: Boolean,default: false,},touchStopDuration: {type: Number,default: 1500,},showCloseIcon: {type: Boolean,default: true,},},watch: {content: {handler() {this.reset();this.$nextTick(() => {this.init();});},deep: true,},},computed: {contentComputed() {if (Object.prototype.toString.call(this.content) === "[object Array]") {return this.content;}return [this.content];},},mounted() {this.calcScreenRefreshRate().then((rate) => {this.screenRefreshRate = rate;this.init();});},methods: {init() {const itemsRef = this.$refs.itemsRef;const scrollW = this.$refs.listRef.scrollWidth;this.scrollW = scrollW;if (itemsRef.length) {this.itemsRef = this.$refs.itemsRef;this.setInterval();}},reset() {this.$refs.listRef.scrollLeft = 0;this.cancelAnimationFrame();},calcScreenRefreshRate() {return new Promise((resolve) => {let screenRefreshRate = 0;const animationFrameFunc = () => {screenRefreshRate += 2;this.refreshRateTimer = window.requestAnimationFrame(animationFrameFunc);};setTimeout(() => {window.cancelAnimationFrame(animationFrameFunc);resolve(screenRefreshRate);}, 500);animationFrameFunc();});},setInterval(num) {this.animationFrameTimer = window.requestAnimationFrame(() => this.roll(num));},roll(num = 0) {const step = this.calcStep();this.itemsRef.forEach((ele) => {ele.style.transform = `translateX(-${num}px)`;});this.animationFrameTimer = window.requestAnimationFrame(() =>this.roll(num < this.scrollW ? num + step : 0));},calcStep() {const screenRefreshRate = this.screenRefreshRate;let step = 0;if (screenRefreshRate < 90) {step = SPEED["FAST"];} else if (screenRefreshRate < 120) {step = SPEED["MIDDLE"];} else {step = SPEED["SLOW"];}return step;},touchstart() {if (!this.touchStop) {this.$refs.listRef.style.overflow = "hidden";} else {this.itemsRef.forEach((ele) => {ele.style.transform = `translateX(0)`;});this.cancelAnimationFrame();window.clearTimeout(this.touchTimeout);}},touchend() {if (this.touchStop) {const scrollLeft = this.$refs.listRef.scrollLeft;this.touchTimeout = setTimeout(() => {this.$refs.listRef.scrollLeft = 0;this.setInterval(scrollLeft);}, this.touchStopDuration);}},close() {this.visible = false;this.$emit("close");this.cancelAnimationFrame();},cancelAnimationFrame() {window.cancelAnimationFrame(this.animationFrameTimer);window.cancelAnimationFrame(this.refreshRateTimer);},},deactivated() {this.cancelAnimationFrame();},beforeDestroy() {this.cancelAnimationFrame();},
};

4、参数和事件

可以的话,点个赞嘛

## 属性content: 内容。参数类型:string|arraytouchStop: 触摸是否停止滚动。参数类型:boolean, 默认 falsetouchStopDuration: 触摸停止滚动持续时长(ms)。参数类型:number, 默认1500showCloseIcon:是否显示关闭按钮。参数类型:boolean, 默认 true## 事件close:关闭事件

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/59015.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

选读SQL经典实例笔记20_Oracle语法示例

1. 计算一年有多少天 1.1. sql select Days in 2005: ||to_char(add_months(trunc(sysdate,y),12)-1,DDD)as reportfrom dualunion allselect Days in 2004: ||to_char(add_months(trunc(to_date(01-SEP-2004),y),12)-1,DDD)from dual REPORT ----------------- Days in 200…

开发工具Eclipse的使用

&#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于Eclipse使用的相关操作吧 目录 &#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 一.Eclipse是什么 二.使用Eclipse的…

一百四十一、Kettle——kettle8.2在Windows本地开启carte服务以及配置子服务器

一、目的 在kettle建好共享资源库后&#xff0c;为了给在服务器上部署kettle的carte服务躺雷&#xff0c;先在Windows本地测试一下怎么玩carte服务 二、Kettle版本以及在Windows本地安装路径 kettle版本是8.2 pdi-ce-8.2.0.0-342 kettle本地安装路径是D:\j…

安全渗透知识总结二

目录 一、html实体编码 1、Unicode字符编码 2、字符的数字表示 3、常见实体编码 4、url 协议 主机 http状态码 http常用的状态码 端口 常见协议端口 查询参数 锚点 url字符 urlcode字符 绝对url和相对url 二、字符编码 Ascll字符集 html字符集 html的url编码 …

RabbitMQ的6种工作模式

RabbitMQ的6种工作模式 官方文档&#xff1a; http://www.rabbitmq.com/ https://www.rabbitmq.com/getstarted.html RabbitMQ 常见的 6 种工作模式&#xff1a; 1、simple简单模式 1)、消息产生后将消息放入队列。 2)、消息的消费者监听消息队列&#xff0c;如果队列中…

【Spring Boot】Spring Boot 集成 RocketMQ 实现简单的消息发送和消费

文章目录 前言基本概念消息和主题相关发送普通消息 发送顺序消息RocketMQTemplate的API介绍参考资料&#xff1a; 前言 本文主要有以下内容&#xff1a; 简单消息的发送顺序消息的发送RocketMQTemplate的API介绍 环境搭建&#xff1a; RocketMQ的安装教程&#xff1a;在官网…

jmeter如何压测和存储

一、存储过程准备&#xff1a; 1、建立一个空表&#xff1a; 1 CREATE TABLE test_data ( id NUMBER, name VARCHAR2(50), age NUMBER ); 2、建立一个存储过程&#xff1a; 1 2 3 4 5 6 7 8 9 CREATE OR REPLACE PROCEDURE insert_test_data (n IN NUMBER) AS BEGIN --E…

Kubernetes工作原理

一、案例概述 传统部署时代&#xff1a; 早期是在物理服务器上运行应用程序。无法为物理服务器中的应用程序定义资源边界&#xff0c;这会导致资源分配出现问题。例如&#xff1a;如果在物理服务器上运行多个应用程序&#xff0c;则可能会出现一个应用程序占用大部分资源的情况…

基于2.4G RF开发的无线游戏手柄解决方案

平时喜欢玩游戏的朋友&#xff0c;肯定知道键鼠在某些类型的游戏适配和操作方面&#xff0c;不如手柄。作为一个游戏爱好者&#xff0c;还得配上一个游戏手柄才行。比如动作和格斗、体育游戏&#xff0c;由于手柄更合理的摇杆位置和按键布局&#xff0c;操作起来也是得心应手。…

【大数据】Flink 从入门到实践(一):初步介绍

Flink 从入门到实践&#xff08;一&#xff09;&#xff1a;初步介绍 Apache Flink 是一个框架和分布式处理引擎&#xff0c;用于在 无边界 和 有边界 数据流上进行 有状态 的计算。Flink 能在所有常见集群环境中运行&#xff0c;并能以内存速度和任意规模进行计算。 1.架构 1…

如何用看板让你的项目管理更上一层楼

项目管理的核心挑战 项目管理始终是一个充满挑战的领域。在多变的环境中&#xff0c;管理一个项目并确保其成功完成是一项巨大的任务。那么&#xff0c;为什么项目管理会如此复杂呢&#xff1f; 概述项目的复杂性 每一个项目都有其独特性&#xff0c;无论是项目的规模、团队…

视觉大模型的全面解析

前言 本文主要围绕Foundational Models&#xff0c;即基础模型&#xff08;通过自监督或半监督方式在大规模数据上训练的模型&#xff0c;可以适应其它多个下游任务。&#xff09;这个概念&#xff0c;向大家全面阐述一个崭新的视觉系统。例如&#xff0c;通过 SAM&#xff0c;…