練習(xí):在左邊輸入框,輸入任意一段話,然后點(diǎn)擊中間將文字右移按鈕,觀察效果

效果:https://songboriceboy.github.io/js_homework/word_move.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>word_move</title>
    <link href="reset.css" rel="stylesheet">
    <style>
        body{
            background-color:#C0C0C0 ;
        }
        ul{
            width:800px;
            margin:50px auto;
            background-color:white;
            padding: 20px;
            overflow: hidden;
            border-radius: 10px;
        }
        li{
            float:left;
        }
        button
        {
            border: 0;
            background-color: orangered;
            color:white;
            width: 110px;
            text-align: center;
            height: 40px;
            margin-left: 5px;
            margin-right: 5px;
            font-size:16px;
        }
        #left{
            background-color:#EFEFD6;
            width:336px;
            height: 240px;
            border: 0;
            font-family: '微軟雅黑';
            font-size: 18px;

        }
        #right{
            background-color:#63EFF7;
            width:336px;
            height: 240px;
            border: 0;
            font-family: '微軟雅黑';
            font-size: 18px;
        }
        h1
        {
            font-size:20px;
            line-height:60px ;
            text-align: center;
        }
    </style>
    <script>
        window.onload = function(){


            var button = document.getElementById("button");
            var h1 = document.getElementById("h1");
            var timer;
            function less(index,arr) {
                var str = "";
                for(var i = index ; i < arr.length ; i++)
                {
                    str += arr[i];
                }
                return str;

            }
            function more(index,arr) {
                var str = "";
                for(var i = 0 ; i < index ; i++)
                {
                    str += arr[i];
                }
                return str;
            }
            button.onclick = function () {
                clearInterval(timer);
                var left = document.getElementById("left");
                var right = document.getElementById("right");
                if(left.value ==="")
                {
                    alert("請(qǐng)?jiān)谧髠?cè)輸入一些文字后再點(diǎn)擊按鈕");
                    return;
                }
                var leftarr = left.value.split("");
                var length = leftarr.length;
                var index = 0;
                timer = setInterval(function () {
                        left.value = less(index,leftarr);
                        right.value = more(index,leftarr);
                        h1.innerHTML = `${index}/${length}`;
                        if(index === length)
                        {
                            clearInterval(timer);
                        }
                        index++;
                    }
                    ,80)
            }
        }
    </script>
</head>
<body>
<ul>
    <li><textarea id="left"></textarea></li>
    <li><button id="button">把文字右移</button><h1 id="h1">0/0</h1></li>
    <li><textarea id="right"></textarea></li>
</ul>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容