简体   繁体   English

角度指令jquery.dotdotdot,如何“动画化”截断?

[英]angular directive jquery.dotdotdot, how to 'animate' truncation?

i'm using angular to make a directive out of the jquery.dotdotdot plugin. 我正在使用angular从jquery.dotdotdot插件中做出指令。 i've made it so that a "read more" or "read less" link is after the text to toggle truncation. 我已经做到了,以便在文本后添加“阅读更多”或“阅读更少”链接来切换截断。 because i have the directive restricted to attribute, this could be used on a number of items but in my case i'm using it on a span of multiline text. 因为我的指令仅限于属性,所以可以在许多项目上使用它,但就我而言,我在多行文本范围内使用它。 this toggling happens via a callback set in the dotdotdot options as seen below. 这种切换是通过在dotdotdot选项中设置的回调进行的,如下所示。 to make it look smoother and provide more feedback, i'm trying to slow down or animate the transition between truncated and not. 为了使它看起来更平滑并提供更多反馈,我正在尝试减慢或动画化截断与否之间的过渡。 the actual truncation works. 实际的截断工作。

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()

i tried variations of using the .css method on the element and the element's parent (a td in a table), changing the 'transition' for height but that didn't work. 我尝试了在元素和元素的父元素(表中的td)上使用.css方法的各种方法,更改了高度的“转换”,但这没有用。 is there a better overall approach? 有没有更好的整体方法? if not, what am i doing wrong with my current approach? 如果不是,那么我目前的方法在做什么?

I ran into the same problem and thought I'd share my solution (regular javascript instead of coffescript): 我遇到了同样的问题,以为我会分享我的解决方案(常规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');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM