继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

多行部分折叠显示省略号

nicefuting
关注TA
已关注
手记 3
粉丝 0
获赞 26

**

方案1

**

无省略号的多行折叠

//先获得原始高度,animate height:auto 无效,不可以
$(function(){
            var $obj = $('#aa');   
            var currentHeight = $obj.css("height");
            $obj.css("height","auto");
            var animateHeight = $obj.css("height");
            $obj.css("height", currentHeight);
            $('#click').click(function(){

                if($('#aa').css('height') == currentHeight){
                    $('#aa').animate({height:animateHeight});
                    $('#aa').removeClass('active');
                }else{
                    $('#aa').animate({height:currentHeight});
                    $('#aa').addClass('active');
                }
            })
        })
.active:after{
    position:absolute;........
}

此方法不能用-webkit-line-clamp 方法。
如果一定要加省略号:
用绝对定位,省略号右下角定位,同时控制class的隐藏

缺点: 字可能会显示一半

image.png

**

方案2

**
同方案1
.active 不同

.active{  display:-webkit-box;  overflow:hidden; text-overflow:ellipsis; -webkit-line-clamp:2;-webkit-box-orient:vertical;}
            var $obj = $('#aa');
            $obj.removeClass('active');  
            var currentHeight = $obj.css("height");
            $obj.css("height","auto");
            var animateHeight = $obj.css("height");
            $obj.css("height", currentHeight);
            $obj.addClass('active');

缺点:仅支持webkit(在webkit下才有省略号),firefox etc 等无省略号

animate 的 效果是 用 行内样式 加上的。

**

方案3

**

jQuery.dotdotdot.js 插件
https://github.com/FrDH/jQuery.dotdotdot
http://dotdotdot.frebsite.nl/ (官网)

function() {
        var t = e("#xmpl-3");
        t.dotdotdot({
            keep: ".toggle"
        });
        var n = t.data("dotdotdot");
        t.on("click", ".toggle", function(e) {
            e.preventDefault(),
            t.hasClass("ddd-truncated") ? (n.restore(),
            t.addClass("full-story")) : (t.removeClass("full-story"),
            n.truncate(),
            n.watch())
        })
    }(),

image.png

image.png
中间截断

function() {
        e("#xmpl-2").find("li").each(function() {
            var t = e(this);
            t.html("<span>" + t.html().split("/").join("</span><span>/") + "</span>"),
            t.children().last().addClass("file"),
            e(this).dotdotdot({
                ellipsis: "/…",
                keep: ".file"
            })
        })
    }(),

image.png

**

方案4

**
clamp.js 插件
https://github.com/josephschmitt/Clamp.js

打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP