【问题标题】:angular directive jquery.dotdotdot, how to 'animate' truncation?角度指令 jquery.dotdotdot,如何“动画”截断?
【发布时间】:2014-02-07 22:05:03
【问题描述】:

我正在使用 angular 从 jquery.dotdotdot 插件中创建一个指令。我已经做到了,以便在文本之后出现“阅读更多”或“阅读更少”链接以切换截断。因为我将指令限制为属性,所以这可以用于许多项目,但在我的情况下,我在多行文本的范围内使用它。这种切换是通过 dotdotdot 选项中设置的回调发生的,如下所示。为了使它看起来更平滑并提供更多反馈,我试图减慢或动画截断和不截断之间的过渡。实际截断有效。

callback: ->
    $(element).find(".read-more").click (e) ->
        e.preventDefault()
        $(element).trigger "destroy.dot"
        $(element).append '<a href="" class="read-less">...Read less</a>'

        $(element).find(".read-less").click (e) ->
            e.preventDefault()
            $(element).find(".read-less").remove()
            truncate()

我尝试了在元素和元素的父元素(表中的 td)上使用 .css 方法的变体,更改了高度的“过渡”,但没有奏效。有更好的整体方法吗?如果没有,我目前的方法做错了什么?

【问题讨论】:

  • 对此进行动画处理会很困难,主要是因为截断是通过逐字更改文本来完成的。您必须通过克隆 div 来截断,将其放在原始的后面,截断克隆,然后淡出原始。我没有太多参与,无法在答案中制作演示。

标签: javascript jquery css angularjs truncation


【解决方案1】:

我遇到了同样的问题,并认为我会分享我的解决方案(常规 javascript 而不是 coffescript):

// truncates truncatedTextSelector using dotdotdot; sets triggerSelector to an animated toggle of truncation; after toggle, 
// updates triggerTextSelector within triggerSelector to hiddenText or shownText when truncated or fully shown, respectively
function toggleTextTruncation(truncatedTextSelector, triggerSelector, triggerTextSelector, hiddenText, shownText) {
    // store truncated and auto heights before enabling dotdotdot
    this.trunc_height = $(truncatedTextSelector).css('height');
    this.auto_height = $(truncatedTextSelector).css({height:'auto'}).css('height');
    // initialize to truncated
    $(truncatedTextSelector).css({height: this.trunc_height}).dotdotdot();
    $(triggerSelector).data('shown', false);
    // on trigger, toggle shown or hidden
    var obj = this;
    $(triggerSelector).click(function(){
        if ($(this).data('shown')) {
            // animate truncated height and apply dotdotdot on complete
            $(truncatedTextSelector).animate({height: obj.trunc_height}, {complete: function(){ $(this).dotdotdot() }});
            $(this).find(triggerTextSelector).html(hiddenText);
            $(this).data('shown', false);
        } else {
            // destroy dotdotdot and animate auto height
            $(truncatedTextSelector).trigger('destroy').css({overflow:'hidden'}).animate({height: obj.auto_height});
            $(this).find(triggerTextSelector).html(shownText);
            $(this).data('shown', true);
        }
    })

    // destroys functionality; must be used whenever involved elements are hidden and re-shown, since this breaks it
    this.selfDestruct = function() {
        $(triggerSelector).unbind('click');
        $(truncatedTextSelector).css({height: this.trunc_height}).trigger('destroy');
    }
}

【讨论】:

  • 你首先无条件设置data-shown属性,见$(truncatedTextSelector).css({height: this.trunc_height}).dotdotdot(); $(triggerSelector).data('shown', false);。在这样做之前,您应该实际检查文本是否被截断。 dotdotdot 提供了一个回调函数,该函数带有一个名为 isTruncated 的参数用于此目的。查看更多dotdotdot.frebsite.nl
猜你喜欢
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2013-11-25
  • 1970-01-01
相关资源
最近更新 更多