近来看到个不错的按钮点击效果,当点击时产生一次水波涟漪效果,挺好玩的,于是简单的实现了下(没考虑低版本浏览器兼容问题)
先看看效果吧,如下图(录制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>
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; } // 获取鼠标点击位置 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", // 过渡时间 "-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);
好了,就这些, 顺便,中秋快乐~
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】