当前位置: 首页 > news >正文

uniapp 悬浮球

悬浮球组件

在这里插入图片描述

<template>
	<view
		class="hover_ball_cell"
		:style="{ width: `${diameter}px`, height: `${diameter}px`, top: `${top}px`, left: `${left}px` }"
		@touchmove.prevent="touchmove"
		@touchend="touchend"
		@touchcancel="touchcancel"
		@tap="onTap"
		:animation="ballAnimation"
	>
		<view v-if="iconUrl == ''" class="wave" />
		<image v-else :src="iconUrl"  mode=""></image>
	</view>
</template>

<script>
export default {
	props: {
		iconUrl: {
			type: String,
			default: ''
		},
		 onClickHover: {
		      type: Function,
		      require: true,
		      default: null,
		    },
	},
	data() {
		return {
			diameter: 0,
			top: 0,
			left: 0,
			isMove: false,
			ballAnimation: {},
			timeout: null,
			modile: {},
		};
	},
	name: 'hover-ball',
	created() {
		let _this = this;
		_this.modile = uni.getSystemInfoSync();
		_this.top = _this.modile.safeArea.height - 180;
		_this.left = _this.modile.safeArea.width;
		_this.diameter = _this.modile.screenHeight / 15;
		let create = uni.createAnimation({
			duration: 400,
			timingFunction: 'ease-in'
		});
		create.translate('-100%').step();
		setTimeout(() => {
			// _this.left = _this.modile.safeArea.width
			create.translate('-50%').step();
			_this.ballAnimation = create.export();
		}, 0)
	},
	methods: {
		onTap() {
			let _this = this;
			let x = '0';
			if (2 * _this.left >= _this.modile.safeArea.width) {
				x = '-100%';
			}
			let create = uni.createAnimation({
				duration: 0
			});
			create.translate(x).step();
			_this.ballAnimation = create.export();
			_this.overBall();
			_this.onClickHover()
		},
		touchmove(e) {
			let _this = this;
			_this.isMove = true;
			if (_this.timeout != null) {
				clearTimeout(_this.timeout);
				_this.timeout = null;
			}
			var touch = e.touches[0] || e.changedTouches[0];
			_this.left = touch.clientX;
			_this.top = touch.clientY;
		},
		touchend(e) {
			let _this = this;
			if (!_this.isMove) {
				return;
			}
			_this.finish(e);
		},
		touchcancel(e) {
			let _this = this;
			if (!_this.isMove) {
				return;
			}
			_this.finish(e);
		},
		finish(e) {
			let _this = this;
			_this.isMove = false;
			var touch = e.touches[0] || e.changedTouches[0];
			_this.left = touch.clientX;
			_this.top = touch.clientY;
			let x = '0';
			if (2 * _this.left + _this.diameter >= _this.modile.safeArea.width) {
				_this.left = _this.modile.safeArea.width;
				x = '-100%';
			} else {
				_this.left = _this.modile.safeArea.left;
			}
			if (_this.top > _this.modile.safeArea.height + _this.modile.safeAreaInsets.bottom) {
				_this.top = _this.modile.safeArea.height + _this.modile.safeAreaInsets.bottom; 
			} else if (_this.top < - _this.diameter / 2) {
				_this.top = - _this.diameter / 2;
			}
			let create = uni.createAnimation({
				duration: 0
			});
			console.log(x)
			create.translate(x).step();
			_this.ballAnimation = create.export();
			_this.overBall();
		},
		overBall() {
			let _this = this;
			_this.timeout = setTimeout(() => {
				_this.timeout = null;
				let create = uni.createAnimation({
					duration: 400,
					timingFunction: 'ease-in'
				});
				create.translate('-50%').step();
				_this.ballAnimation = create.export();
			}, 1200);
		}
	}
};
</script>

<style lang="less" scoped>
.hover_ball_cell {
	position: fixed;
	overflow: hidden;
	border-radius: 50%;
	border: 4rpx solid #67c23a;
	background: #ffffff;
	transform: translate(-50%, 0);
	padding: 4rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 100000;

	.wave {
		position: relative;
		width: 100%;
		height: 100%;
		background-image: linear-gradient(-180deg, #aaff80 13%, #67c23a 91%);
		border-radius: 50%;
	}
	
	image{
		width: 60%;
		height: 60%;
	}
}
</style>

使用组件

在这里插入图片描述

<template>
	<view class="version">
		<hover-ball :onClickHover="onClickHover" :iconUrl="iconUrl"/>
	</view>
</template>

<script>
export default {
	data() {
		return {
			iconUrl: '../../static/image/shopping-cart.png'
		};
	},
	onLoad(option) {
	},
	onShow() {
	},
	methods: {
		onClickHover(){
			uni.$u.route('/pages/secondary/shopping-cart');
		},
	}
};
</script>

<style scoped lang="scss">

</style>

相关文章:

  • 统计耗时 System.currentTimeMillis()
  • 阿里巴巴java方向笔试题 带详细答案
  • (二)springcloud实战之config配置中心
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • 升级pip 升级pip3的快速方法
  • 【CDGP】如何识别主数据?
  • 南大通用的GBase 8s 图形化管理工具介绍
  • 通过 MSE 实现基于Apache APISIX的全链路灰度
  • 神经网络优化算法有哪些,人工神经网络优化算法
  • 使用python-docx完成word操作
  • Interactive Tools Recommendation System integrating QT/ROS /Pytorch
  • 嵌入式属于人工智能吗?
  • Linux操作系统——进程控制【一】
  • 鸡葡萄菌感染怎么防治 预防鸡感染球菌病吃啥药
  • 技术分享 | App测试时常用的adb命令你都掌握了哪些呢?
  • @angular/forms 源码解析之双向绑定
  • Android组件 - 收藏集 - 掘金
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • mac修复ab及siege安装
  • Python进阶细节
  • 多线程 start 和 run 方法到底有什么区别?
  • 关于 Cirru Editor 存储格式
  • 机器学习 vs. 深度学习
  • 解析带emoji和链接的聊天系统消息
  • 聚类分析——Kmeans
  • 力扣(LeetCode)965
  • 聊聊flink的TableFactory
  • 前嗅ForeSpider教程:创建模板
  • 巧用 TypeScript (一)
  • 王永庆:技术创新改变教育未来
  • 赢得Docker挑战最佳实践
  • 在electron中实现跨域请求,无需更改服务器端设置
  • const的用法,特别是用在函数前面与后面的区别
  • ​插件化DPI在商用WIFI中的价值
  • #define、const、typedef的差别
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (4)Elastix图像配准:3D图像
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (十三)Maven插件解析运行机制
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (正则)提取页面里的img标签
  • (转)Sublime Text3配置Lua运行环境
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .net FrameWork简介,数组,枚举
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .Net语言中的StringBuilder:入门到精通
  • ??myeclipse+tomcat
  • @ResponseBody
  • [AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)