网站添加点击爱心特效

这是一个非常好看的特效,因为,我建了一个表白墙,所以就非常喜欢这个特效,于是在网上找了很多,终于在自己的努力下,找到一个不需要借助外力的js文件,就可以轻松实现这个功能的代码

直接显示js

非常好用的代码,话不多说,代码就放在下面吧

<script type="text/javascript">
        /*cdn.yvu.cc - 网页鼠标点击特效(爱心)*/
        !function (e, t, a) {function r() {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");requestAnimationFrame(r)}function n() {var t = "function" == typeof e.onclick && e.onclick;e.onclick = function (e) {t && t(), o(e)}}function o(e) {var a = t.createElement("div");a.className = "heart", s.push({el: a,x: e.clientX - 5,y: e.clientY - 5,scale: 1,alpha: 1,color: c()}), t.body.appendChild(a)}function i(e) {var a = t.createElement("style");a.type = "text/css";try {a.appendChild(t.createTextNode(e))} catch (t) {a.styleSheet.cssText = e}t.getElementsByTagName("head")[0].appendChild(a)}function c() {return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"}var s = [];e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {setTimeout(e, 1e3 / 60)}, 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()}(window, document);
</script>

以上代码你可以放在foot.php上,也就是页面底部,头部也行,反正能够全覆盖

外部嵌入js

当然你也可以选择外接js文件

在你的外部建一个js文件,就比如说love.js,放在自己一个目录下面,然后把下面代码放在你的love.js

(function(window,document,undefined){
    var hearts = [];
    window.requestAnimationFrame = (function(){
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function (callback){
    setTimeout(callback,1000/60);
    }
    })();
    init();
    function init(){
    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;}");
    attachEvent();
    gameloop();
    }
    function gameloop(){
    for(var i=0;i<hearts.length;i++){
    if(hearts[i].alpha <=0){
    document.body.removeChild(hearts[i].el);
    hearts.splice(i,1);
    continue;
    }
    hearts[i].y--;
    hearts[i].scale += 0.004;
    hearts[i].alpha -= 0.013;
    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;
    }
    requestAnimationFrame(gameloop);
    }
    function attachEvent(){
    var old = typeof window.onclick==="function" && window.onclick;
    window.onclick = function(event){
    old && old();
    createHeart(event);
    }
    }
    function createHeart(event){
    var d = document.createElement("div");
    d.className = "heart";
    hearts.push({
    el : d,
    x : event.clientX - 5,
    y : event.clientY - 5,
    scale : 1,
    alpha : 1,
    color : randomColor()
    });
    document.body.appendChild(d);
    }
    function css(css){
    var style = document.createElement("style");
    style.type="text/css";
    try{
    style.appendChild(document.createTextNode(css));
    }catch(ex){
    style.styleSheet.cssText = css;
    }
    document.getElementsByTagName('head')[0].appendChild(style);
    }
    function randomColor(){
    return "rgb("+(~~(Math.random()*255))+","+(~~(Math.random()*255))+","+(~~(Math.random()*255))+")";
    }
    })(window,document);

然后找到一个合适的地方加入以下代码

<!-- 点击爱心 -->
    <script src="js链接地址"></script>

好了,差不多就是这样,不过我还是推荐使用第一种方法,因为比较简单,而且也还很好用,不懂得评论区,大佬就不用吐槽了,我是个小白哈 @qq(64) @qq(64) @qq(64)

Comments | 7 条评论
  • ejsoon

    最終我還是拿走了,哈哈!請問本文可以轉載嗎?

  • ejsoon

    我本來想拿走,後來發現它跟我的博客風格不太搭…

  • 小予

    office 2021 永久密钥
    NJHVR-FK6XR-M97MB-P8CXD-FM4PJ

    NXVJY-X9T8G-Y6RWG-8VQ7V-PWHPT

    W8W6K-3N7KK-PXB9H-8TD8W-BWTH9

    N9J9Q-Q7MMP-XDDM6-63KKP-76FPM

    9DP6T-9AGWG-KWV33-9MPC8-JDCVF

    7G2HE-JR8KL-ABB9D-Y7789-GLNFL

    U2PWU-H7D9H-69T3B-JEYC2-3R2NG

    R8R8P-MTT6F-KLRPM-J7CAB-PJM8C

    A4XV7-QP9JN-E7FCB-VQFRD-4NLKC

    U272H-HH2F6-WDG36-ULUPX-PCHTJ

    7LR67-WTXPA-KLUHV-GEK2E-QW4CK

    EUYTH-3KWKL-PJMX7-XBCPW-9U2DD

    office 2021 激活 KEY 激话密钥:

    9dp6T-9AGwG-KwV33-9MpC8-JdCVF

    7G2HE-JR8KL-ABB9d-Y7789-GLnFL

    U2pwU-H7d9H-69T3B-JEYC2-3R2nG

    R8R8p-MTT6F-KLRpM-J7CAB-pJM8C

    A4xV7-Qp9Jn-E7FCB-VQFRd-4nLKC

    U272H-HH2F6-wdG36-ULUpx-pCHTJ

    7LR67-wTxpA-KLUHV-GEK2E-Qw4CK

    EUYTH-3KwKL-pJMx7-xBCpw-9U2dd

  • 摄影协会

    小白表示看不懂,看似很高级的样子。

消息盒子
# 您有1条未读消息 #
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息