How can you achieve smooth truncation animation with the angular directive jquery.dotdotdot?

Incorporating the jQuery.dotdotdot plugin into an Angular directive has been a challenging but rewarding experience. By adding a "read more" or "read less" link after text, I've created a toggle truncation feature within the directive. Restricting the directive to attribute allows for versatile use across different items. In my case, I am applying it to a span containing multiline text. The toggling functionality is achieved through a callback defined in the dotdotdot options below. I am now focusing on making the transition between truncated and full text smoother by incorporating animations.

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 have experimented with using the .css method on both the element and its parent (in this case, a td within a table) to adjust the 'transition' property for height without success. Is there a more effective approach to achieving a smooth transition? If not, what adjustments should I make to improve my current method?

Answer №1

After encountering a similar issue, I came up with my own solution using plain JavaScript instead of coffeescript:

// This function toggles text truncation using dotdotdot; it sets the triggerSelector to an animated toggle for truncation,
// and updates triggerTextSelector within triggerSelector to hiddenText or shownText when the text is truncated or fully shown, respectively.
function toggleTruncatedText(truncatedTextSelector, triggerSelector, triggerTextSelector, hiddenText, shownText) {
    // Store the heights before enabling dotdotdot
    this.trunc_height = $(truncatedTextSelector).css('height');
    this.auto_height = $(truncatedTextSelector).css({height:'auto'}).css('height');
    
    // Initialize to truncated state
    $(truncatedTextSelector).css({height: this.trunc_height}).dotdotdot();
    $(triggerSelector).data('shown', false);
    
    // Toggle functionality on trigger click
    var self = this;
    $(triggerSelector).click(function(){
        if ($(this).data('shown')) {
            // Animate back to truncated height and apply dotdotdot once done
            $(truncatedTextSelector).animate({height: self.trunc_height}, {complete: function(){ $(this).dotdotdot() }});
            $(this).find(triggerTextSelector).html(hiddenText);
            $(this).data('shown', false);
        } else {
            // Remove dotdotdot and animate to auto height
            $(truncatedTextSelector).trigger('destroy').css({overflow:'hidden'}).animate({height: self.auto_height});
            $(this).find(triggerTextSelector).html(shownText);
            $(this).data('shown', true);
        }
    })

    // Function to clean up when elements are hidden and re-shown
    this.cleanup = function() {
        $(triggerSelector).unbind('click');
        $(truncatedTextSelector).css({height: self.trunc_height}).trigger('destroy');
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Featuring a visual arrangement of categorized images with AngularJS for an interactive display

As I work on developing a web application with Angular, my goal is to create a grid layout that organizes items by category. Each row will represent a different category. Below is an example of the code structure I have in place: <ion-item ng-repeat="i ...

What is the process for using populate with a custom ObjectId that is of type String?

My current setup involves using a firebase project for authentication and saving additional user information in MongoDB. The challenge comes in when assigning the UID of the firebase user to the _id field of the user model in MongoDB. To make this possible ...

verified that all form checkboxes were selected in angularJS

Hey there, I have a form in AngularJS with multiple checkboxes. When the first checkbox (Check All) is clicked, I want all the checkboxes to be checked. How can I achieve this using Angular for each field loop? Below is a snippet of my code: vm.asas = f ...

Dropdown menu is misaligned on the screen

I am currently working on enhancing my navigation bar with a dropdown menu using Bootstrap. However, I have encountered an issue where one of the items in the menu is not displayed properly. If you would like to see the full HTML and CSS code, you can vie ...

Utilizing AngularJS/AJAX/JavaScript to Invoke PHP

I am completely new to the world of PHP and AJAX, so I would greatly appreciate any guidance on whether this approach is even suitable. Essentially, my goal is to populate a .JSON file with data from a Microsoft SQL Server. I have created a PHP file to es ...

Utilizing effective CSS media queries within an Angular application

According to what I've read, it is not recommended in Angular to use the CSS hidden element to hide an element like this: .container{ background-color : powderblue; height : 50px; width : 100% } @media (max-width: 400px){ .container{ ...

Modifying the default class styles from Bootstrap based on the HTML displayed in Devtools

I am attempting to customize the styles of my application using bootstrap. Upon inspecting the HTML in Chrome Devtools, I found the following rendered code: <div data-v-256a5f3d="" data-v-7bf4ea52="" class="row bg-gray-200 bord ...

Place the "Close" button outside of the div container

I am facing an issue with the login button protruding slightly outside the border of a div. Despite trying to adjust the code to align it correctly, the issue persists. .closeWindow{ float: right; margin-left: 15px; /* half the width of the close b ...

Ensure that you select the checkbox if it is visible; if it is not, then you should

I am currently working on a page script and I only require a simple JavaScript solution. When a specific text, for example - no found, appears on the page, the script should automatically click the find work button. This action needs to be triggered at 1 ...

Hosting both a deployd backend and an angular.js frontend on a single server

I'm attempting to host a deployd API on the same server as my AngularJS application, but I'm encountering routing conflicts between the two. The deployd server is set to listen on port 5000 and here is how it's configured: var deployd = re ...

How can I use AJAX to remove {{$key}} in Laravel?

In our codebase, we are utilizing multiple hidden inputs within a loop structured like @foreach. <form action="{{ route('parties.updateAndCreate', auth()->id()) }}" method="post"> @csrf @method('PUT' ...

Exciting new functionality: JavaScript track currently playing

I currently have a continuous audio track playing music on my website. I am interested in adding a feature that displays text like this: "Now playing: (1st song name)" and then changes after a certain number of seconds "Now playing: (2nd song name)" Ev ...

Unable to locate the Chart object within the chartjs-plugin-labels.js file

Hello there, I am currently working on an Angular project where I want to incorporate a chart plugin. To achieve this, I executed the following commands: npm install angular2-chartjs npm install chartjs-plugin-labels Following that, I imported it into my ...

The input box refuses to accept any typed characters

I encountered a strange issue where the input box in the HTML was not allowing me to type anything. const para = document.createElement('p') const innerCard = document.getElementsByClassName('attach') for(let i = 0; i < innerCard.l ...

Minifying Angular mdDialog controller code results in failure

I have successfully implemented a custom template called mdDialog to display a dialog box for entering a name and selecting a boolean value from a checkbox. The dialog pops up when an element is clicked. Everything works perfectly in development, but I en ...

Discovering mouse clicks outside of the region in angularjs

Is there a way to reset an angular controller whenever the user clicks outside of the controller's scope? How can this be achieved? Here is a sample HTML structure: <div id='parent'> <div id='1' ng-controller="ctrl1" ...

retrieve the php variable value and display it in an input field using javascript

I am looking to combine the variable $variable with the input value using JavaScript $variable= file_get_contents('./env.txt'); <select id="customer_id" name="customer_id" w300 required pj-chosen" data-msg-required=&q ...

In Angular, the scrollIntoView method does not activate the mousewheel or scroll events

I am working with Angular and have registered both the mousewheel and scroll events. However, when I use scrollIntoView, these events do not seem to trigger. Are there any other events that I may be overlooking? @HostListener('mousewheel', [&ap ...

Displaying a dropdown menu from the top by utilizing the show() function

I've successfully implemented a dropdown menu using cssmenumaker. It works perfectly on desktop and mobile view. However, I'm facing an issue on mobile where the dropdown menu slides in from the left when clicked. I would prefer it to slide down ...

Utilizing an array as a child component in conditional rendering with vue.js

I have a Laravel API and the front end is built with Vue.js + Laravel. I am fetching data from the API and passing it to the view. Now, I want to implement conditional rendering based on certain criteria. Here's what my array looks like: "data" =&g ...