【jQuery】 根据高度判断多行展开

多行展开

图片描述

图片描述

  1. introduce-more-box是根据高度决定是否追加展开箭头的div

  2. intro-more 这是阿里字体库的图标(可以忽略)

<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值