【问题标题】:dotdotdot.js only works after a window resizedotdotdot.js 仅在窗口调整大小后有效
【发布时间】:2013-03-08 20:09:34
【问题描述】:

当我的网站上的多个多行段落超过一定高度时,我正尝试在它们后面附加一个省略号。为此,我使用了 jquery 插件dotdotdot

奇怪的是当我刷新页面时它不起作用。它仅在我调整窗口大小后才有效(然后它完美地工作)。我已经尝试将所有脚本放在我的 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;
}

【问题讨论】:

  • 尝试使用窗口加载事件,以便它等到图像全部加载完毕。
  • 你能分享你的css吗?
  • @KevinB,你的意思是 $(window).ready 而不是 $(document).ready?我已经尝试过了,不幸的是没有任何区别..
  • @Samuel 不,我的意思是窗口加载。 $(window).load(function(){...})
  • @DanielA.White,当然!我已将其添加到问题中。

标签: javascript jquery html css jquery-plugins


【解决方案1】:

有类似的问题。只需要将 dotdotdot 初始化放在窗口加载事件处理程序中,而不是准备好 dom。

【讨论】:

  • 我在使用完全不同的 JS 库时遇到了类似的问题,这似乎可以解决问题。
【解决方案2】:

起初,dotdotdot 似乎很容易。但是我也遇到了包含大量内容的响应式页面的问题。文档准备好后,页面上的容器仍在调整大小,因为正在填充内容。 在您的 cmets 的帮助下,我现在的解决方案是:

$(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
    });
}

我认为正确的解决方案是在更改位置或大小以更新 dotdotdot 时,将侦听器附加到每个带有文本的容器,而不仅仅是调整窗口大小。也许与Ben Alman yquery resize plugin

或者是否存在更好地处理此内容加载问题的插件?

【讨论】:

  • 太棒了。太棒了
【解决方案3】:

或者,简单地将整个函数包装在 .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
});

[编辑]: 根据 Kevin 的建议,由于 .dotdotdot() 已经侦听了 resize 事件,因此您甚至不需要包装该函数。只需在文档准备好时触发事件,使用$(window).resize()

【讨论】:

  • 或者只是触发调整大小,因为 dotdotdot 已经监听了调整大小事件。
  • @KevinB 在这种情况下是的,因为watch : true。 dotdotdot 没有在他们的网站上提供更多示例/sn-ps 有点尴尬dotdotdot.frebsite.nl
【解决方案4】:

从 看:“窗口” 到 观看:真实

它开始为我工作了!

【讨论】:

    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2019-09-22
    • 1970-01-01
    • 2013-09-19
    • 2018-11-01
    • 2019-10-12
    相关资源
    最近更新 更多