programing tip

Firefox 4의 text-overflow : ellipsis?

itbloger 2020. 8. 14. 07:30
반응형

Firefox 4의 text-overflow : ellipsis? (및 FF5)


text-overflow:ellipsis;CSS 속성은 마이크로 소프트가 웹에 적합한 일을했다는 몇 가지 중 하나 여야합니다.

이제 Firefox를 제외한 다른 모든 브라우저에서 지원합니다.

파이어 폭스 개발자들은 2005 년부터이 문제에 대해 논쟁을 벌여 왔지만 분명한 요구에도 불구하고 실제로 구현할 수없는 것 같습니다 (실험적인 -moz-구현으로도 충분할 것입니다).

몇 년 전 누군가가 Firefox 3를 해킹하여 생략 부호를 지원하도록 만드는 방법을 알아 냈습니다 . 해킹은이 -moz-binding기능을 사용하여 XUL을 사용하여 구현합니다. 현재 상당수의 사이트에서이 해킹을 사용하고 있습니다.

나쁜 소식? Firefox 4는 -moz-binding기능을 제거하고 있으며 , 이는이 해킹이 더 이상 작동하지 않음을 의미합니다.

따라서 Firefox 4가 출시 되 자마자 (이번 달 말에)이 기능을 지원하지 못하는 문제로 돌아갈 것입니다.

그래서 내 질문은 : 이것에 대해 다른 방법이 있습니까? (가능한 한 Javascript 솔루션으로 돌아 가지 않으려 고 노력하고 있습니다)

[편집]
많은 찬성 투표, 그래서 내가 알고 싶어하는 유일한 사람은 아니지만, 기본적으로 '자바 스크립트 사용'이라는 답이 하나 있습니다. 나는 여전히 JS가 전혀 필요하지 않거나 최악의 경우 CSS 기능이 작동하지 않는 대체 수단으로 만 사용하는 솔루션을 기대하고 있습니다. 그래서 저는 질문에 대한 현상금을 게시 할 것입니다. 누군가 어딘가에서 답을 찾았을 가능성이 있습니다.

[편집]
업데이트 : Firefox는 빠른 개발 모드로 들어 갔지만 FF5가 출시 되었음에도 불구하고이 기능은 여전히 ​​지원되지 않습니다. 그리고 대부분의 사용자가 FF3.6에서 업그레이드 했으므로 해킹은 더 이상 해결책이 아닙니다. 좋은 소식 은 파이어 폭스 6에 추가 될 수 있다는 말을 들었 습니다. 파이어 폭스 6은 새로운 출시 일정이 몇 달 안에 나올 것입니다. 그렇다면 기다릴 수있을 것 같지만 더 빨리 분류 할 수 없었던 것이 아쉽습니다.

[최종 편집]
줄임표 기능이 Firefox의 "Aurora Channel"(즉, 개발 버전)에 마침내 추가 된 것을 확인했습니다. 이것은 2011 년 말에 나올 파이어 폭스 7의 일부로 출시되어야한다는 것을 의미합니다.

릴리스 정보는 https://developer.mozilla.org/en-US/Firefox/Releases/7에서 확인할 수 있습니다.


Spudley, jQuery를 사용하여 작은 JavaScript를 작성하여 동일한 결과를 얻을 수 있습니다.

var limit = 50;
var ellipsis = "...";
if( $('#limitedWidthTextBox').val().length > limit) {
   // -4 to include the ellipsis size and also since it is an index
   var trimmedText = $('#limitedWidthTextBox').val().substring(0, limit - 4); 
   trimmedText += ellipsis;
   $('#limitedWidthTextBox').val(trimmedText);
}

모든 브라우저가 기본적으로 (자바 스크립트없이)이를 지원하는 방법이 있어야한다는 것을 이해합니다. 그러나 이것이 현재 우리가 가지고있는 것입니다.

편집 또한 css모든 고정 너비 필드에 클래스를 연결하여 더 깔끔하게 만들 수 fixWidth있으며 다음과 같이 할 수 있습니다.

