感觉这个特效对大神们来说简直so so so so so easy!但我就是要要要要要要要要要发出来,分享给正在学习前端开发的朋友们,代码在下文直接复制走,而且不用说谢谢~嘿嘿。时不时发一点好玩的特效,喜欢的朋友点一下上面的关注哦。我只是小小的前端开发工程师而已,各位大佬别喷我,谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢啦。(你还别说,这支付宝体真挺好玩。)别喷我,比起让你加群拿代码的那些教育机构的推广者,我连个广告都没打!先上一个效果图(键盘控制的,效果有点鬼畜!)按下键盘上的A 网页上的A就会动哦~代码如下()<!doctype html><html><head><meta charset=”utf-8″><title>网页键盘钢琴按键特效</title><style>*{margin:0;padding:0;list-style:none;}#box{width:400px;margin:300px auto 0;}ul{margin-right:-20px;}li{width:48px;height:48px;border:1px solid #666;float:left;margin-right:20px;text-align:center;line-height:48px;background:#000;color:#fff;font-weight:bold;position:relative;}li span{display:block;opacity:0.5;filter:alpha(opacity:50);}li p{width:100%;height:0;background:#03F;position:absolute;bottom:49px;left:0;}.keyA{background:#e51c55;}.keyS{background:#c926a5;}.keyD{background:#9c4bd5;}.keyJ{background:#4f65c3;}.keyK{background:#6ccab7;}.keyL{background:#92d55d;}</style></head><body><div id=”box”><ul> <li> <span class=”keyA”>A</span> <p class=”keyA”></p> </li> <li> <span class=”keyS”>S</span> <p class=”keyS”></p> </li> <li> <span class=”keyD”>D</span> <p class=”keyD”></p> </li> <li> <span class=”keyJ”>J</span> <p class=”keyJ”></p> </li> <li> <span class=”keyK”>K</span> <p class=”keyK”></p> </li> <li> <span class=”keyL”>L</span> <p class=”keyL”></p> </li> </ul></div><script>window.onload = function(){var aP = document.getElementsByTagName(“p”);var aSpan = document.getElementsByTagName(“span”);var aKeyCode = [65,83,68,74,75,76]; //定义键盘按键的键值 65表示大写A 具体键值请参照文章里的ascii表里的值document.onkeydown = function(event){var event = event || window.event;for(var i=0; i<aKeyCode.length; i++){if(event.keyCode == aKeyCode[i]){startMove(aP[i],{height:240});startMove(aSpan[i],{opacity:100})}}}document.onkeyup = function(event){var event = event || window.event;for(var i=0; i<aKeyCode.length; i++){if(event.keyCode == aKeyCode[i]){startMove(aP[i],{height:0});startMove(aSpan[i],{opacity:50})}}}}function startMove(obj, json, fnEnd){clearInterval(obj.timer); obj.timer=setInterval(function (){var bSwitch=true;for(var sAttribute in json){var iTarget=json[sAttribute];if(sAttribute==’opacity’){var cur=Math.round(parseFloat(getStyle(obj, sAttribute))*100); }else{var cur=parseInt(getStyle(obj, sAttribute));}var speed=(iTarget-cur)/5;speed=speed>0?Math.ceil(speed):Math.floor(speed);if(sAttribute==’opacity’){obj.style.filter=’alpha(opacity:’+(cur+speed)+’)’; //IEobj.style.opacity=(cur+speed)/100; //ff chrome}else{obj.style[sAttribute]=cur+speed+’px’;}if(cur!=iTarget){bSwitch=false;}}if(bSwitch){ clearInterval(obj.timer); if(fnEnd){fnEnd(); }}}, 30);}//获取飞行间样式function getStyle(obj,sAuttribute){var result = “”if(window.getComputedStyle){result = window.getComputedStyle(obj,false)[sAuttribute];}else{resule = obj.currentStyle[sAuttribute];}return result;}</script></body></html>ASCII码表如果哪句代码不理解,可以直接在下面留言,我看到了会一一解释哦。当然啦,你喷我我也会回你。喜欢JS特效的朋友,记得关注我哦,不定期分享好玩好看的JS特效,前端路上,一起成长~