Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="test13">
  <div class="test13-placeholder"></div>
  <div class="test13-more">...更多</div>
  <span class="test13-text"><!--注释-->文字文<span>
    </span>
    <span style="display: inline-block; width: 30px;height: 30px; background: pink;margin: 10px;">
    </span>

    字文字文字文字文字文字文字文<span>字<span>文<span>字</span>文</span>字</span>文字english文字文字文字文字文字文字文字文字文字文字文字文字文字文
    <span style="display: inline-block; width: 30px;height: 30px; background: pink;margin: 10px;">
      ok
    </span>
    字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字字文字文字文字文字文字文字</span>
</div>
              
            
!

CSS

              
                .test13 {
    position: relative;
    overflow: hidden;
    text-align: justify;
    background-color: rgba(0,0,0,.2);
    line-height: 1.25em;
}
.test13-text {
    display: inline;
    word-break: break-all;
    word-wrap: break-word; 
    word-break: normal; 
}
.test13-more {
    display: none;
    clear: right;
    float: right;
    line-height: 1;
    background-color: rgba(0,0,0,.2);
}
.test13.is-ellipsis .test13-more {
    display: block;
}
.test13-placeholder {
    content: "";
    display: none;
    width: 0;
    height: 57px;
    float: right;
}
.test13.is-ellipsis .test13-placeholder {
    display: block;
}
              
            
!

