前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原生JS实现飘浮关键词特效

原生JS实现飘浮关键词特效

作者头像
越陌度阡
发布2020-11-26 15:17:11
1.4K0
发布2020-11-26 15:17:11
举报

分享一个原生JS实现的飘浮关键词的效果,鼠标悬停可让关键词停止运动,点击关键词可跳转到对应的页面,效果如下:

实现的代码如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS实现飘浮关键词特效</title>
    <style type="text/css">
        * {
            margin: 0px;
            margin: 0px;
        }

        body {
            background: #8a234e url(images/0.jpg) no-repeat center 0;
        }

        h1 {
            text-align: center;
            color: #fff;
            font-size: 16px;
            position: absolute;
            top: 100px;
            left: 50%;
            width: 300px;
            margin-left: -150px;
        }

        #div1 {
            position: relative;
            width: 500px;
            height: 260px;
            margin: 146px auto 0;
            overflow: hidden;
        }

        #div1 a {
            position: absolute;
            color: #000;
            text-decoration: none;
            top: 260px;
            display: block;
            border: solid 1px #000;
            background: #fff;
            filter: alpha(opacity=30);
            opacity: 0.3;
            font-size: 14px;
            padding: 3px 5px;
            font-family: arial;
        }

        #div1 a:hover {
            filter: alpha(opacity=00);
            opacity: 1;
            font-weight: bold;
            font-size: 16px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var oDiv = document.getElementById('div1');
            var aA = document.getElementsByTagName('a');
            var i = 0;
            for (i = 0; i < aA.length; i++) {
                aA[i].pause = 1;
                aA[i].time = null;
                initialize(aA[i]);
                aA[i].onmouseover = function () {
                    this.pause = 0;
                };
                aA[i].onmouseout = function () {
                    this.pause = 1;
                };
            }
            setInterval(starmove, 24);
            function starmove() {
                for (i = 0; i < aA.length; i++) {
                    if (aA[i].pause) {
                        domove(aA[i]);
                    }
                }
            }
            function domove(obj) {
                if (obj.offsetTop <= -obj.offsetHeight) {
                    obj.style.top = oDiv.offsetHeight + "px";
                    initialize(obj);
                } else {
                    obj.style.top = obj.offsetTop - obj.ispeed + "px";
                }
            }
            function initialize(obj) {
                var iLeft = parseInt(Math.random() * oDiv.offsetWidth);
                var scale = Math.random() * 1 + 1;
                var iTimer = parseInt(Math.random() * 1500);
                obj.pause = 0;

                obj.style.fontSize = 12 * scale + 'px';

                if ((iLeft - obj.offsetWidth) > 0) {
                    obj.style.left = iLeft - obj.offsetWidth + "px";
                } else {
                    obj.style.left = iLeft + "px";
                }
                clearTimeout(obj.time);
                obj.time = setTimeout(function () {
                    obj.pause = 1;
                }, iTimer);
                obj.ispeed = Math.ceil(Math.random() * 4) + 1;
            }
        };
    </script>
</head>

<body>
    <div id="div1">
        <a target="_blank">课程</a>
        <a target="_blank">教程</a>
        <a target="_blank">试听</a>
        <a target="_blank">精品</a>
        <a target="_blank">视频</a>
        <a target="_blank">特效</a>
    </div>
</body>

</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档