--
特效一
很簡單的實現,缺點是會影響點兩下的文字選取功能
完整網頁程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!DOCTYPE html> <html> <head> <style type="text/css"> div.clickEffect{ position:fixed; box-sizing:border-box; border-style:solid; border-color:#000000; border-radius:50%; animation:clickEffect 0.4s ease-out; z-index:99999; } @keyframes clickEffect{ 0%{ opacity:1; width:0.5em; height:0.5em; margin:-0.25em; border-width:0.5rem; } 100%{ opacity:0.2; width:15em; height:15em; margin:-7.5em; border-width:0.03rem; } } </style> <script type="text/javascript"> function clickEffect(e){ var d=document.createElement("div"); d.className="clickEffect"; d.style.top=e.clientY+"px";d.style.left=e.clientX+"px"; document.body.appendChild(d); d.addEventListener('animationend',function(){d.parentElement.removeChild(d);}.bind(this)); } document.addEventListener('click',clickEffect); </script> </head> <body> <h1>Mouse Click Animation example</h1> Click around on this page. </body> </html> |
原理
滑鼠點擊產生一個 div 像這樣 <div class="clickEffect">,然後定義 clickEffect 的 CSS 產生動畫效果
--
RippleButtons.js
將 ripple.js 放在 </body> 前即可,對連點兩下選取影響很小,而且可以對 <a> 和 <button> 產生波紋特效動畫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
(function(){ "use strict"; var colour = "#4285f4"; var opacity = 0.3; var ripple_within_elements = ['input', 'button', 'a']; var ripple_without_diameter = 50; var overlays = { items: [], get: function(){ var $element; for(var i = 0; i < overlays.items.length; i++){ $element = overlays.items[i]; if($element.transition_phase === false) { $element.transition_phase = 0; return $element; } } $element = document.createElement("div"); $element.style.position = "absolute"; $element.style.opacity = opacity; //$element.style.outline = "10px solid red"; $element.style.pointerEvents = "none"; $element.style.background = "-webkit-radial-gradient(" + colour + " 64%, rgba(0,0,0,0) 65%) no-repeat"; $element.style.background = "radial-gradient(" + colour + " 64%, rgba(0,0,0,0) 65%) no-repeat"; $element.style.transform = "translateZ(0)"; $element.transition_phase = 0; $element.rid = overlays.items.length; $element.next_transition = overlays.next_transition_generator($element); document.body.appendChild($element); overlays.items.push($element); return $element; }, next_transition_generator: function($element){ return function(){ $element.transition_phase++; switch($element.transition_phase){ case 1: $element.style[transition] = "all 0.2s ease-in-out"; $element.style.backgroundSize = $element.ripple_backgroundSize; $element.style.backgroundPosition = $element.ripple_backgroundPosition; setTimeout($element.next_transition, 0.2 * 1000); //now I know transitionend is better but it fires multiple times when multiple properties are animated, so this is simpler code and (imo) worth tiny delays break; case 2: $element.style[transition] = "opacity 0.15s ease-in-out"; $element.style.opacity = 0; setTimeout($element.next_transition, 0.15 * 1000); break; case 3: overlays.recycle($element); break; } }; }, recycle: function($element){ $element.style.display = "none"; $element.style[transition] = "none"; if($element.timer) clearTimeout($element.timer); $element.transition_phase = false; } }; var transition = function(){ var i, el = document.createElement('div'), transitions = { 'WebkitTransition':'webkitTransition', 'transition':'transition', 'OTransition':'otransition', 'MozTransition':'transition' }; for (i in transitions) { if (transitions.hasOwnProperty(i) && el.style[i] !== undefined) { return transitions[i]; } } }(); var click = function(event){ var $element = overlays.get(), touch, x, y; touch = event.touches ? event.touches[0] : event; $element.style[transition] = "none"; $element.style.backgroundSize = "3px 3px"; $element.style.opacity = opacity; if(ripple_within_elements.indexOf(touch.target.nodeName.toLowerCase()) > -1) { x = touch.offsetX; y = touch.offsetY; var dimensions = touch.target.getBoundingClientRect(); if(!x || !y){ x = (touch.clientX || touch.x) - dimensions.left; y = (touch.clientY || touch.y) - dimensions.top; } $element.style.backgroundPosition = x + "px " + y + "px"; $element.style.width = dimensions.width + "px"; $element.style.height = dimensions.height + "px"; $element.style.left = (dimensions.left) + "px"; $element.style.top = (dimensions.top + document.body.scrollTop + document.documentElement.scrollTop) + "px"; var computed_style = window.getComputedStyle(event.target); for (var key in computed_style) { if (key.toString().indexOf("adius") > -1) { if(computed_style[key]) { $element.style[key] = computed_style[key]; } } else if(parseInt(key, 10).toString() === key && computed_style[key].indexOf("adius") > -1){ $element.style[computed_style[key]] = computed_style[computed_style[key]]; } } $element.style.backgroundPosition = x + "px " + y + "px"; $element.ripple_backgroundPosition = (x - dimensions.width) + "px " + (y - dimensions.width) + "px"; $element.ripple_backgroundSize = (dimensions.width * 2) + "px " + (dimensions.width * 2) + "px"; } else { //click was outside of ripple element x = touch.clientX || touch.x || touch.pageX; y = touch.clientY || touch.y || touch.pageY; $element.style.borderRadius = "0px"; $element.style.left = (x - ripple_without_diameter / 2) + "px"; $element.style.top = (document.body.scrollTop + document.documentElement.scrollTop + y - ripple_without_diameter / 2) + "px"; $element.ripple_backgroundSize = ripple_without_diameter + "px " + ripple_without_diameter + "px"; $element.style.width = ripple_without_diameter + "px"; $element.style.height = ripple_without_diameter + "px"; $element.style.backgroundPosition = "center center"; $element.ripple_backgroundPosition = "center center"; $element.ripple_backgroundSize = ripple_without_diameter + "px " + ripple_without_diameter + "px"; } $element.ripple_x = x; $element.ripple_y = y; $element.style.display = "block"; setTimeout($element.next_transition, 20); }; if('ontouchstart' in window || 'onmsgesturechange' in window){ document.addEventListener("touchstart", click, false); } else { document.addEventListener("click", click, false); } }()); |
--
Anime.js Fireworks canvas
煙火特效,非常華麗,放在比較輕鬆、生活類的網頁很適合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
<!DOCTYPE html> <html lang='en' class=''> <head> <meta charset='UTF-8'> <title>CodePen Demo</title> </head> <body> <canvas class="fireworks" style="left: 0; pointer-events: none; position: fixed; top: 0;"></canvas> <script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-2c7831bb44f98c1391d6a4ffda0e1fd302503391ca806e7fcc7b9b87197aec26.js"></script> <script src="https://codepen.io/juliangarnier/pen/yMLaRG.js"></script> <script> window.human = false; var canvasEl = document.querySelector('.fireworks'); var ctx = canvasEl.getContext('2d'); var numberOfParticules = 30; var pointerX = 0; var pointerY = 0; var tap = 'ontouchstart' in window || navigator.msMaxTouchPoints ? 'touchstart' : 'mousedown'; var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C']; function setCanvasSize() { canvasEl.width = window.innerWidth * 2; canvasEl.height = window.innerHeight * 2; canvasEl.style.width = window.innerWidth + 'px'; canvasEl.style.height = window.innerHeight + 'px'; canvasEl.getContext('2d').scale(2, 2); } function updateCoords(e) { pointerX = e.clientX || e.touches[0].clientX; pointerY = e.clientY || e.touches[0].clientY; } function setParticuleDirection(p) { var angle = anime.random(0, 360) * Math.PI / 180; var value = anime.random(50, 180); var radius = [-1, 1][anime.random(0, 1)] * value; return { x: p.x + radius * Math.cos(angle), y: p.y + radius * Math.sin(angle) }; } function createParticule(x, y) { var p = {}; p.x = x; p.y = y; p.color = colors[anime.random(0, colors.length - 1)]; p.radius = anime.random(16, 32); p.endPos = setParticuleDirection(p); p.draw = function () { ctx.beginPath(); ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true); ctx.fillStyle = p.color; ctx.fill(); }; return p; } function createCircle(x, y) { var p = {}; p.x = x; p.y = y; p.color = '#FFF'; p.radius = 0.1; p.alpha = .5; p.lineWidth = 6; p.draw = function () { ctx.globalAlpha = p.alpha; ctx.beginPath(); ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true); ctx.lineWidth = p.lineWidth; ctx.strokeStyle = p.color; ctx.stroke(); ctx.globalAlpha = 1; }; return p; } function renderParticule(anim) { for (var i = 0; i < anim.animatables.length; i++) {if (window.CP.shouldStopExecution(0)) break; anim.animatables[i].target.draw(); }window.CP.exitedLoop(0); } function animateParticules(x, y) { var circle = createCircle(x, y); var particules = []; for (var i = 0; i < numberOfParticules; i++) {if (window.CP.shouldStopExecution(1)) break; particules.push(createParticule(x, y)); }window.CP.exitedLoop(1); anime.timeline().add({ targets: particules, x: function (p) {return p.endPos.x;}, y: function (p) {return p.endPos.y;}, radius: 0.1, duration: anime.random(1200, 1800), easing: 'easeOutExpo', update: renderParticule }). add({ targets: circle, radius: anime.random(80, 160), lineWidth: 0, alpha: { value: 0, easing: 'linear', duration: anime.random(600, 800) }, duration: anime.random(1200, 1800), easing: 'easeOutExpo', update: renderParticule, offset: 0 }); } var render = anime({ duration: Infinity, update: function () { ctx.clearRect(0, 0, canvasEl.width, canvasEl.height); } }); document.addEventListener(tap, function (e) { window.human = true; render.play(); updateCoords(e); animateParticules(pointerX, pointerY); }, false); var centerX = window.innerWidth / 2; var centerY = window.innerHeight / 2; function autoClick() { if (window.human) return; animateParticules( anime.random(centerX - 50, centerX + 50), anime.random(centerY - 50, centerY + 50)); anime({ duration: 200 }).finished.then(autoClick); } // autoClick(); setCanvasSize(); window.addEventListener('resize', setCanvasSize, false); </script> </body> </html> |
--
結論
放在工作的網頁,一整天都有點擊特效還是無法使用,最後全都移除了 🤣🤣🤣
--
545 total views, 1 views today