js 实现Material UI点击涟漪效果示例


Posted in Javascript onSeptember 23, 2022

正文

我个人而言还是挺喜欢Material UI这套设计风格的。一些细节方面做的还不错。就比如今天要给大家分享的点击涟漪效果。Material UI里面叫做Ripples。好了,话不多说,开始吧。

HTML

<div class="example">Click me</div>

CSS

.example {
    position: relative;
    width: 300px;
    height: 300px;
    line-height: 300px;
    text-align: center;
    margin-top: 30px;
    box-shadow: 0 2px 4px -1px #0003, 0 4px 5px #00000024, 0 1px 10px #0000001f;
    overflow: hidden;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.ripple {
    position: absolute;
    border-radius: 50%;
    background-color: #0000001a;
    animation: ripple 225ms cubic-bezier(0, 0, .2, 1) forwards;
    transform: scale3d(0, 0, 0);
    pointer-events: none;
}
@keyframes ripple {
    from {
        transform: scale3d(0, 0, 0);
    }
    to {
        transform: scale3d(1, 1, 1);
    }
}

JS

const exampleEl = document.querySelector('.example');
// 移动端触发顺序 touchstart => touchend => mousemove => mousedown => mouseup => click
// 文档https://w3c.github.io/touch-events/#mouse-events
let rippleEl = null, startTime, isMouseup = false;
// touchstart
function handleTouchstart(e) {
    createRipple(e);
}
// touchend
function handleTouchend(e) {
    removeRipple(e);
    // 阻止mouse事件触发
    e.preventDefault();
}
// touchcancel
function handleTouchcancel(e) {
    removeRipple(e);
}
// mousedown
function handleMousedown(e) {
    // 避免mouseup,mouseleave重复执行
    isMouseup = false;
    createRipple(e);
}
// mouseup
function handleMouseup(e) {
    isMouseup = true;
    removeRipple(e);
}
// mouseleave
function handleMouseleave(e) {
    if (isMouseup || rippleEl === null) {
        return;
    }
    removeRipple(e)
}
// 创建ripple
function createRipple(e) {
    startTime = e.timeStamp;
    const current = { x: e.clientX, y: e.clientY }
    if (e.type === 'touchstart') {
        current.x = e.touches[0].clientX;
        current.y = e.touches[0].clientY;
    }
    const rect = exampleEl.getBoundingClientRect();
    const vertex = {
        nw: { x: rect.left, y: rect.top },
        ne: { x: rect.left + rect.width, y: rect.top },
        se: { x: rect.left + rect.width, y: rect.top + rect.height },
        sw: { x: rect.left, y: rect.top + rect.height }
    }
    let max = 0;
    for (const key in vertex) {
        // ripple半径
        const radius = getDistance({ x: current.x, y: current.y }, vertex[key]);
        max = Math.max(max, radius);
    }
    rippleEl = document.createElement('div');
    rippleEl.className = 'ripple';
    rippleEl.style.left = (current.x - rect.left - max) + 'px';
    rippleEl.style.top = (current.y - rect.top - max) + 'px';
    rippleEl.style.width = (max * 2) + 'px';
    rippleEl.style.height = (max * 2) + 'px';
    exampleEl.appendChild(rippleEl);
}
// 移除ripple
function removeRipple(e) {
    if (e.timeStamp - startTime > 225) {
        rippleEl.remove();
        rippleEl = null;
    } else {
        // 采用animation属性实现动画效果。相比transition的好处在于不用手动触发重绘
        rippleEl.addEventListener('animationend', function () {
            this.remove();
            if (this === rippleEl) {
                rippleEl = null;
            }
        });
    }
}
// 绑定事件
exampleEl.addEventListener('mousedown', handleMousedown);
exampleEl.addEventListener('mouseup', handleMouseup);
exampleEl.addEventListener('mouseleave', handleMouseleave);
exampleEl.addEventListener('touchstart', handleTouchstart);
exampleEl.addEventListener('touchend', handleTouchend);
exampleEl.addEventListener('touchcancel', handleTouchcancel);
/**
 * 获取两点间距离
 * @param {object} a 第一个点坐标
 * @param {object} b 第二个点坐标
 * @returns
 */
function getDistance(a, b) {
    const x = a.x - b.x;
    const y = a.y - b.y;
    return Math.hypot(x, y); // Math.sqrt(x * x + y * y);
}

实现效果 

js 实现Material UI点击涟漪效果示例

以上就是js 实现Material UI点击涟漪效果示例的详细内容,更多关于js Material UI点击涟漪效果的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
js 操作select相关方法函数
Dec 06 Javascript
基于jquery的大众点评,分类导航实现代码
Aug 23 Javascript
javascript 基础篇4 window对象,DOM
Mar 14 Javascript
JS实现表格数据各种搜索功能的方法
Mar 03 Javascript
BootStrap 智能表单实战系列(十)自动完成组件的支持
Jun 13 Javascript
基于JavaScript实现屏幕滚动效果
Jan 18 Javascript
javascript 判断一个对象为数组的方法
May 03 Javascript
React Native开发封装Toast与加载Loading组件示例
Sep 08 Javascript
JavaScript实现的联动菜单特效示例
Jul 08 Javascript
Vue路由之JWT身份认证的实现方法
Aug 26 Javascript
使用typescript构建Vue应用的实现
Aug 26 Javascript
es6中reduce的基本使用方法
Sep 10 Javascript
js 实现验证码输入框示例详解
Sep 23 #Javascript
TypeScript 内置高级类型编程示例
Sep 23 #Javascript
详解Anyscript开发指南绕过typescript类型检查
Sep 23 #Javascript
js基于div丝滑实现贝塞尔曲线
Sep 23 #Javascript
TS 类型兼容教程示例详解
Sep 23 #Javascript
TS 类型收窄教程示例详解
Sep 23 #Javascript
JavaScript实现简单的音乐播放器
Aug 14 #Javascript
You might like
用PHP实现Ftp用户的在线管理
2012/02/16 PHP
php实现事件监听与触发的方法
2014/11/21 PHP
php中删除、清空session的方式总结
2015/10/09 PHP
tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
2019/05/28 PHP
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
使用jquery的ajax需要注意的地方dataType的设置
2013/08/12 Javascript
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
深入理解Javascript里的依赖注入
2014/03/19 Javascript
用js通过url传参把数据从一个页面传到另一个页面
2014/09/01 Javascript
JavaScript的各种常见函数定义方法
2014/09/16 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
Jquery ajax 同步阻塞引起的UI线程阻塞问题
2015/11/17 Javascript
JS密码生成与强度检测完整实例(附demo源码下载)
2016/04/06 Javascript
基于Vue.js的表格分页组件
2016/05/22 Javascript
妙用Bootstrap的 popover插件实现校验表单提示功能
2016/08/29 Javascript
H5移动端适配 Flexible方案
2016/10/24 Javascript
Avalonjs 实现简单购物车功能(实例代码)
2017/02/07 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
jQuery实现DIV响应鼠标滑过由下向上展开效果示例【测试可用】
2018/04/26 jQuery
解决vue+element 键盘回车事件导致页面刷新的问题
2018/08/25 Javascript
vue 组件销毁并重置的实现
2020/01/13 Javascript
Vue Object 的变化侦测实现代码
2020/04/15 Javascript
react中hook介绍以及使用教程
2020/12/11 Javascript
Python访问MySQL封装的常用类实例
2014/11/11 Python
Python脚本在Appium库上对移动应用实现自动化测试
2015/04/17 Python
Python smallseg分词用法实例分析
2015/05/28 Python
Jmeter HTTPS接口测试证书导入过程图解
2020/07/22 Python
Python pymysql模块安装并操作过程解析
2020/10/13 Python
全球高级音频和视频专家:HiDef Lifestyle
2019/08/02 全球购物
求∏的近似值,直到最后一项的绝对值小于指定的数
2016/02/12 面试题
支部书记四风问题对照检查材料
2014/10/04 职场文书
商业门面租房协议书
2014/11/25 职场文书
艺术节开幕词
2015/01/28 职场文书
自从在 IDEA 中用了热部署神器 JRebel 之后,开发效率提升了 10(真棒)
2021/06/26 Java/Android
win10识别不了U盘怎么办 win10系统读取U盘失败的解决办法
2022/08/05 数码科技
Redis配置外网可访问(redis远程连接不上)的方法
2022/12/24 Redis