原生js添加博客点击鼠标出小心心效果~~

  昨天刚申请成功JS权限,心血来潮想添加点东西,记得之前看到别人家博客首页点击鼠标的时候会出现炫酷的 “小心心”,自己也来搞一个。没有用jquery啥的框架,原生js写起来麻烦了点,不过主要是怕博客首页加载不进去jquery所以先这么着试试,意料之中没有问题。

  毕竟刚开始给自己博客添加JS才疏学浅,有更好的方法添加更炫酷的动态效果可以分享给我呀。有大神给指点指点那就更多谢啦。欢迎指正错误~

  效果如下:

 

话不多说了,先添加css代码,主要是用来画“小心心”的:

1 body {position: relative;}
2 .img {width: 20px;height: 20px;opacity: 1;position: absolute;z-index: 100000;transition: 1s;}
3 .left,.right {width: 10px;height: 10px;border-radius: 100%;position: absolute;}
4 .right {right: 0;}
5 .under {width: 10px;height: 10px;position: absolute;top: 5px;left: 5px;transform: rotate(45deg)}
6 .text {width: 50px;font-size: 10px;line-height: 1;position: absolute;top: -1em;left: -15px;text-align: center;}

啰嗦几句,我添加在页面定制css代码 那个文本框里面没有生效,于是乎干脆在侧边栏公告代码 中添加style标签后添加css就好了:

 1 <style>
 2 body{
 3 position:relative;
 4 }
 5 .img {width: 20px;height: 20px;opacity: 1;position: absolute;z-index: 100000;transition: 1s;}
 6 .left,.right {width: 10px;height: 10px;border-radius: 100%;position: absolute;}
 7 .right {right: 0;}
 8 .under {width: 10px;height: 10px;position: absolute;top: 5px;left: 5px;transform: rotate(45deg)}
 9 .text {width: 50px;font-size: 10px;line-height: 1;position: absolute;top: -1em;left: -15px;text-align: center;}
10 </style>
11 <script>

 

接下来是js:

 1 <script>
 2     // 点击出的文字数组,可自行添加,不要太多哦
 3     text = ["你好呀~", "点我呀~", "啦啦啦~", "哎呀呀~", "求关注~", "帅哥美女~", "哦~", "咦~"];
 4     // 计数
 5     var count = 0;
 6     // 鼠标按下事件
 7     document.body.onmousedown = function (e) {
 8         // 获取鼠标点击位置
 9         var x = event.pageX - 18;
10         var y = event.pageY - 30;
11         // 分别创建所需要的元素节点
12         var img = document.createElement("div");
13         var left = document.createElement("div");
14         var right = document.createElement("div");
15         var under = document.createElement("div");
16         var txt = document.createElement("div");
17         // 通过随机数从文字数组中获取随机下标的文字
18         var textNode = document.createTextNode(text[parseInt(Math.random() * text.length)]);
19         // 文字添加到txt节点中
20         txt.appendChild(textNode);
21 
22         img.className = "img" + " " + "img" + count;
23         left.className = "left";
24         right.className = "right";
25         under.className = "under";
26         txt.className = "text";
27         img.style.top = y + "px";
28         img.style.left = x + "px";
29         // 将创建的元素添加到容器中
30         img.appendChild(left);
31         img.appendChild(right);
32         img.appendChild(under);
33         img.appendChild(txt);
34         document.body.appendChild(img);
35         // 获取随机颜色
36         var color = "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," +
37             parseInt(Math.random() * 255) + ")";
38         txt.style.color=color;
39         for (var i = 0; i < 3; i++) {
40             img.children[i].style.background = color;
41         }
42     }
43     // 鼠标抬起事件
44     document.body.onmouseup = function () {
45         document.getElementsByClassName("img" + count)[0].style.transform = "scale(0.5)";
46         document.getElementsByClassName("img" + count)[0].style.transform = "translateY(-40px)";
47         document.getElementsByClassName("img" + count)[0].style.opacity = "0";
48         count++;
49     }
50 </script>

到这就没了,不过等下,想添加这些东西在你的博客首页上生效,你得先申请博客园的js权限呀 = = 

 

 

转载于:https://www.cnblogs.com/Mr-Car/p/10721471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值