博客園主頁點擊出現小星星效果


同樣事先聲明:本文借鑒於他人作品,如有侵權請告知QAQ

借鑒博客 :https://www.cnblogs.com/Frank-dev-blog/

上一篇是背景動態線條

這里有兩個例子,可以試試

不多說直接上代碼:

一、

<!-- 愛心特效 -->
<script type="text/javascript">

(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>

把這個代碼加入到博客側邊公告欄里 (要申請js權限):

 

二、

這一種是帶文字的效果如下:

 

 

上代碼:首先是css代碼:

    

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

直接復制到css代碼框里面:

 

   然后是js代碼:

<script>
    // 點擊出的文字數組,可自行添加,不要太多哦
    text = ["你好呀~", "點我呀~", "啦啦啦~", "哎呀呀~", "求關注~", "帥哥美女~", "哦~", "咦~"];
    // 計數
    var count = 0;
    // 鼠標按下事件
    document.body.onmousedown = function (e) {
        // 獲取鼠標點擊位置
        var x = event.pageX - 18;
        var y = event.pageY - 30;
        // 分別創建所需要的元素節點
        var img = document.createElement("div");
        var left = document.createElement("div");
        var right = document.createElement("div");
        var under = document.createElement("div");
        var txt = document.createElement("div");
        // 通過隨機數從文字數組中獲取隨機下標的文字
        var textNode = document.createTextNode(text[parseInt(Math.random() * text.length)]);
        // 文字添加到txt節點中
        txt.appendChild(textNode);

        img.className = "img" + " " + "img" + count;
        left.className = "left";
        right.className = "right";
        under.className = "under";
        txt.className = "text";
        img.style.top = y + "px";
        img.style.left = x + "px";
        // 將創建的元素添加到容器中
        img.appendChild(left);
        img.appendChild(right);
        img.appendChild(under);
        img.appendChild(txt);
        document.body.appendChild(img);
        // 獲取隨機顏色
        var color = "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," +
            parseInt(Math.random() * 255) + ")";
        txt.style.color=color;
        for (var i = 0; i < 3; i++) {
            img.children[i].style.background = color;
        }
    }
    // 鼠標抬起事件
    document.body.onmouseup = function () {
        document.getElementsByClassName("img" + count)[0].style.transform = "scale(0.5)";
        document.getElementsByClassName("img" + count)[0].style.transform = "translateY(-40px)";
        document.getElementsByClassName("img" + count)[0].style.opacity = "0";
        count++;
    }
</script>

 

 把js代碼復制到博客側邊欄公告(要開通js權限)如圖:

 

我用的是第一種方法;第二種我用的有一點小bug所以我選了第一種朋友們可以根據自己的效果選擇

有人更好的效果和方法就和我分享一下子感謝各位老鐵

有錯誤的地方還請糾正我會修改

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM