点击页面爱心效果

发布时间:2020-11-21 发布网站:编程之家
编程之家收集整理的这篇文章主要介绍了点击页面爱心效果编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 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);

 

总结

以上是编程之家为你收集整理的点击页面爱心效果全部内容,希望文章能够帮你解决点击页面爱心效果所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478