javascript鼠标单击触发显示波纹动画特效

所属分类: 网页特效-动画效果    2024-01-26 11:56:45

javascript鼠标单击触发显示波纹动画特效 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript鼠标单击触发显示波纹动画特效(共2个文件)

    • index.html
    • ripple.js

使用方法

const isTouchCapable = 'ontouchstart' in window ||
    (window.DocumentTouch && document instanceof window.DocumentTouch) ||
    navigator.maxTouchPoints > 0 ||
    window.navigator.msMaxTouchPoints > 0;
let startEv;
let endEv;
if (isTouchCapable) {
    startEv = 'touchstart';
    endEv = 'touchend';
} else {
    startEv = 'mousedown';
    endEv = 'mouseup';
}
const rippleEffect = {
    elements: ".r,button",
    color: "rgba(0,0,0,.1)",
    transitionDuration: 1,

    initialize: () => {
        const styleElement = document.createElement('style');
        styleElement.innerHTML = `
      ${rippleEffect.elements} {
        -webkit-tap-highlight-color: transparent;
        position: relative;
        cursor: pointer;
        overflow: hidden;
      }
      :root{
          --rippleColor:${rippleEffect.color}
      }
      .rs {
        position: absolute;
        background: var(--rippleColor);
        pointer-events: none;
        width: 5px;
        height: 5px;
        transform: translate(-50%, -50%);
        border-radius: 100%;
      }
      @keyframes rippleAnimation {
        0% {
          opacity: 0;
        }
        15% {
          opacity: 1;
        }
        50%, 100% {
          opacity: 1;
          width: var(--ms);
          height: var(--ms);
        }
      }
    `;
        document.head.append(styleElement);

        const elements = document.querySelectorAll(rippleEffect.elements);

        function addRippleEffect(element) {
            element.classList.add("rd");

            element.addEventListener(startEv, (event) => {
                let tchData;
                if (isTouchCapable) {
                    tchData = event.touches[0];
                } else {
                    tchData = event;
                }
                const ripple = document.createElement("div");
                const y = tchData.clientY - element.getBoundingClientRect().top;
                const x = tchData.clientX - element.getBoundingClientRect().left;
                const scaleY = Math.max(element.clientHeight - y, y) * 2.5;
                const scaleX = Math.max(element.clientWidth - x, x) * 2.5;
                const rippleSize = Math.max(scaleY, scaleX) * 3 / 2;
                const transitionDuration = Math.max(rippleSize / 1250, 0.2) + rippleEffect.transitionDuration;

                ripple.style.cssText = `
          transition: ${transitionDuration}s;
          transition-delay:.1s;
          animation: rippleAnimation forwards ${transitionDuration / 1.2}s;
          top: ${y}px;
          left: ${x}px;
          --ms: ${rippleSize}px;
        `;
                ripple.classList.add('rs');

                function removeRipple() {
                    ripple.style.background = 'transparent';
                }

                element.addEventListener(endEv, removeRipple, false);


                element.insertAdjacentElement('beforeend', ripple);
            }, false);
        }

        elements.forEach(addRippleEffect);
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((addedNode) => {
                    if (addedNode.nodeType === 1 && addedNode.matches(rippleEffect.elements)) {
                        addRippleEffect(addedNode);
                    }
                });
            });
        });
        observer.observe(document.body, {childList: true, subtree: true});
    }
};
window.addEventListener('DOMContentLoaded', () => {
    rippleEffect.initialize()
}, false)

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 动画效果

js创建的丰富的多彩动画特效代码

一款鼠标点击弹出动画特效代码,卡片有各种颜色,并在鼠标点击后触发显示扩展动画特效。
  动画效果
 8190  0

jquery和animate.css制作打字机打字动画效果代码

一款模仿打字机动画打字特效,有一种文字渐显的感觉!
  动画效果
 7255  0

3D立体圣诞饰品挂件墙three.js

一款圣诞饰品展示墙,鼠标按住左键不放,来回移动,可多角度翻转3D视觉。
  动画效果
 382  0

jquery鼠标悬停触发背景动画特效

鼠标hover经过按钮上方,触发button背景色淡出淡入动画特效!
  动画效果
 6343  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论