本文跟大家分享基於js和canves實現點擊按鈕水波紋效果,效果非常逼真,對此感興趣的朋友一起看看吧
最近看到個不錯的按鈕點擊效果,當點擊時產生一次水波漣漪效果,挺好玩的,於是簡單的實現了下(沒考慮低版本瀏覽器兼容問題)
先看看效果吧,如下圖(錄製gif軟體有點渣,看起來卡卡的...)
這個效果可以由元素內嵌套canves實現,也可以由css3實現。
Canves實作
網路上摘了一份canves實現的程式碼,略微去掉了些重複定義的樣式並且給出js註釋,程式碼如下
html程式碼
<a class="btn color-1 material-design" data-color="#2f5398">Press me!</a>
css程式碼
#* { box-sizing: border-box; outline: none; } body { font-family: 'Open Sans'; font-size: 100%; font-weight: 300; line-height: 1.5em; text-align: center; } .btn { border: none; display: inline-block; color: white; overflow: hidden; margin: 1rem; padding: 0; width: 150px; height: 40px; text-align: center; line-height: 40px; border-radius: 5px; } .btn.color-1 { background-color: #426fc5; } .btn-border.color-1 { background-color: transparent; border: 2px solid #426fc5; color: #426fc5; } .material-design { position: relative; } .material-design canvas { opacity: 0.25; position: absolute; top: 0; left: 0; } .container { align-content: center; align-items: flex-start; display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; margin: 0 auto; max-width: 46rem; }
js程式碼
var canvas = {}, centerX = 0, centerY = 0, color = '', containers = document.getElementsByClassName('material-design') context = {}, element = {}, radius = 0, // 根据callback生成requestAnimationFrame动画 requestAnimFrame = function () { return ( window.requestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); } ); } (), // 为每个指定元素生成canves init = function () { containers = Array.prototype.slice.call(containers); for (var i = 0; i < containers.length; i += 1) { canvas = document.createElement('canvas'); canvas.addEventListener('click', press, false); containers[i].appendChild(canvas); canvas.style.width ='100%'; canvas.style.height='100%'; canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; } }, // 点击并且获取需要的数据,如点击坐标、元素大小、颜色 press = function (event) { color = event.toElement.parentElement.dataset.color; element = event.toElement; context = element.getContext('2d'); radius = 0; centerX = event.offsetX; centerY = event.offsetY; context.clearRect(0, 0, element.width, element.height); draw(); }, // 绘制圆形,并且执行动画 draw = function () { context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.fillStyle = color; context.fill(); radius += 2; // 通过判断半径小于元素宽度,不断绘制 radius += 2 的圆形 if (radius < element.width) { requestAnimFrame(draw); } }; init();
CSS3實作
接下來就是純手打的程式碼了...覺得還是css3實現的方便些,可能是css寫習慣了...html程式碼
<a class="waves ts-btn">Press me!</a>
.waves{ position:relative; cursor:pointer; display:inline-block; overflow:hidden; text-align: center; -webkit-tap-highlight-color:transparent; z-index:1; } .waves .waves-animation{ position:absolute; border-radius:50%; width:25px; height:25px; opacity:0; background:rgba(255,255,255,0.3); transition:all 0.7s ease-out; transition-property:transform, opacity, -webkit-transform; -webkit-transform:scale(0); transform:scale(0); pointer-events:none } .ts-btn{ width: 200px; height: 56px; line-height: 56px; background: #f57035; color: #fff; border-radius: 5px; }
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網! 相關推薦:
######jQuery和CSS3折疊卡片式下拉列錶框實現效果###############關於JS元件Bootstrap導航條的使用方法##################以上是JS和Canves實現點擊按鈕水波紋效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!