当我的网站上的几个多行段落超过一定的高度时,我会尝试在它们后面加上一个省略号。为此,我使用了dotdotdot,jquery插件。
奇怪的是,当我刷新页面时,它不起作用。它只有在我调整窗口大小后才能工作(然后它才能完美地工作)。我已经试过把所有的脚本放在html的末尾,这样dotdotdot就会在最后加载,但仍然不能正常工作。有人知道为什么会发生这种情况吗?
我对dotdotdot使用了这些设置:
$(document).ready(function() {
$("p.article-content").dotdotdot(
{
/* The HTML to add as ellipsis. */
ellipsis : '...',
/* How to cut off the text/html: 'word'/'letter'/'children' */
wrap : 'word',
/* jQuery-selector for the element to keep and put after the ellipsis. */
after : null,
/* Whether to update the ellipsis: true/'window' */
watch : true,
/* Optionally set a max-height, if null, the height will be measured. */
height : null,
/* Deviation for the height-option. */
tolerance : 0,
/* Callback function that is fired after the ellipsis is added,
receives two parameters: isTruncated(boolean), orgContent(string). */
callback : function( isTruncated, orgContent ) {},
lastCharacter : {
/* Remove these characters from the end of the truncated text. */
remove : [ ' ', ',', ';', '.', '!', '?' ],
/* Don't add an ellipsis if this array contains
the last character of the truncated text. */
noEllipsis : []
}
});
});相关的HTML是(它很难看,我知道,我还在试验中):
<article class="article">
<div class="article-image"></div>
<h2>Title</h2>
<p class="date">December 19, 2012</p>
<p class="article-content">Lorem ipsum etc. (the actual content is larger)</p>
</article>和CSS:
article {
font-size: 99%;
width: 28%;
line-height: 1.5;
float: left;
margin-left: 8%;
margin-bottom: 3em;
text-align: justify;
}
article h2 {
font-size: 125%;
line-height: 0.5;
text-transform: uppercase;
font-weight: normal;
text-align: left;
color: rgba(0,0,0,0.65);
}
.date {
margin-top: 0.3em;
margin-bottom: 1em;
font-family: 'PT Sans';
color: rgba(0,0,0,0.5);
}
.article-image {
background-image: url(http://lorempixel.com/g/400/300/city/7);
width: 100%;
height: 13em;
overflow: hidden;
margin-bottom: 1.5em;
}
p.article-content {
font-family : 'PT Sans';
color : rgba(0,0,0,0.65);
margin-bottom : 0;
height : 7em;
overflow : hidden;
}发布于 2013-03-09 04:13:53
或者,简单地将整个函数包装在一个.resize()函数中。不是最优雅的解决方案,但它会起作用的:
$(document).ready(function() {
$(window).resize(function()
{
$("p.article-content").dotdotdot(
{
// All your code goes here
});
}).resize();
// The second .resize() will fire when the document is ready (i.e. onload),
// therefore executing .dotdotdot() upon loading
});编辑:根据凯文的建议,由于.dotdotdot()已经侦听了调整大小事件,因此您甚至不需要包装函数。只需使用$(window).resize()在文档准备就绪时触发事件。
发布于 2013-06-25 18:02:57
也有类似的问题。只需将点点初始化放在一个窗口加载程序中,而不是准备好dom。
发布于 2014-08-28 20:50:17
一开始,使用dotdotdot看起来很简单。但是我也遇到了这个问题,因为响应式页面包含了很多内容。文档准备好后,页面上的容器仍在调整大小,因为内容正在填充。在你的评论的帮助下,我现在的解决方案是:
$(document).ready(ellipsizeText); // Plugin must have been working flawles inhere as described in documentation.
window.setTimeout(ellipsizeText, 400); // Just for any case.
window.setTimeout(ellipsizeText, 800); // Maybe user didn't saw it flickering.
$(window).load(ellipsizeText); // Oh hell! the images are loading so not all containers are yet on their places. We wait on them..
function ellipsizeText()
{
$(".selectorForEllipsis").dotdotdot({
watch: true
});
}我认为正确的解决方案是,当每个容器的位置或大小发生变化时,将listener附加到每个包含文本的容器中,以更新点点,而不仅仅是调整窗口大小。也许可以使用Ben Alman yquery resize plugin
或者现有的插件可以更好地处理这个内容加载问题?
https://stackoverflow.com/questions/15302438
复制相似问题