gpt4 book ai didi

javascript - JavaScript 中的 CSS 溢出检测

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:28:19 24 4
gpt4 key购买 nike

为了显示一行文本(如在论坛中),如果文本溢出该行(不使用 CSS overflow 属性 chop ),则以“...”结束该行), 有多种方法。我仍在寻求最佳解决方案。例如,

在 CSS 中添加一个“...”作为背景图像是一种可能,但它会一直出现,这不是一个好的解决方案。

一些网站只计算字符数,如果超过 - 比如说 - 100,则 chop 字符串以仅保留 100(或 97)个字符并在末尾添加“...”。但字体通常不成比例,所以效果不佳。例如空间 - 以像素为单位 - 由

  • “AAA”和“iii”明显不同
  • “AAA”“iii” 通过比例字体具有相同的宽度

还有另一种方法可以获取字符串的精确大小(以像素为单位):

  • 在 Javascript 中创建一个 DIV
  • 在其中插入文本(例如通过 innerHTML)
  • 测量宽度(通过 .offsetWidth)

尚未实现。不过,请问会不会是浏览器兼容性问题?
有没有人尝试过这个解决方案?欢迎提出其他建议。

最佳答案

因此,使用一点 JS,我就能让它工作:http://jsfiddle.net/ug8Fc/3/

基本上,它使用隐藏在背景中的虚拟跨度来计算“内容”的宽度(这应该允许使用几乎任何类型的字体,因为跨度会相应地扩展)。然后,如果内容超出容器,它会为省略号腾出空间并不断删除字符,直到它适合为止。最后,重新添加修改后的内容和省略号。

如果有人精力充沛,也许你可以把它变成一个可以更容易地应用于任何选择器的 jquery 函数。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
table {
border: 1px solid #000;
border-collapse: collapse;
border-spacing: 0;
width: 800px;
}
table thead {
background-color: #AAA;
color: #333;
}
table tbody tr {
border: 1px solid #000;
}
table tbody td {
padding: 2px;
}
.from {
width: 100px;
}
.preview {
}
.date {
width: 90px;
}
</style>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
// Create a span we can use just to test the widths of strings
var spanTest = $('<span>').css('display','none').attr('id','span-test-tester');
$('body').append(spanTest);

// function to get the length of a string
function getLength(txt){
return spanTest.text(txt).width();
}

// now make all the previews fit
$('table tbody .preview').each(function(){
// build a bench-mark to gauge it from
var roomWidth = $(this).innerWidth();

// now, get the width and play with it until it fits (if it doesn't)
var txt = $(this).text();
var contentsWidth = getLength(txt);
if (contentsWidth > roomWidth){ // bigger than we have to work with
roomWidth -= getLength('...'); // work within confines of room + the ellipsis
while (contentsWidth > roomWidth){
txt = txt.substring(0,txt.length-1);
contentsWidth = getLength(txt);
}
// set the text to this
$(this).text(spanTest.text()).append($('<span>').text('...'));
}
});

});
//]]>
</script>
</head>
<body>
<table>
<thead>
<tr>
<th class="from">From</th>
<th class="preview">Subject</th>
<th class="date">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td class="from">Joe Smith</td>
<td class="preview">Hello bob, I just wanted to check in and see how things were going.
I know we spoke last week about etc. etc. etc.</td>
<td class="date">Dec 1, 2010</td>
</tr>
<tr>
<td class="from">John Doe</td>
<td class="preview">Hey bob, got any plans for new years yet?</td>
<td class="date">Dec 28, 2010</td>
</tr>
<tr>
<td class="from">Ben Franklin</td>
<td class="preview">Happy New Year! Hope you had a great time at the casinos (and didn't
spend too much money (haha)). Hope to see you this Memorial day blah blah blah
</tr>
</tbody>
</table>
</body>
</html>

关于javascript - JavaScript 中的 CSS 溢出检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4560083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com