美文网首页
html +css 超出两行显示省略 及显示全文按钮(第二种效果

html +css 超出两行显示省略 及显示全文按钮(第二种效果

作者: Pino | 来源:发表于2021-10-12 17:38 被阅读0次

    少废话 先上图


    省略号2.gif

    少废话 直接上代码了

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                font-size: 16px;
            }
            .content {
                width: 100%;
                position: relative;
                overflow: hidden;
                padding: 10px;
                box-sizing: border-box;
            }
            .text {
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-box-pack: center;
                -webkit-box-align: center;
                -webkit-line-clamp: 2;
                overflow: hidden;
            }
    
            .show—all1 label ,.show—all2 label{
                color: red;
                border: 1px solid red;
                border-radius: 2px;
                font-size: 12px;
            }
    
            .show—all2 {
                display: none;
                position: absolute;
                bottom: 10px;
                right: 10px;
                background-color: #fff;
                line-height: 20px;
                z-index: 100;
            }
        </style>
    </head>
    
    <body>
        <div class="content">
            <div class="text">
                发鬼地方个覆盖电饭锅电饭锅豆腐干豆腐电饭锅豆腐干豆腐豆腐干豆腐换地方大范甘迪发鬼地方个的发给对方
                <span class="show—all1"><label onclick="showAll()">全文</label></span>
            </div>
            <span class="show—all2">... <label onclick="showAll()">全文</label></span>
        </div>
    </body>
    
    </html>
    
    <script>
        // 根绝内容宽度来显示 标签
        window.onresize = function () {
            var content = document.getElementsByClassName("content")[0]
            var textview = document.getElementsByClassName("text")[0]
            var showAll1 = document.getElementsByClassName("show—all1")[0]
            var showAll2 = document.getElementsByClassName("show—all2")[0]
    
            var scrollHeight = textview.scrollHeight
            var height = content.clientHeight
            if (scrollHeight > height) {
                console.log('被省略了')
                showAll1.style.display = 'none';
                showAll2.style.display = 'block';
            }else{
                console.log('完全显示')
                showAll1.style.display = 'inline-block';
                showAll2.style.display = 'none';
            }
        }
        function showAll() {
            alert("显示全文")
        }
    </script>
    

    相关文章

      网友评论

          本文标题:html +css 超出两行显示省略 及显示全文按钮(第二种效果

          本文链接:https://www.haomeiwen.com/subject/euqjoltx.html