Insert ellipsis (…) into HTML tag if content too wide

后端 未结 24 2805
名媛妹妹
名媛妹妹 2020-11-22 10:07

I have a webpage with an elastic layout that changes its width if the browser window is resized.

In this layout there are headlines (h2) that will have

相关标签:
24条回答
  • 2020-11-22 10:50

    I've got a solution working in FF3, Safari and IE6+ with single and multiline text

    .ellipsis {
        white-space: nowrap;
        overflow: hidden;
    }
    
    .ellipsis.multiline {
        white-space: normal;
    }
    
    <div class="ellipsis" style="width: 100px; border: 1px solid black;">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>
    <div class="ellipsis multiline" style="width: 100px; height: 40px; border: 1px solid black; margin-bottom: 100px">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>
    
    <script type="text/javascript" src="/js/jquery.ellipsis.js"></script>
    <script type="text/javascript">
    $(".ellipsis").ellipsis();
    </script>
    

    jquery.ellipsis.js

    (function($) {
        $.fn.ellipsis = function()
        {
            return this.each(function()
            {
                var el = $(this);
    
                if(el.css("overflow") == "hidden")
                {
                    var text = el.html();
                    var multiline = el.hasClass('multiline');
                    var t = $(this.cloneNode(true))
                        .hide()
                        .css('position', 'absolute')
                        .css('overflow', 'visible')
                        .width(multiline ? el.width() : 'auto')
                        .height(multiline ? 'auto' : el.height())
                        ;
    
                    el.after(t);
    
                    function height() { return t.height() > el.height(); };
                    function width() { return t.width() > el.width(); };
    
                    var func = multiline ? height : width;
    
                    while (text.length > 0 && func())
                    {
                        text = text.substr(0, text.length - 1);
                        t.html(text + "...");
                    }
    
                    el.html(t.html());
                    t.remove();
                }
            });
        };
    })(jQuery);
    
    0 讨论(0)
  • 2020-11-22 10:52

    I was a bit surprised by the behavior of the css though.

    var cssEllipsis = 
    {   "width": "100%","display": "inline-block", 
    "vertical-align": "middle", "white-space": "nowrap", 
    "overflow": "hidden", "text-overflow": "ellipsis" 
    };
    

    Unless I provided the width to the control to which i needed to bind the ellipsis didn't suppost my cause. Is width a must property to be added ??? Please put your thoughts.

    0 讨论(0)
  • 2020-11-22 10:53

    Well, one simple solution, that doesn't quite add the "...", but does prevent the <h2> from breaking into two lines would be to add this bit of css:

    h2 {
        height:some_height_in_px; /* this is the height of the line */
        overflow:hidden; /* so that the second (or third, fourth, etc.)
                            line is not visible */
    }
    

    I gave it some more thought, and I came up with this solution, you have to wrap the textual contents of your h2 tag with another tag (e.g. a span) (or alternatively wrap the h2s with something that has the given height) and then you can use this sort of javascript to filter out the unneeded words:

    var elems = document.getElementById('conainter_of_h2s').
                         getElementsByTagName('h2');
    
        for ( var i = 0, l = elems.length; i < l; i++) {
            var span = elems.item(i).getElementsByTagName('span')[0];
            if ( span.offsetHeight > elems.item(i).offsetHeight ) {
                var text_arr = span.innerHTML.split(' ');
                for ( var j = text_arr.length - 1; j>0 ; j--) {
                    delete text_arr[j];
                    span.innerHTML = text_arr.join(' ') + '...';
                    if ( span.offsetHeight <= 
                                            elems.item(i).offsetHeight ){
                        break;
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 10:53

    Pure CSS Multi-line Ellipsis for text content:

    .container{
        position: relative;  /* Essential */
        background-color: #bbb;  /* Essential */
        padding: 20px; /* Arbritrary */
    }
    .text {
        overflow: hidden;  /* Essential */
        /*text-overflow: ellipsis; Not needed */
        line-height: 16px;  /* Essential */
        max-height: 48px; /* Multiples of line-height */
    }
    .ellipsis {
        position: absolute;/* Relies on relative container */
        bottom: 20px; /* Matches container padding */
        right: 20px; /* Matches container padding */
        height: 16px; /* Matches line height */
        width: 30px; /* Arbritrary */
        background-color: inherit; /* Essential...or specify a color */
        padding-left: 8px; /* Arbritrary */
    }
    <div class="container">
        <div class="text">
            Lorem ipsum dolor sit amet, consectetur eu in adipiscing elit. Aliquam consectetur venenatis blandit. Praesent vehicula, libero non pretium vulputate, lacus arcu facilisis lectus, sed feugiat tellus nulla eu dolor. Nulla porta bibendum lectus quis euismod. Aliquam volutpat ultricies porttitor. Cras risus nisi, accumsan vel cursus ut, sollicitudin vitae dolor. Fusce scelerisque eleifend lectus in bibendum. Suspendisse lacinia egestas felis a volutpat. Aliquam volutpat ultricies porttitor. Cras risus nisi, accumsan vel cursus ut, sollicitudin vitae dolor. Fusce scelerisque eleifend lectus in bibendum. Suspendisse lacinia egestas felis a volutpat.
        </div>
        <div class="ellipsis">...</div>
    </div>

    Please checkout the snippet for a live example.

    0 讨论(0)
  • 2020-11-22 10:58

    A more flexible jQuery plugin enabling you to keep a element after the ellipsis (for example a "read-more" button) and update onWindowResize. It also works around text with markup:

    http://dotdotdot.frebsite.nl

    0 讨论(0)
  • 2020-11-22 10:59

    Just in case y'all end up here in 2013 - here is a pure css approach I found here: http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

    .truncate {
      width: 250px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    

    It works well.

    0 讨论(0)
提交回复
热议问题