内容溢出用(...)表示的数种方法
1.常规css方法——可以实现IE,Safari,chrome,opera浏览器下文字溢出省略号表示
- .text_overflow_1{
- width:27em; white-space:nowrap;
- text-overflow:ellipsis;
- -o-text-overflow:ellipsis;
- overflow:hidden;
- }
2.使用ellipsis.xml文件使Firefox支持文字溢出后点点点省略号表示
- .text_overflow_2{
- width:27em; white-space:nowrap;
- text-overflow:ellipsis;
- -o-text-overflow:ellipsis;
- -moz-binding:url('../xml/ellipsis.xml#ellipsis');
- overflow:hidden;
- }
ps: 在Firefox下上面div文字溢出省略号显示,此div内容是“这段代码调用了同文件夹路径的ellipsis.xml文件,看Firefox浏览器下是够文字溢出省略号表示”。至此,几乎所有主流浏览器都实现文字溢出省略号表示。
- .text_overflow_3{
- width:27em;
- white-space:nowrap;
- text-overflow:ellipsis;
- -o-text-overflow:ellipsis;
- -moz-binding:url('ellipsis.xml#ellipsis');
- overflow:hidden;
- }
这里有个小结论:这个使Firefox浏览器下文字溢出省略号表示的ellipsis.xml文件要在页面的同一目录下,或是下一级目录下(我已测试),要是向上跨文件夹访问或绝对路径访问则失效。原因不详!
提供ellipsis.xml的源码(当然你也可以点击这里进行下载[使用 右键 另存为 的方式]):
- <?xml version="1.0" encoding="UTF-8"?>
- <bindings
- xmlns="http://www.mozilla.org/xbl"
- xmlns:xbl="http://www.mozilla.org/xbl"
- xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
- >
- <binding id="ellipsis">
- <content>
- <xul:description crop="end" xbl:inherits="value=xbl:text"><children/></xul:description>
- </content>
- </binding>
- </bindings>
虽然Firefox通过XUL的方式实现了ellipsis,但是还是需要注意以下这些问题:
1.经过XUL处理过的文本你将不能被选中,按理说-moz-user-select: text; 属性将允许文本被选中,但是我没有试验成功。
2.如果你给父元素绑定了XUL,那么子元素的内容将变得不可见。
XUL是英文“XML User Interface Language”的首字母缩写。它是为了支持Mozilla系列的应用程序(如Mozilla Firefox和Mozilla Thunderbird)而开发的使用者界面标示语言。顾名思义,它是一种应用XML来描述使用者界面的标示语言。(百度百科)
3.使用:after伪类和content实现文字溢出后省略号表示
这个方法流传已久,经过多次测试都不能完全兼容ie6/7/8/ff/Safari,chrome,opera;在Safari,chrome这两个浏览器下表现为省略号重复出现一次(即为6个),其他浏览器下为正常的3个,这里只要稍作改动即可兼容以上各浏览器,把text-overflow:ellipsis; 改为_text-overflow:ellipsis; 即可。用此方法多了一个空的标签。
- css部分:
- <style>
- .box{
- width:308px; float:left; font-size:14px;
- white-space:nowrap;
- _text-overflow:ellipsis;
- overflow:hidden;
- }
- .dotted{
- float:left;
- }
- .dotted:after{
- content:"…";
- }
- </style>
4.margin负值定位法
- HTML部分:<div class="text_overflow_4">
- <div class="text_con">这是一段比较长的文字,用来测试是否文字溢出时会用省略号显示。</div>
- <div class="text_dotted">…</div>
- </div>
- CSS部分:
- .text_overflow_4{
- width:24em;
- height:1.3em;
- overflow:hidden;
- zoom:1;
- }
- .text_overflow_4 .text_con{
- float:left;
- height:1.3em;
- margin-right:3em;
- overflow:hidden;
- }
- .text_overflow_4 .text_dotted{
- width:3em;
- height:1.31em;
- float:right;
- margin-top:-1.3em;
- }
简要说明:
此方法兼容IE6,IE7(IE8未测过),Firefox,chrome,Safari,opera浏览器。没有hack,没有生僻的css样式。文字短时,没有省略号,文字溢出时就出现省略号。可以说是相当不错的一个方法。原理也很简单:当文字内容足够长时就把隐藏在上面的省略号层给挤下来了。关键就是点点点所在div层的高度的绝对值要比其margin的绝对值大那么一点点。
5.jQuery限制字符字数的方法
- HTML部分:
- <div class="text_overflow_5">你个杀千刀的,怎么写了这么多的文字,我要被拦腰截断了啊,天啊!</div>
- 没有css啦,只有js代码:
- $(document).ready(function(){
- //限制字符个数
- $(".text_overflow_5").each(function(){
- var maxwidth=23;
- if($(this).text().length>maxwidth){
- $(this).text($(this).text().substring(0,maxwidth));
- $(this).html($(this).html()+'…');
- }
- });
- });
6.jQuery自动判断宽度是否超出的方法
- HTML部分:
- <div class="text_overflow_6">你个杀千刀的,怎么写了这么多的文字,我要被拦腰截断了啊,kitty救我!</div>
- css部分:
- .text_overflow_6{width:400px;}
- js部分:
- var wordLimit=function(){
- $(".text_overflow_6").each(function(){
- var copyThis = $(this.cloneNode(true)).hide().css({
- 'position': 'absolute',
- 'width': 'auto',
- 'overflow': 'visible'
- });
- $(this).after(copyThis);
- if(copyThis.width()>$(this).width()){
- $(this).text($(this).text().substring(0,$(this).html().length-4));
- $(this).html($(this).html()+'…');
- copyThis.remove();
- wordLimit();
- }else{
- copyThis.remove(); //清除复制
- return;
- }
- });
- }wordLimit();
前者直接限定字符个数,后者通过宽度判断,去掉最后一个字符,循环,直到文字内容宽度小于div的限制宽度。
7.jQuery.ellipsis插件
插件的源码如下(同样的,你可以点击这里下载[同样是 右键 另存为]):
- (function($) {
- $.fn.ellipsis = function(enableUpdating){
- var s = document.documentElement.style;
- if (!('textOverflow' in s || 'OTextOverflow' in s)) {
- return this.each(function(){
- var el = $(this);
- if(el.css("overflow") == "hidden"){
- var originalText = el.html();
- var w = el.width();
- var t = $(this.cloneNode(true)).hide().css({
- 'position': 'absolute',
- 'width': 'auto',
- 'overflow': 'visible',
- 'max-width': 'inherit'
- });
- el.after(t);
- var text = originalText;
- while(text.length > 0 && t.width() > el.width()){
- text = text.substr(0, text.length - 1);
- t.html(text + "…");
- }
- el.html(t.html());
- t.remove();
- if(enableUpdating == true){
- var oldW = el.width();
- setInterval(function(){
- if(el.width() != oldW){
- oldW = el.width();
- el.html(originalText);
- el.ellipsis();
- }
- }, 200);
- }
- }
- });
- } else return this;
- };
- })(jQuery);
这段js的原理很简单,就是通过不断的比较宽度值,然后逐个缩短字符宽度,当最后宽度合适的时候,停止循环,就实现了文字溢出显示…的效果。
这段js还需要一段css来配合。
- .overflow {
- text-overflow: ellipsis;
- -o-text-overflow: ellipsis;
- white-space: nowrap;
- overflow: hidden;
- }
js里有个判断就是当样式里出现text-overflow或者-o-text-overflow的时候,便不会执行这段js。因为支持这两个属性的浏览器可以自己实现ellipsis效果。


0 条留言
我要留言