js點(diǎn)擊按鈕實(shí)現(xiàn)水波紋效果代碼(CSS3和Canves)
近來(lái)看到個(gè)不錯(cuò)的按鈕點(diǎn)擊效果,當(dāng)點(diǎn)擊時(shí)產(chǎn)生一次水波漣漪效果,挺好玩的,于是簡(jiǎn)單的實(shí)現(xiàn)了下(沒(méi)考慮低版本瀏覽器兼容問(wèn)題)
先看看效果吧,如下圖(錄制gif軟件有點(diǎn)渣,看起來(lái)卡卡的...)

這種效果可以由元素內(nèi)嵌套canves實(shí)現(xiàn),也可以由css3實(shí)現(xiàn)。
Canves實(shí)現(xiàn)
網(wǎng)上摘了一份canves實(shí)現(xiàn)的代碼,略微去掉了些重復(fù)定義的樣式并且給出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,
// 根據(jù)callback生成requestAnimationFrame動(dòng)畫(huà)
requestAnimFrame = function () {
return (
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
}
);
} (),
// 為每個(gè)指定元素生成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;
}
},
// 點(diǎn)擊并且獲取需要的數(shù)據(jù),如點(diǎn)擊坐標(biāo)、元素大小、顏色
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();
},
// 繪制圓形,并且執(zhí)行動(dòng)畫(huà)
draw = function () {
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = color;
context.fill();
radius += 2;
// 通過(guò)判斷半徑小于元素寬度,不斷繪制 radius += 2 的圓形
if (radius < element.width) {
requestAnimFrame(draw);
}
};
init();
CSS3實(shí)現(xiàn)
接下來(lái)就是純手打的代碼了...覺(jué)得還是css3實(shí)現(xiàn)的方便些,可能是css寫(xiě)習(xí)慣了...
html代碼
<a class="waves ts-btn">Press me!</a>
css代碼
.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;
}
js代碼
document.addEventListener('DOMContentLoaded',function(){
var duration = 750;
// 樣式string拼湊
var forStyle = function(position){
var cssStr = '';
for( var key in position){
if(position.hasOwnProperty(key)) cssStr += key+':'+position[key]+';';
};
return cssStr;
}
// 獲取鼠標(biāo)點(diǎn)擊位置
var forRect = function(target){
var position = {
top:0,
left:0
}, ele = document.documentElement;
'undefined' != typeof target.getBoundingClientRect && (position = target.getBoundingClientRect());
return {
top: position.top + window.pageYOffset - ele.clientTop,
left: position.left + window.pageXOffset - ele.clientLeft
}
}
var show = function(event){
var pDiv = event.target,
cDiv = document.createElement('div');
pDiv.appendChild(cDiv);
var rectObj = forRect(pDiv),
_height = event.pageY - rectObj.top,
_left = event.pageX - rectObj.left,
_scale = 'scale(' + pDiv.clientWidth / 100 * 10 + ')';
var position = {
top: _height+'px',
left: _left+'px'
};
cDiv.className = cDiv.className + " waves-animation",
cDiv.setAttribute("style", forStyle(position)),
position["-webkit-transform"] = _scale,
position["-moz-transform"] = _scale,
position["-ms-transform"] = _scale,
position["-o-transform"] = _scale,
position.transform = _scale,
position.opacity = "1",
position["-webkit-transition-duration"] = duration + "ms",
position["-moz-transition-duration"] = duration + "ms",
position["-o-transition-duration"] = duration + "ms",
position["transition-duration"] = duration + "ms",
position["-webkit-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["-moz-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["-o-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
cDiv.setAttribute("style", forStyle(position));
var finishStyle = {
opacity: 0,
"-webkit-transition-duration": duration + "ms", // 過(guò)渡時(shí)間
"-moz-transition-duration": duration + "ms",
"-o-transition-duration": duration + "ms",
"transition-duration": duration + "ms",
"-webkit-transform" : _scale,
"-moz-transform" : _scale,
"-ms-transform" : _scale,
"-o-transform" : _scale,
top: _height + "px",
left: _left + "px",
};
setTimeout(function(){
cDiv.setAttribute("style", forStyle(finishStyle));
setTimeout(function(){
pDiv.removeChild(cDiv);
},duration);
},100)
}
document.querySelector('.waves').addEventListener('click',function(e){
show(e);
},!1);
},!1);
就這些,原理也簡(jiǎn)單,獲取點(diǎn)擊位置 > 添加樣式 順便,中秋快樂(lè)~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
仿163填寫(xiě)郵件地址自動(dòng)顯示下拉(無(wú)優(yōu)化)
本框內(nèi)填個(gè)1,這些值都寫(xiě)在隱藏域了。代碼里可以看到,用戶輸入包含在里面的時(shí)候,可以按ENTER鍵選中.2008-11-11
延時(shí)重復(fù)執(zhí)行函數(shù) lLoopRun.js
延時(shí)重復(fù)執(zhí)行函數(shù) lLoopRun.js...2007-05-05
JavaScript函數(shù)式編程(Functional Programming)高階函數(shù)(Higher order fun
這篇文章主要介紹了JavaScript函數(shù)式編程(Functional Programming)高階函數(shù)(Higher order functions),結(jié)合實(shí)例形式分析了javascript函數(shù)式編程高級(jí)函數(shù)的概念、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05
JavaScript實(shí)現(xiàn)多欄目切換效果
在網(wǎng)站開(kāi)發(fā)中尤其是新聞?lì)惥W(wǎng)站,經(jīng)常遇到多欄目切換的設(shè)計(jì),這種效果有很多種實(shí)現(xiàn)效果,現(xiàn)在記錄一種很簡(jiǎn)單的寫(xiě)法2016-12-12
關(guān)于數(shù)據(jù)與后端進(jìn)行交流匹配(點(diǎn)亮星星)
這篇文章主要介紹了關(guān)于數(shù)據(jù)與后端進(jìn)行交流匹配(點(diǎn)亮星星) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
使用JavaScript實(shí)現(xiàn)計(jì)算顏色的相對(duì)亮度并確定相應(yīng)的文本顏色
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)計(jì)算顏色的相對(duì)亮度并確定相應(yīng)的文本顏色,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
js實(shí)現(xiàn)倒計(jì)時(shí)時(shí)鐘的示例代碼
本篇文章主要是對(duì)js倒計(jì)時(shí)時(shí)鐘的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12