JS

              
                !function(doc, win, classList, matches, addSelectorListener, getRowRects) {
    var selector = ".test13",
        maxRow = 2,
        elements = [];

    function ellipsis(element, selector) {
        [].forEach.call(element.children, function(child) {
            if(child.matches(selector+"-text")) {
                var rowRects = getRowRects(child),
                    elementRect = element.getBoundingClientRect(),
                    lineHeight = 0;

                if(rowRects.length > maxRow){
                    element.classList.add("is-ellipsis");
                    element.querySelector(selector+"-placeholder").style.height = (rowRects[maxRow - 1].top - elementRect.top) + "px";
                    lineHeight = rowRects[maxRow].top - rowRects[maxRow - 1].bottom;
                    element.style.maxHeight = (rowRects[maxRow - 1].bottom + lineHeight/2 - elementRect.top) + "px";
                }
                else {
                    element.classList.remove("is-ellipsis");
                }
                return false;
            }
        })
    }

    addSelectorListener(selector, function(element, selector) {//监听选择器
        elements.push({element: element, selector: selector});
        ellipsis(element, selector);
    });
    win.addEventListener("resize" ,function() {//窗口尺寸变化
        elements.forEach(function(item, index) {//已有元素
            if(item.element.parentNode) {
                ellipsis(item.element, item.selector);
            }
            else {
                elements.splice(index, 1);//如果没有父元素则删除引用
            }
        })
    });
}(
    document,
    window,
    (function() {//classList polyfill,不支持classList.forEach
        /*! @source https://purl.eligrey.com/github/classList.js/blob/master/classList.js */
        if("document" in self){if(!("classList" in document.createElement("_"))){(function(j){"use strict";if(!("Element" in j)){return}var a="classList",f="prototype",m=j.Element[f],b=Object,k=String[f].trim||function(){return this.replace(/^\s+|\s+$/g,"")},c=Array[f].indexOf||function(q){var p=0,o=this.length;for(;p<o;p++){if(p in this&&this[p]===q){return p}}return -1},n=function(o,p){this.name=o;this.code=DOMException[o];this.message=p},g=function(p,o){if(o===""){throw new n("SYNTAX_ERR","An invalid or illegal string was specified")}if(/\s/.test(o)){throw new n("INVALID_CHARACTER_ERR","String contains an invalid character")}return c.call(p,o)},d=function(s){var r=k.call(s.getAttribute("class")||""),q=r?r.split(/\s+/):[],p=0,o=q.length;for(;p<o;p++){this.push(q[p])}this._updateClassName=function(){s.setAttribute("class",this.toString())}},e=d[f]=[],i=function(){return new d(this)};n[f]=Error[f];e.item=function(o){return this[o]||null};e.contains=function(o){o+="";return g(this,o)!==-1};e.add=function(){var s=arguments,r=0,p=s.length,q,o=false;do{q=s[r]+"";if(g(this,q)===-1){this.push(q);o=true}}while(++r<p);if(o){this._updateClassName()}};e.remove=function(){var t=arguments,s=0,p=t.length,r,o=false,q;do{r=t[s]+"";q=g(this,r);while(q!==-1){this.splice(q,1);o=true;q=g(this,r)}}while(++s<p);if(o){this._updateClassName()}};e.toggle=function(p,q){p+="";var o=this.contains(p),r=o?q!==true&&"remove":q!==false&&"add";if(r){this[r](p)}if(q===true||q===false){return q}else{return !o}};e.toString=function(){return this.join(" ")};if(b.defineProperty){var l={get:i,enumerable:true,configurable:true};try{b.defineProperty(m,a,l)}catch(h){if(h.number===-2146823252){l.enumerable=false;b.defineProperty(m,a,l)}}}else{if(b[f].__defineGetter__){m.__defineGetter__(a,i)}}}(self))}else{(function(){var b=document.createElement("_");b.classList.add("c1","c2");if(!b.classList.contains("c2")){var c=function(e){var d=DOMTokenList.prototype[e];DOMTokenList.prototype[e]=function(h){var g,f=arguments.length;for(g=0;g<f;g++){h=arguments[g];d.call(this,h)}}};c("add");c("remove")}b.classList.toggle("c3",false);if(b.classList.contains("c3")){var a=DOMTokenList.prototype.toggle;DOMTokenList.prototype.toggle=function(d,e){if(1 in arguments&&!this.contains(d)===!e){return e}else{return a.call(this,d)}}}b=null}())}};
    })(),
    (function() {// matches polyfill
        Element && function(ElementPrototype) {
            ElementPrototype.matches = ElementPrototype.matches ||
            ElementPrototype.matchesSelector ||
            ElementPrototype.webkitMatchesSelector ||
            function(selector) {
                var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
                while (nodes[++i] && nodes[i] != node);
                return !!nodes[i];
            }
        }(Element.prototype);
        return Element.prototype.matches
    })(),
    function (selector, handler) {//addSelectorListener
        var uid = "js-live-",
            $style = document.createElement("style");


        function animationStart(e) {
            if(e.animationName == uid) {
                var element = e.target;
                element.classList.add(uid);//增加触发标志,去除animation占用
                handler(element, selector);
            }
        }

        //选择器有变动则触发animationstart,然后调用回调
        document.getElementsByTagName("head")[0].appendChild($style);
        for(var i=0,el=$style; el = el.previousElementSibling; i++) ;//利用style的位置做uid
        uid += i;
        $style.appendChild(document.createTextNode("@-webkit-keyframes "+uid+" {0%{}}@keyframes "+uid+" {0%{}}"+selector+":not(."+uid+"){-webkit-animation:"+uid+"!important;animation:"+uid+"!important}"));
        document.addEventListener("webkitAnimationStart", animationStart, false);
        document.addEventListener("animationstart", animationStart, false);
        //如果立即执行animationstart会重复动画
        

    },
    function(element) {//geRowRects
        var rects = [],
            clientRects = element.getClientRects(),
            len = clientRects.length,
            clientRect, top, rectsLen, rect, i;

        for(i=0; i<len; i++) {
            has = false;
            rectsLen = rects.length;
            clientRect = clientRects[i];
            top = clientRect.top;
            while(rectsLen--) {
                rect = rects[rectsLen];
                if (rect.top == top) {
                    has = true;
                    break;
                }
            }
            if(has) {
                rect.right = rect.right > clientRect.right ? rect.right : clientRect.right;
                rect.width = rect.right - rect.left;
            }
            else {
                rects.push({
                    top: clientRect.top,
                    right: clientRect.right,
                    bottom: clientRect.bottom,
                    left: clientRect.left,
                    width: clientRect.width,
                    height: clientRect.height
                });
            }
        }
        return rects;
    }
)

              
            
!
999px

Console