博客园主页点击出现小星星效果

同样事先声明:本文借鉴于他人作品,如有侵权请告知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所以我选了第一种朋友们可以根据自己的效果选择

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

有错误的地方还请纠正我会修改

 

上一篇:CSS盒模型


下一篇:2020-02-22