0

i have this idea for fadeout last limit characters of any text strings.

example :

enter image description here

Is it possible? Does anyone know, how to create this using jquery/CSS3?

BBKing
  • 2,279
  • 8
  • 38
  • 44

4 Answers4

2

You can add text-overflow: ellipsis to your element and append a <span> with transparent gradient background via jQuery - DEMO

p {
    text-overflow: ellipsis;
    width: 140px;
    overflow: hidden;
    white-space: nowrap;
    margin: 25px 0;
} 

span {
    display: inline-block;
    height: 20px; 
    width: 50px;
    background: red;
    position: absolute;
    left: 85px;

    background: -moz-linear-gradient(right,  rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, right top, left top, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left, rgba(255,255,255,0),rgba(255,255,255,1)); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left,  rgba(255,255,255,1) 100%,rgba(255,255,255,0) 0%); /* Opera 11.10+ */
    background: -ms-linear-gradient(left,  rgba(255,255,255,1) 100%,rgba(255,255,255,0) 0%); /* IE10+ */
    background: linear-gradient(to right,  rgba(255,255,255,1) 100%,rgba(255,255,255,0) 0%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 */
}
​
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
2

I strongly recommend using CSS text-overflow:ellipsis for this -- it's the standard way of doing this kind of thing.

Back in the day when Firefox was still at version 3.6, it didn't support ellipsis, and I had to come up with an alternative solution. My solution at the time was exactly what you're suggesting here, a fade-out at the end of the line.

I achieved it by placing an additional element on top of the element involved, which had a gradient background that fades from transparent to solid colour, to match the background of the text. This gradient could be achieved using CSS, but since I needed to support old versions of IE (and since CSS3Pie didn't exist at the time), I actually used a PNG image instead for the gradient effect. Same difference either way, though.

The plus side of this was that it worked, and gave us a good looking fade out effect for the text.

The down sides were (1) it caused problems for users with selecting the text, (2) it looks terrible if your background isn't a simple solid colour, and (3) it added extra work, when the ellipsis solution was so much simpler.

I'm afraid I can't show you any code, because we got rid of it as soon as most Firefox users were upgraded to a version that supported ellipsis.

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

You can try CSS3 text-overflow property:

.test {
    text-overflow: ellipsis;
} 
Ram
  • 143,282
  • 16
  • 168
  • 197
  • This doesn't seem to be included in CSS3 specification. [Just a "module" so far](http://dev.w3.org/csswg/css3-ui/#text-overflow). – VisioN Aug 31 '12 at 20:32
  • [It's commonly supported in all major browsers](http://caniuse.com/#feat=text-overflow), however, this doesn't answer the original question – MrOBrian Aug 31 '12 at 20:35
  • 1
    ellipsis is supported in all browsers, and has been for ages. The only browser you'll have trouble with is old versions of firefox (eg 3.6). See http://stackoverflow.com/questions/4927257/text-overflowellipsis-in-firefox-4-and-ff5 for more info. – Spudley Aug 31 '12 at 20:43
  • the image in the question shows what the expected result is. Not to fade out the text and fade in the ellipsis, but to have the end of the text be a gradient fade (not animated) – MrOBrian Aug 31 '12 at 20:45
  • I also don't see _ellipsis_ in the question, but the sample image very clearly shows the text getting lighter toward the end. – MrOBrian Aug 31 '12 at 21:04
1

I cannot think of a better way:

<span>test test test test</span><span style="opacity: 0.50; "> fading.</span><span style="opacity: 0.40; ">.</span><span style="opacity: 0.20; ">.</span><span style="opacity: 0.10; ">.</span>​​​​​​​​​​

jsFiddle: http://jsfiddle.net/DLbBZ/

This is a jQuery workaround, although you already have the answer :P

http://jsfiddle.net/sXNQF/

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75