文字竖排demo2

文字竖排demo2-通过两次旋转的方式

by wu huan

HTML

<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<div class="container">
        <p>&nbsp;</p>
        <div class="items">              
            <div class="item"></div>          
            <div class="item"></div>
            <div class="item"></div>
            <div class="item rotate" style="font-size:36px;font-weight:bold;">爱</div>     
            <div class="item"></div>
            <div class="item rotate" style="font-size:36px;font-weight:bold;">莲</div> 
            <div class="item"></div>
            <div class="item rotate" style="font-size:36px;font-weight:bold;">说</div>
             <div class="item"></div>   

            <div class="item"></div>
            <div class="item"></div>         
            <div class="item"></div>
            <p>&nbsp;</p>
        </div>
        <p class='action1'></p>
    </div>

CSS

.container {
        position: absolute;
        right: 300px;
        margin-top: -200px;
        width: 440px;
        height: 1000px;
        background-color: #FFFFFF;
        padding-left: 36px;
        font-family: "楷体";
        border: 1px solid #999999;
        margin-left: 100px;
        overflow-y: scroll;
        overflow-x:hidden;
        -webkit-transform: rotate(90deg);
    }
    
    .container .items {
        width: 445px;
        height: 34px;
        /*background-color: #EEEEEE;*/
        /*border-left: 1px solid #999999;*/
    }
    
    .container .items .item {
        display: inline-block;
        width: 20px;
        height: 20px;
        font-size: 20px;
        text-align: center;
        line-height: 20px;
        /*border-bottom: 1px solid #CCCCCC;*/
    }
    
    .container .items .item:hover {
        background-color: pink;
        cursor: pointer;
        font-size: 22px;
    }
    
    .action1 {
        width: 100px !important;
        padding-left: 20px;
    }
    
    .rotate {
        -webkit-transform: rotate(-90deg);
    }
    
    .style1 {
        background-color: #ff6699;
        color: #EEEEEE;
    }
    
    .style2 {
        background-color: #4c98ce;
        color: #EEEEEE;
    }
    
    #vertical div {
        width: 1.5em;
        float: right;
    }
    
    #vertical .title {
        font-size: xx-large;
        font-weight: bold;
        line-height: 1em;
    }

JavaScript

var arr = "水陆草木之花,可爱者甚蕃。晋陶渊明独爱菊。自李唐来,世人甚爱牡丹。予独爱莲之出淤泥而不染,濯清涟而不妖,中通外直,不蔓不枝,香远益清,亭亭净植,可远观而不可亵玩焉。予谓菊,花之隐逸者也;牡丹,花之富贵者也;莲,花之君子者也。噫!菊之爱,陶后鲜有闻。莲之爱,同予者何人?牡丹之爱,宜乎众矣!".split("");

    var cHeight = $(".items").height();

    var textHeight = $(".item").height();

    var vCount = parseInt(cHeight / textHeight); //每列显示的字数

    var textArr = [];
    for (var i = 0; i < arr.length; i++) {
        if (i % 20 == 0) {
            textArr.push({
                "hanzi": arr[i]
            });
        } else {
            textArr[textArr.length - 1].hanzi += arr[i];
        }
    }

    var html = "";
    for (var j = 0; j < textArr.length; j++) {
        html += "<div class='items'>";
        var tempArr = textArr[j].hanzi.split("");
        console.log(tempArr)
        for (var k = 0; k < tempArr.length; k++) {
           if(/[<>《》!*(^)$%~!@#$…&%¥—+=、。,;‘’“”:·`]/.test(tempArr[k])){
                html += "<div class='item' style='display:inline'>" + tempArr[k] + "</div>";
            } else {
                html += "<div class='item rotate'>" + tempArr[k] + "</div>";
            }
        }
        html += "</div>";
    }
    
    $(".action1").after(html);