uniapp实现 uview1 u-button的水波纹效果

说明:

由于uview2已经移除水波纹效果,这边又觉得那个效果好看,所以开发这个功能(原谅我不会录动图)

效果:

具体代码:

javascript 复制代码
<view class="ripple-container" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
	<view class="btn">
		<text>登录</text>
	</view>
	<!-- 水波纹元素 -->
	<view v-if="showRipple" class="ripple-effect" :style="{
        left: rippleX + 'px',
        top: rippleY + 'px',
        width: rippleSize + 'px',
        height: rippleSize + 'px'
      }"></view>
</view>


<script>
	export default {
		data() {
			return {
				// 水波纹
				showRipple: false,
				rippleX: 0,
				rippleY: 0,
				rippleSize: 0 // 根据按钮大小动态计算
			};
		},
		methods: {
			handleTouchStart(event) {
				// 获取按钮布局信息
				const query = uni.createSelectorQuery().in(this);
				query.select('.ripple-container').boundingClientRect(rect => {
					if (!rect) return;
					// 计算点击位置(相对于按钮)
					const touch = event.touches[0];
					this.rippleX = touch.clientX - rect.left;
					this.rippleY = touch.clientY - rect.top;
					// 计算波纹大小(取按钮宽高中的最大值)
					this.rippleSize = Math.max(rect.width, rect.height);
					// 触发动画
					this.showRipple = true;
				}).exec();
			},
			handleTouchEnd() {
				// 动画结束后重置状态
				setTimeout(() => {
					this.showRipple = false;
				}, 600); // 与动画时间保持一致
			}
		}
	}
</script>

<style lang="scss" scoped>
		.btn {
			border: none;
			border-radius: 10rpx;
			color: #fff;
			background-color: #3f39ff;
			padding: 10rpx 20rpx;
		}

		// 水波纹样式
		.ripple-container {
			position: relative;
			display: inline-block;
			overflow: hidden;
			@keyframes ripple {
				from {
					transform: translate(-50%, -50%) scale(0);
					opacity: 1;
				}

				to {
					transform: translate(-50%, -50%) scale(2);
					opacity: 0;
				}
			}
			.ripple-effect {
				position: absolute;
				z-index: 0;
				background: rgba(0, 0, 0, 0.15);
				border-radius: 100%;
				background-clip: padding-box;
				transform: translate(-50%, -50%);
				pointer-events: none;
				user-select: none;
				transform: scale(0);
				opacity: 1;
				transform-origin: center;
				animation: ripple 0.6s ease-out;
			}
		}
</style>
相关推荐
骨子里的偏爱5 小时前
uniapp实现数据存储到本地文件,除非卸载app,否则数据一直存在
javascript·chrome·uni-app
_Jyann_8 小时前
uniapp两种方式实现自定义tabbar
前端·javascript·uni-app
郑州光合科技余经理8 小时前
PHP技术栈:上门系统海外版开发与源码解析
java·开发语言·javascript·git·uni-app·php·uniapp
2501_915921438 小时前
Fiddler抓包工具详解,HTTPHTTPS调试、代理配置与接口分析实战教程
服务器·ios·小程序·fiddler·uni-app·php·webview
undsky8 小时前
【RuoYi-SpringBoot3-UniApp】:一套代码,多端运行的移动端开发方案
前端·uni-app
diygwcom9 小时前
UniApp 鸿蒙NEXT蓝牙连接及数据写入
华为·uni-app·harmonyos
00后程序员张9 小时前
数据流抓包实战指南,TCPUDP 流量分析、HTTPS 解密与多工具协同方案
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159214310 小时前
iOS 性能分析工具全景解析,构建从底层诊断到真机监控的多层级性能分析体系
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090610 小时前
如何防止 IPA 被反编译,从攻防视角构建一套真正有效的 iOS 成品保护体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9160074710 小时前
专业的 IPA 处理工具 构建可维护、可回滚的 iOS 成品加工与加固流水线
android·ios·小程序·https·uni-app·iphone·webview