微信小程序点击触发水波纹容器组件

作者: 小枫枫

临枫的项目经历分享给你们啦~

扫码交朋友

标签:

特别声明:文章有少部分为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

 

123.gif

使用 index.wxml  index.json

<water-ripple-container>
    <view>
         啦啦啦啦啦啦
    </view>
</water-ripple-container>


//json引入组件


{
    "component": true,
    "usingComponents": {
        "water-ripple-container": "/pages/_components/water-ripple-container/water-ripple-container"
    }
}

 

 

 

 

 

water-ripple-container.wxml

 

<view class="container" bindtouchstart="containerTap">
    <slot></slot>
    <view class="ripple" style="{{rippleStyle}}"></view>
</view>

 

water-ripple-container.wxss

 

.container {
  overflow: hidden;
  position: relative;
}
.ripple {
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 100%;
  height: 10px;
  width: 10px;
  position: absolute;
  transform: scale(0);
}
@keyframes ripple {
  100% {
    transform: scale(80);
    transform: scale(80);
    background-color: transparent;
  }
}

 

water-ripple-container.js

 

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        rippleStyle: '',
        rippleTop: 0,
        rippleLeft: 0
    },

    /**
     * 组件的方法列表
     */
    methods: {
        containerTap(e) {
            let x = e.touches[0].pageX;
            this.getContainerRect(rect => {
                let min = 20;
                let max = 80;
                let randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
                let rippleTop = randomNumber;
                let rippleLeft = x - rect.left;
                let rippleStyle = `top: ${rippleTop}%; left: ${rippleLeft}px; animation: ripple 0.5s linear;`;
                this.setData({
                    rippleStyle,
                    rippleTop,
                    rippleLeft
                });
                setTimeout(() => {
                    this.setData({
                        rippleStyle: ''
                    });
                }, 500);
            });
        },

        getContainerRect(callback) {
            let query = this.createSelectorQuery();
            query.select('.container').boundingClientRect();
            query.exec(rects => {
                if (typeof callback === 'function') {
                    callback(rects[0]);
                }
            });
        }
    }
})
分享到:
打赏

作者: 小枫枫, 转载或复制请以 超链接形式 并注明出处 小枫枫不疯喔
原文地址: 《微信小程序点击触发水波纹容器组件》 发布于2023-8-12

评论

切换注册

登录

您也可以使用第三方帐号快捷登录

切换登录

注册

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