原生js实现评论

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>评论</title>
    <style>
        *{
            margin:0;
            padding: 0;
        }
        body{
            background-color: pink;
        }
        .outer{
            width: 600px;
            margin:20px auto 0px;
            padding:20px 40px;
            background-color: #fff;
            border-radius:8px;
        }
        .head{
            font-size: 20px;
            margin-bottom: 20px;
        }
        #userName{
            font-size: 20px;
            outline:none;
            padding: 2px 8px;
        }
        #userWords{
            width: 100%;
            height: 100px;
            outline: none;
            resize: none;
            font-size: 14px;
            padding: 10px;
        }
        .word_footer{
            margin-top: 20px;
            line-height: 30px;
            font-size: 20px;
        }
        #btn{
            float:right;
            width: 80px;
            height: 30px;
            border:none;
            background: #ccc;
            color:#fff;
            font-size: 14px;
            border-radius: 5px;
            outline: none;
        }
        #myList{
            margin-top: 20px;
        }
        #myList li{
            list-style: none;
            overflow: hidden;
        }
        #myList li img{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            float:left;
        }
        .right{
            float:right;
            width: 530px;
        }
        .top a{
            float:right;
            text-decoration: none;
            color:gray;
        }
        .top a:hover{
            color:orange;
        }
        .words{
            margin-top: 10px;
            margin-bottom: 20px;

        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="head">
            用户:<input type="text" id="userName">
        </div>
        <textarea name="" id="userWords" cols="30" rows="10"></textarea>
        <div class="word_footer">
            <span id="wordsNum">还可以输入140个字</span>
            <button id="btn">发布</button>
        </div>
        <ul id="myList">

        </ul>
    </div>

    <script src="../tween.js"></script>
    <script>
        function getId(str){
            return document.getElementById(str);
        }
        var userName = getId('userName');
        var userWords = getId('userWords');
        var wordsNum = getId('wordsNum');
        var btn = getId('btn');
        var myList = getId('myList');
        userName.oninput = function(){
            // 当两个输入框内容都不为空时,改变按钮颜色,可以点击
            judge();
        }
        userWords.oninput = function(){
            // 当两个输入框内容都不为空时,改变按钮颜色,可以点击
            judge();
            // 每次输入都获取内容长度,并计算剩余可输入字数
            var wLength = 140 - userWords.value.length;
            if(wLength <= 0){
                wLength = 0;
                // userWords.disabled = true;
                userWords.value.innerHTML = userWords.value.substring(0,140);
            }
            wordsNum.innerHTML = '还可以输入' + wLength + '个字';
        }
        function judge(){
            if(userWords.value && userName.value){
                btn.style.backgroundColor = 'orangered';
                btn.onclick = function(){
                    var newLi = document.createElement('li');
                    newLi.innerHTML = '<img src="1.gif" alt=""><div class="right"><p class="top"><span class="name">'+userName.value+'</span><a href="#" class="remove">删除</a></p><p class="words">'+userWords.value+'</p></div>';
                    // 将创建好的li添加到ul中
                    if(myList.children.length != 0){
                        console.log(myList.children);
                        myList.insertBefore(newLi,myList.firstElementChild);
                    }else{
                        myList.appendChild(newLi);
                    }
                    // 获取到插入的li的高度,再将li设置为0
                    var end = newLi.offsetHeight;
                    var start = 0;
                    var change = end - start;
                    var t = 0;
                    var maxT = 50;
                    var time = setInterval(function(){
                        t++;
                        if(t >= maxT){
                            clearInterval(time);
                        }
                        newLi.style.height = Tween.Elastic.easeOut(t,start,change,maxT) + 'px';
                    },20);

                    // 元素添加完毕,清除输入框
                    userName.value = '';
                    userWords.value = '';
                    wordsNum.innerHTML = '还可以输入140个字';
                    btn.onclick = null;
                    btn.style.backgroundColor = '#ccc';
                    var remBtn = myList.getElementsByTagName('a');
                    for(var i=0; i<remBtn.length; i++){
                        remBtn[i].onclick = function(){
                            var t = 0;
                            var myLi = this.parentNode.parentNode.parentNode;
                            var start = myLi.offsetHeight;
                            var end = 0;
                            var change = end - start;
                            var maxT = 50;
                            var time = setInterval(function(){
                                t++;
                                if(t >= 50){
                                    clearInterval(time);
                                    myList.removeChild(myLi);
                                }
                                myLi.style.height = Tween.Elastic.easeOut(t,start,change,maxT) + 'px';
                            },20);

                        }
                    }
                }

            }else{
                btn.style.backgroundColor = '#ccc';
                btn.onclick = null;
            }
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值