多行文字超出 JS省略號...

一喵嗚發表於2015-06-20
(function($) {
     $(".first-button").on("click", function(event){
          $(".first-text").ellipsis(200);  
          $(this).hide();
     });
})(jQuery);


(function($) {
    
    $.fn.ellipsis = function(charCount) {
        return $(this).each(function() {
            var el = $(this);
           
            var originalText = el.html();
            var originalTextLength = originalText.length;
           
            var tempNode = $(this.cloneNode(true));
           
            var min = 0;
            var max = charCount;
           
            if(charCount < originalTextLength){
                 if (min<=max){
                      var tooltipHTML = null;
                      var trimLocation = (min + max);
                     var text = originalText.substr(0, trimLocation);
                     tempNode.html(text + "…");
                     tooltipHTML = '<span style="position:relative"><span class="ellipsis-tooltip-theme ellipsis-tooltip" style="margin-top:0.3%">'+originalText+'<span class="ellipsis-tooltip-nub"></span></span ></span >';
                     el.after(tooltipHTML);
                 }
                 if (min > max) {
                      //If the max value is greater than the min value, just raise a warning.
                      console.log('character count has to be greater than 0.');
                 }
            }
            else {
                 return false;
            }
            el.html(tempNode.html());
            tempNode.remove();
           
            el.mouseenter(function(){
                    el.parent().find('.ellipsis-tooltip').fadeIn(500);
               });
              
               el.mouseleave(function(){
                    el.parent().find('.ellipsis-tooltip').fadeOut(500);
               });
        });
    };
})(jQuery); 


相關文章