温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jQuery如何实现点击出现爱心特效

发布时间:2021-08-23 10:03:23 阅读:163 作者:小新 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍jQuery如何实现点击出现爱心特效,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体内容如下

jQuery如何实现点击出现爱心特效

代码:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <title>爱心效果</title>
        <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
        <style type="text/css">
            #love {
                width30px;
                height30px;
                /*border: 1px solid red;*/
                position: absolute;
            }
            
            #first {
                width15px;
                height26px;
                background-color: red;
                position: absolute;
                right3.2px;
                bottom0;
                transformrotate(45deg);
                border-radius10px 10px 1px 1px;
                opacity1;
            }
            
            #second {
                width15px;
                height26px;
                background-color: red;
                position: absolute;
                left3.2px;
                bottom0;
                transformrotate(-45deg);
                border-radius10px 10px 1px 1px;
                opacity1;
            }
        </style>
    </head>

    <body></body>

    <script type="text/javascript">
        function random(lower, upper) {
            return Math.floor(Math.random() * (upper - lower)) + lower;
        }
        var body = document.getElementsByTagName("body")[0];
        document.onclick = function(e) {
            var num = random(019);
            // 颜色数组
            var color = ["peru""goldenrod""yellow",
                "chartreuse""palevioletred""deeppink",
                "pink""palegreen""plum",
                "darkorange""powderblue""orangered",
                "orange""orchid""red",
                "aqua""salmon""gold""lawngreen"
            ]

            var divmain = document.createElement("div");
            var first = document.createElement("div");
            var second = document.createElement("div");
            // 给div加属性
            divmain.setAttribute("id""love");
            divmain.setAttribute("class""love");
            first.setAttribute("id""first");
            second.setAttribute("id""second");
            // 向最外层内添加内层div
            divmain.appendChild(first);
            divmain.appendChild(second);
            // 根据鼠标位置来确定div的位置
            //divmain.style.top = e.pageY + "px";
            //divmain.style.left = e.pageX + "px";
            divmain.style.cssText = "top:" + e.pageY + "px;left:" + e.pageX + "px";

            // 给心形div加随机颜色
            first.style.backgroundColor = color[num];
            second.style.backgroundColor = color[num];
            body.appendChild(divmain);

            $(".love").animate({
                opacity"0",
                top"-=50px"
            }, 1500);

        }

        // 利用定时器来清除页面的垃圾
        setInterval(function() {
            var div = document.getElementsByClassName("love");
            var len = div.length;
            var num;
            for(var i = len - 1; i >= 0; i--) {
                num = parseInt(div[i].style.opacity);
                if(num <= 0) {
                    div[i].remove();
                }
            }

        }, 3500);
    </script>

</html>

以上是“jQuery如何实现点击出现爱心特效”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×