$(document).ready(function() {
   $('.fixWidth').each(function() {
      var limit = 50;
      var ellipsis = "...";
      var text = $(this).val();
      if (text.length > limit) {
         // -4 to include the ellipsis size and also since it is an index
         var trimmedText = text.substring(0, limit - 4); 
         trimmedText += ellipsis;
         $(this).val(trimmedText);
      }
   });
});//EOF

2011 년 9 월 30 일 수정

FF7이 나왔고이 버그 가 해결되었으며 작동합니다!


2011 년 8 월 29 일 수정

이 문제는 해결 된 것으로 표시되며 FF 7에서 사용할 수 있습니다. 현재 2011 년 9 월 27 일에 출시 될 예정입니다.

달력을 표시하고 배치 한 모든 해킹을 제거 할 준비를하십시오.

낡은

내가 다른 대답을 : 대기 .

FF 개발팀은이 문제를 해결하기 위해 뜨거운 노력을 기울이고 있습니다.

Firefox 6에 대한 임시 수정 세트가 있습니다.

Firefox 6 !! 언제 나올까요?!?

상상의 과민 반응을 보이는 사람. Firefox는 빠른 개발 과정에 있습니다. FF6은 Firefox 5 이후 6 주에 출시 될 예정입니다. Firefox 5는 2011 년 6 월 21 일 출시 될 예정입니다.

2011 년 8 월 초에 수정이 이루어지기를 바랍니다.

원래 포스터 질문의 링크에서 버그를 따라 메일 링리스트에 가입 할 수 있습니다.

또는 여기클릭하십시오 . 어느 쪽이든 가장 쉬운 것입니다.


내 응용 프로그램에서 유일한 브라우저 특정 해킹이 FF4를 지원하는 것이 조금 실망 스럽다고 말해야합니다. 위의 자바 스크립트 솔루션은 가변 너비 글꼴을 고려하지 않습니다. 이를 설명하는보다 자세한 스크립트가 있습니다. 이 솔루션의 큰 문제는 코드가 실행될 때 텍스트를 포함하는 요소가 숨겨지면 상자의 너비를 알 수 없다는 것입니다. 이것은 나에게 거래 차단기 였기 때문에 작업 / 테스트를 중단했지만 누군가에게 유용 할 경우 여기에 게시 할 것이라고 생각했습니다. 내 테스트가 철저하지 않았으므로 잘 테스트하십시오. FF4에 대한 코드 만 실행하고 다른 모든 브라우저가 기존 솔루션을 사용하도록 브라우저 검사를 추가하려고했습니다.

여기에서 확인할 수 있습니다 : http://jsfiddle.net/kn9Qg/130/

HTML :

<div id="test">hello World</div>

CSS :

#test {
    margin-top: 20px;
    width: 68px;
    overflow: hidden;
    white-space: nowrap;
    border: 1px solid green;
}

Javascript (uses jQuery)

function ellipsify($c){
    // <div $c>                 content container (passed)
    //    <div $b>              bounds
    //       <div $o>           outer
    //          <span $i>       inner
    //       </div>
    //       <span $d></span>   dots
    //    </div>
    // </div>

    var $i = $('<span>' + $c.html() + '</span>');
    var $d = $('<span>...</span>');
    var $o = $('<div></div>');
    var $b = $('<div></div>');

    $b.css( {
        'white-space' : "nowrap",
        'display' : "block",
        'overflow': "hidden"
    }).attr('title', $c.html());

    $o.css({
        'overflow' : "hidden",
        'width' : "100%",
        'float' : "left"
    });

    $c.html('').append($b.append( $o.append($i)).append($d));

    function getWidth($w){
        return parseInt( $w.css('width').replace('px', '') );
    }

    if (getWidth($o) < getWidth($i))
    {
        while (getWidth($i) > (getWidth($b) - getWidth($d)) )
        {
             var content = $i.html();
             $i.html(content.substr(0, content.length - 1));
        }

        $o.css('width', (getWidth($b) - getWidth($d)) + 'px');
    }
    else
    {
        var content = $i.html();
        $c.empty().html(content);
    }
}

