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>
相关推荐
嘉琪0011 小时前
uni-app 核心坑点及解决方案——2026 0309
uni-app
行者-全栈开发4 小时前
uni-app 审批流程组件封装:打造企业级工作流可视化方案
uni-app
2501_916008896 小时前
iPhone 上怎么抓 App 的网络请求,在 iOS 设备上捕获网络请求
android·网络·ios·小程序·uni-app·iphone·webview
jingling5556 小时前
无需重新安装APK | uni-app 热更新技术实战
前端·javascript·前端框架·uni-app·node.js
遇见小美好y6 小时前
uniapp 实现向下追加数据功能
前端·javascript·uni-app
行者-全栈开发7 小时前
43 篇系统实战:uni-app 从入门到架构师成长之路
前端·typescript·uni-app·vue3·最佳实践·企业级架构
00后程序员张8 小时前
iOS上架工具,AppUploader(开心上架)用于证书生成、描述文件管理Xcode用于应用构建
android·macos·ios·小程序·uni-app·iphone·xcode
万物得其道者成9 小时前
uniapp 滑动过快 onReachBottom 事件不触发
uni-app
2501_915921439 小时前
只有 IPA 没有源码时,如何给 iOS 应用做安全处理
android·安全·ios·小程序·uni-app·iphone·webview