方法一:直接在script中加代码
1 !function (e, t, a) { 2 function r() { 3 for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999"); 4 requestAnimationFrame(r) 5 } 6 function n() { 7 var t = "function" == typeof e.onclick && e.onclick; 8 e.onclick = function (e) { 9 t && t(), o(e) 10 } 11 } 12 function o(e) { 13 var a = t.createElement("div"); 14 a.className = "heart", s.push({ 15 el: a, 16 x: e.clientX - 5, 17 y: e.clientY - 5, 18 scale: 1, 19 alpha: 1, 20 color: c() 21 }), t.body.appendChild(a) 22 } 23 function i(e) { 24 var a = t.createElement("style"); 25 a.type = "text/css"; 26 try { 27 a.appendChild(t.createTextNode(e)) 28 } catch (t) { 29 a.styleSheet.cssText = e 30 } 31 t.getElementsByTagName("head")[0].appendChild(a) 32 } 33 function c() { 34 return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")" 35 } 36 var s = []; 37 e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) { 38 setTimeout(e, 1e3 / 60) 39 }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r() 40 }(window, document);
方法二:整个页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>测试</title> 6 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> 7 <style type="text/css"> 8 *{ 9 margin: 0px; 10 padding: 0px; 11 } 12 </style> 13 <script type="text/javascript"> 14 $(function(){ 15 var height=$(window).width(); 16 $('#test').css({ 17 'height':height, 18 }); 19 var n=1; 20 $('#test').click(function(e){ 21 if(n%2==0){ 22 var $i=$('<b></b>').text('你点击了一下');//双数显示这个 23 }else{ 24 var $i=$('<b></b>').text('❤');//单数显示这个 25 } 26 n++; 27 var x=e.pageX,y=e.pageY;//获取鼠标点击的位置坐标 28 $i.css({ 29 "z-index": 9999, 30 "top": y - 20, 31 "left": x, 32 "position": "absolute", 33 "color": 'red', 34 "font-size": 14, 35 }); 36 $("body").append($i); 37 $i.animate({ 38 "top": y - 180, 39 "opacity": 0 40 }, 1500, function() { 41 $i.remove(); 42 });//设置动画 43 }); 44 }); 45 </script> 46 </head> 47 <body> 48 <div id="test"> 49 </div> 50 </body> 51 </html>
作者:缪鹏
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,如果你心情还比较高兴,可以扫码打赏博主哦,(*^▽^*)