mirror of
https://github.com/multipleofnpm/tiny-ripple.git
synced 2026-01-13 16:17:57 +00:00
Update index.js
This commit is contained in:
33
index.js
33
index.js
@@ -1,31 +1,2 @@
|
||||
if(window.__tinyRippleInitialized) throw new Error('tiny-ripple already initialized')
|
||||
window.__tinyRippleInitialized = true
|
||||
var __tinyRippleStyleElement = null
|
||||
var __tinyRipplePointerHandler = null
|
||||
var __tinyRippleFallbackTimeouts = []
|
||||
var __tinyRippleDefaultDurationMs = 550
|
||||
function prefersReducedMotion(){try{return window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches}catch(e){return false}}
|
||||
if(prefersReducedMotion()){
|
||||
window.tinyRipple = {disable:function(){window.__tinyRippleInitialized=false}}
|
||||
} else {
|
||||
var css = ":root{--tiny-ripple-color:rgba(0,0,0,.15);--tiny-ripple-duration:550ms;} .tiny-ripple{position:fixed;border-radius:50%;pointer-events:none;transform:translate(-50%,-50%) scale(0);background:var(--tiny-ripple-color);will-change:transform,opacity;z-index:2147483647} .tiny-ripple.tiny-ripple--anim{transition:transform var(--tiny-ripple-duration) cubic-bezier(.2,.8,.2,1),opacity var(--tiny-ripple-duration) linear;transform:translate(-50%,-50%) scale(1);opacity:0} "
|
||||
__tinyRippleStyleElement = document.createElement('style')
|
||||
__tinyRippleStyleElement.type = 'text/css'
|
||||
__tinyRippleStyleElement.appendChild(document.createTextNode(css))
|
||||
;(document.head||document.documentElement).appendChild(__tinyRippleStyleElement)
|
||||
function getComputedDurationMs(){try{var v=getComputedStyle(document.documentElement).getPropertyValue('--tiny-ripple-duration')||'';v=v.trim();if(!v) return __tinyRippleDefaultDurationMs; if(v.endsWith('ms')) return Math.round(parseFloat(v)); if(v.endsWith('s')) return Math.round(parseFloat(v)*1000); return __tinyRippleDefaultDurationMs}catch(e){return __tinyRippleDefaultDurationMs}}
|
||||
function removeElement(el){if(!el) return; if(el.parentNode) el.parentNode.removeChild(el)}
|
||||
function makeRipple(x,y){var dx=Math.max(x,window.innerWidth-x),dy=Math.max(y,window.innerHeight-y);var r=Math.sqrt(dx*dx+dy*dy);var size=Math.ceil(r*2);var el=document.createElement('div');el.className='tiny-ripple';el.style.width=el.style.height=size+'px';el.style.left=x+'px';el.style.top=y+'px';document.body.appendChild(el);void el.getBoundingClientRect();el.classList.add('tiny-ripple--anim');var duration=getComputedDurationMs();var onEnd=function(){removeElement(el);el.removeEventListener('transitionend',onEnd)};el.addEventListener('transitionend',onEnd);var to=setTimeout(function(){removeElement(el);el.removeEventListener('transitionend',onEnd)},duration+100);__tinyRippleFallbackTimeouts.push(to)}
|
||||
__tinyRipplePointerHandler = function(ev){if('button' in ev && ev.button!==0) return;var x=(ev.clientX!==undefined&&ev.clientX!==null)?ev.clientX:((ev.touches&&ev.touches[0]&&ev.touches[0].clientX)||0);var y=(ev.clientY!==undefined&&ev.clientY!==null)?ev.clientY:((ev.touches&&ev.touches[0]&&ev.touches[0].clientY)||0);makeRipple(x,y)}
|
||||
if(window.PointerEvent) document.addEventListener('pointerdown',__tinyRipplePointerHandler,{passive:true})
|
||||
else { document.addEventListener('touchstart',__tinyRipplePointerHandler,{passive:true}); document.addEventListener('mousedown',__tinyRipplePointerHandler,{passive:true}) }
|
||||
window.tinyRipple = {
|
||||
disable:function(){
|
||||
if(window.PointerEvent) document.removeEventListener('pointerdown',__tinyRipplePointerHandler)
|
||||
else { document.removeEventListener('touchstart',__tinyRipplePointerHandler); document.removeEventListener('mousedown',__tinyRipplePointerHandler) }
|
||||
if(__tinyRippleStyleElement && __tinyRippleStyleElement.parentNode) __tinyRippleStyleElement.parentNode.removeChild(__tinyRippleStyleElement)
|
||||
var nodes=document.querySelectorAll('.tiny-ripple');for(var i=0;i<nodes.length;i++) removeElement(nodes[i])
|
||||
for(var j=0;j<__tinyRippleFallbackTimeouts.length;j++) clearTimeout(__tinyRippleFallbackTimeouts[j]);__tinyRippleFallbackTimeouts.length=0;window.__tinyRippleInitialized=false
|
||||
}
|
||||
}
|
||||
}
|
||||
if(window.__tinyRippleInit) throw new Error('tiny-ripple already initialized') window.__tinyRippleInit = true var __tr_style = document.createElement('style') __tr_style.textContent = ".r{position:fixed;border-radius:50%;pointer-events:none;background:transparent;box-shadow:0 8px 18px rgba(0,0,0,0.14);transform:translate(-50%,-50%) scale(.15);opacity:1;transition:transform .42s cubic-bezier(.2,.8,.2,1),opacity .42s linear,box-shadow .42s linear;z-index:9999999}.r.a{transform:translate(-50%,-50%) scale(1);opacity:0;box-shadow:0 0 0 rgba(0,0,0,0)}" document.head.appendChild(__tr_style) var __tr_dur = 420 function __tr_make(x,y){var dx=Math.max(x,innerWidth-x),dy=Math.max(y,innerHeight-y),r=Math.hypot(dx,dy),s=Math.max(48,Math.ceil(r*2));var e=document.createElement('div');e.className='r';e.style.width=e.style.height=s+'px';e.style.left=x+'px';e.style.top=y+'px';document.body.appendChild(e);void e.offsetWidth;e.classList.add('a');setTimeout(function(){if(e.parentNode) e.parentNode.removeChild(e)},__tr_dur+60)} var __tr_h = function(ev){var x=('clientX' in ev && ev.clientX!=null)?ev.clientX:(ev.touches&&ev.touches[0]&&ev.touches[0].clientX)||0;var y=('clientY' in ev && ev.clientY!=null)?ev.clientY:(ev.touches&&ev.touches[0]&&ev.touches[0].clientY)||0;__tr_make(x,y)} if(window.PointerEvent) addEventListener('pointerdown',__tr_h,{passive:true}); else{addEventListener('touchstart',__tr_h,{passive:true});addEventListener('mousedown',__tr_h,{passive:true})} window.tinyRipple={disable:function(){if(window.PointerEvent) removeEventListener('pointerdown',__tr_h);else{removeEventListener('touchstart',__tr_h);removeEventListener('mousedown',__tr_h)}if(__tr_style.parentNode) __tr_style.parentNode.removeChild(__tr_style);window.__tinyRippleInit=false}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user