【jQuery】 根據高度判斷多行展開

多行展開

圖片描述

圖片描述

  1. introduce-more-box是根據高度決定是否追加展開箭頭的divjavascript

  2. intro-more 這是阿里字體庫的圖標(能夠忽略)css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .cor-introduce{
            line-height: 24px;
            color: #999;
            position: relative;
            overflow: hidden;
        }
        
        .introduce-more-box{
            text-align: right;
        }
        .intro-more{
            color: #999;
            line-height: 24px;
        }
    </style>
</head>
<body>


<p class="cor-introduce">簡介:多行展開。</p>
<div class="introduce-more-box"></div>

<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
      $(function(){
            //實際高度
            var infoHeight = $(".cor-introduce").height();
            // alert(infoHeight)
            //默認高度
            var defHeight = 72;
            // 若是高度超出
            if (infoHeight > defHeight) {
                // 給p設置默認高度,隱藏超出部分
                $('.cor-introduce').css('height', defHeight + 'px');
                //加按鈕
                $(".introduce-more-box").append('<a href="javascript:;" class="iconfont intro-more">&#xe61c;</a>');

                // 點擊按鈕
                $(".intro-more").click(function() {
                    var curHeight = $('.cor-introduce').height();
                    if (curHeight == defHeight) {
                        $(".cor-introduce").height("auto");
                        $(this).html('<a href="javascript:;" class="iconfont intro-more">&#xe61b;</a>')
                    } else{
                        $('.cor-introduce').height(defHeight);
                        $(this).html('<a href="javascript:;" class="iconfont intro-more">&#xe61c;</a>');
                    };
                });
            } 
        })
</script>

</body>
</html>
相關文章
相關標籤/搜索