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

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

说明:

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

效果:

水波纹

具体代码:

<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>

http://www.kler.cn/a/584677.html

相关文章:

  • Jump Desktop for Mac v9.0.94 优秀的远程桌面连接工具 支持M、Intel芯片
  • 数据结构——顺序表seqlist
  • PostgreSQL16 的双向逻辑复制
  • Netty基础—4.NIO的使用简介一
  • 【贪心算法5】
  • 使用DeepSeek完成一个简单嵌入式开发
  • 如何优化AI模型的Prompt:深度指南
  • 基于jvisualvm的内存监控与远程连接配置指南
  • K8s 1.27.1 实战系列(十)PV PVC
  • C# Unity 唐老狮 No.9 模拟面试题
  • Vue:其他指令
  • Qt的QMenu 和 QAction的样式设置
  • golang从入门到做牛马:第二十篇-Go语言接口:行为的“契约”
  • C#类型转换大总结
  • 4.3 数组和集合的初始及赋值
  • 云原生可观测性:智能运维的数据中枢
  • DeepSeek-R1深度解读
  • HarmonyOS开发 - 电商App实例三( 网络请求axios)
  • nginx中proxy_pass和root的区别
  • STL-List模拟