js实现跳动文字

效果展示:

源码展示:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>js实现文字跳动</title>

    <style>
        #txtDom {
            padding:50px;
            width:1200px;
            margin:0 auto;
            font-size:30px;
            font-family:"微软雅黑";
            line-height:1.3em;
            text-indent:2em;
        }
    </style>
</head>
<body>
<div id="txtDom">
    大家好 我是老王!今天给大家分享的是使用js实现跳动的文字效果!!!!
</div>

<script>
    var txtAnim = {
        len: 0,
        txtDom: "",
        arrTxt: [],
        init: function(obj) {
            this.obj = obj;
            this.txtDom = obj.innerHTML.replace(/\s+/g, "");
            this.len = this.txtDom.length;
            obj.innerHTML = "";
            this.addDom();
        },
        addDom: function() {
            for (var i = 0; i < this.len; i++) {
                var spanDom = document.createElement("span");
                spanDom.innerHTML = this.txtDom.substring(i, i + 1);
                this.obj.appendChild(spanDom);
                this.arrTxt.push(spanDom);
            };
            for (var j = 0; j < this.len; j++) {
                this.arrTxt[j].style.position = "relative";
            };
            this.strat();
        },
        strat: function() {
            for (var i = 0; i < this.len; i++) {
                this.arrTxt[i].onmouseover = function() {
                    this.stop = 0;
                    this.speed = -1;
                    var $this = this;
                    this.timer = setInterval(function() {

                        $this.stop += $this.speed; //0  += -1
                        if ($this.stop <= -20) {
                            $this.speed = 1;
                        }

                        $this.style.top = $this.stop + "px";

                        if ($this.stop >= 0) {
                            clearInterval($this.timer)
                            $this.style.top = 0 + "px";
                        };
                    }, 15);
                };
            }
        }
    }

    var txtDom = document.getElementById("txtDom");
    txtAnim.init(txtDom);
</script>


<pre style="color:red">
 感:  最近贡献一下我在教学中的小案例  希望能给你一些帮助 ,希望大家继续关注我的博客

                                                                               --王
</pre>
</body>
</html>

 

评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值