点击页面爱心效果

前端开发 作者: 2024-08-26 06:30:01
1 2 (function (window, document, undefined) { 3 var hearts = []; 4 window.requestAnimationFrame = (f
 1 <!-- 为页面添加爱心特效 -->
 2 (function (window,document,undefined) {
 3     var hearts = [];
 4     window.requestAnimationFrame = ( () {
 5         return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
 6              (callback) {
 7                 setTimeout(callback,1000 / 60);
 8             }
 9     })();
10     init();
11 
12      init() {
13         css(".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: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}"14         attachEvent();
15         gameloop();
16     }
17 
18      gameloop() {
19         for (var i = 0; i < hearts.length; i++) {
20             if (hearts[i].alpha <= 021                 document.body.removeChild(hearts[i].el);
22                 hearts.splice(i,123                 continue;
24 25             hearts[i].y--26             hearts[i].scale += 0.00427             hearts[i].alpha -= 0.01328             hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
29         }
30         requestAnimationFrame(gameloop);
31 32 
33      attachEvent() {
34         var old = typeof window.onclick === "function" && window.onclick;
35         window.onclick =  (event) {
36             old && old();
37             createHeart(event);
38 39 40 
41      createHeart(event) {
42         var d = document.createElement("div"43         d.className = "heart"44         hearts.push({
45             el: d,46             x: event.clientX - 5,1)">47             y: event.clientY - 548             scale: 149             alpha: 150             color: randomColor()
51         });
52         document.body.appendChild(d);
53 54 
55      css(css) {
56         var style = document.createElement("style"57         style.type = "text/css"58         try {
59             style.appendChild(document.createTextNode(css));
60         } catch (ex) {
61             style.styleSheet.cssText = css;
62 63         document.getElementsByTagName('head')[0].appendChild(style);
64 65 
66      randomColor() {
67         return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")"68 69 })(window,document);
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68826.html
点击页面爱心效果