It would be called like:

$(function(){
    ellipsify($('#test'));
});

I have run into this gremlin over the past week as well.

Since the accepted solution does not account for variable width fonts and wwwhack's solution has a While Loop, I will throw in my $.02.

I was able to drastically reduce the processing time of my problem by using cross-multiplication. Basically, we have a formula that looks like this:

enter image description here

The variable x in this case is what we need to solve. When returned as an Integer, it will give the new length that the over-flowing text should be. I multiplied the MaxLength by 80% to give the ellipses enough room to show.

Here is a full html example:

<html>
    <head>
        <!-- CSS setting the width of the DIV elements for the table columns.  Assume that these widths could change.  -->
        <style type="text/css">
            .div1 { overflow: hidden; white-space: nowrap; width: 80px; }
            .div2 { overflow: hidden; white-space: nowrap; width: 150px; }
            .div3 { overflow: hidden; white-space: nowrap; width: 70px; }
        </style>
        <!-- Make a call to Google jQuery to run the javascript below.  
             NOTE:  jQuery is NOT necessary for the ellipses javascript to work; including jQuery to make this example work -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {  
                //Loop through each DIV element
                $('div').each(function(index) {
                    var myDiv = this;  //The original Div element which will have a nodeType of 1 (e.g. ELEMENT_NODE)
                    var divText = myDiv; //Variable used to obtain the text from the DIV element above  

                    //Get the nodeType of 3 (e.g. TEXT_NODE) from the DIV element.  For this example, it will always be the firstChild  
                    divText = divText.firstChild;  

                    //Create another variable to hold the display text  
                    var sDisplayText = divText.nodeValue;  

                    //Determine if the DIV element is longer than it's supposed to be.
                    if (myDiv.scrollWidth > myDiv.offsetWidth) {  
                        //Percentage Factor is just a way of determining how much text should be removed to append the ellipses  
                        //With variable width fonts, there's no magic number, but 80%, should give you enough room
                        var percentageFactor = .8;  

                        //This is where the magic happens.
                        var sliceFactor = ((myDiv.offsetWidth * percentageFactor) * sDisplayText.length) / myDiv.scrollWidth;
                        sliceFactor = parseInt(sliceFactor);  //Get the value as an Integer
                        sDisplayText = sDisplayText.slice(0, sliceFactor) + "..."; //Append the ellipses
                        divText.nodeValue = sDisplayText; //Set the nodeValue of the Display Text
                    }
                });
            });
        </script>
    </head>
    <body>
        <table border="0">
            <tr>    
                <td><div class="div1">Short Value</div></td>    
                <td><div class="div2">The quick brown fox jumps over the lazy dog; lots and lots of times</div></td>    
                <td><div class="div3">Prince</div></td> 
            </tr>
            <tr>    
                <td><div class="div1">Longer Value</div></td>   
                <td><div class="div2">For score and seven year ago</div></td>   
                <td><div class="div3">Brown, James</div></td>   
            </tr>
            <tr>    
                <td><div class="div1">Even Long Td and Div Value</div></td> 
                <td><div class="div2">Once upon a time</div></td>   
                <td><div class="div3">Schwarzenegger, Arnold</div></td> 
            </tr>
        </table>
    </body>
</html>

I understand this is a JS only fix, but until Mozilla fixes the bug, I'm just not smart enough to come up with a CSS solution.

This example works best for me because the JS is called every time a grid loads in our application. The column-width for each grid vary and we have no control over what type of computer our Firefox users view our app (which, of course, we shouldn't have that control :) ).


This pure CSS solution is really close, except for the fact that it causes ellipsis to appear after every line.

참고URL : https://stackoverflow.com/questions/4927257/text-overflowellipsis-in-firefox-4-and-ff5

반응형