Tips on utilizing jquery slideDown alongside appendTo

I am currently utilizing an ajax request to retrieve records from the database and then appending the data in HTML. However, I want the HTML to slide down once the data has been appended, but I'm unsure of how to achieve this.

Below is the ajax method I have implemented:

$.ajax({
    url: "/get_messages",
    type: "post",
    dataType: "json",
    data: {user_id: UserId},
    success: function(response) {
        if (response.success == true && response.messages.length > 1) {
            $("#user_" + UserId).html("");
            var Length = response.messages.length;
            //alert(Length);
            $.each([response.messages], function(index, value) {
                for (var i = 0; i < Length; i++) {
                    var NewMessage = '<div class="para-repeat inner"><h4 class="h4_"' + value[i]['user_id'] + '>' + value[i]['date'] + '</h4><h4 class="h4_"' + value[i]['user_id'] + '>' + value[i]['truck'] + '</h4><h4 class="h4_"' + value[i]['user_id'] + '>' + value[i]['name'] + '</h4><p class="h4_"' + value[i]['user_id'] + '>' + value[i]['content'] + '</p><a href="#new" class="mail-icon"></a></div>';
                    $("#user_" + UserId).append(NewMessage).show('slow');
                }
            });
            
        } else {
            //$("#One").show();
            $("#One").css("display", "block").delay(2000).slideUp("slow");
        }
    }, error: function(xhr, txt) {
        console.log("Something went wrong ", xhr.status);
    }
});

Here is a snippet of my HTML structure:

        <% @new_users.each do |message| %>
                <div class="para-repeat outer" id="user_<%= message[:user_id]%>">
                    <h4><%= message[:date] %></h4>
                    <h4><%= message[:truck] %></h4>
                    <h4><%= message[:name] %></h4>
                    <p><%= message[:content] %></p>
                    <a href="#new" class="mail-icon"></a>
                </div>
                <a id="<%= message[:user_id]%>" href="javascript:void(0);" onclick="getMessages(this.id)" class="expand">Expand All</a>
        <% end %>

Answer №1

Give this a shot for a smooth sliding effect

$('body,html').animate({'scrollTop':$("#user_"+UserId).offset().top)});

This method could be beneficial...

If you want to slide up all the messages on the page

$('.para-repeat:first',$("#user_"+UserId)).siblings().slideUp().end().slideDown();

Answer №2

If you want to add a scroll animation to your content, you can follow these steps:

Firstly,

$("#user_"+UserId).append(NewMessage)

Then insert the following code:

var container = $('body');
var scrollTo = $("#user_"+UserId);
container.animate({
    scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop()
});​

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

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

Is fetch a viable alternative to AJAX?

Is it possible to achieve the same functionalities in Fetch as you can with traditional AJAX? I am currently facing an issue with a basic login authentication setup using Express. My goal is to provide feedback to the user without refreshing the page - dis ...

"Troubleshooting: jQuery Find function not functioning correctly with HTML template

I am having trouble with a Shopify liquid code that I am trying to load into an HTML template <script type="text/template" id="description"> <div class="product-ddd"> {{ product.description }} </div> ...

What is the process for selecting a radio button using Selenium WebDriver?

I am trying to select the radio button in this HTML code, but I'm having trouble. Here is the snippet of my HTML: <div class="appendContent"> <div> id="contentContainer" class="grid_list_template"> <div id="routeMonitor ...

The malfunction of the AJAX toolkit SliderExtender is triggered by the ASP.NET timer

In my current setup, I have implemented 2 update panels with UpdateMode set to "Conditional". The first update panel consists of: 2 AJAX toolkit SliderExtender components 2 corresponding Text Boxes Within the first update panel, I have configured the T ...

What is the best way to incorporate a token when making an axios ajax request?

Currently in my project I am utilizing Laravel 5.3 along with vue.js 2.0. For my ajax requests, I am using axios (https://github.com/mzabriskie/axios). In order to set the TOKEN, I have followed the documentation provided: <script> axios.defa ...

Ways to locate two div class elements that are generated dynamically

I am looking to dynamically create 2 divs in different locations. One for displaying information and another for editing information. I want to be able to delete both divs with the same class when using the delete button. Since they are located in differe ...

What could be the reason my black overlay doesn't show up when the button is clicked?

Attempting to craft a pop-up window on my own, I encountered an issue. Upon pressing the button, instead of the anticipated pop-up appearing and darkening the background below it, the entire page freezes with no sign of the pop-up box. If I eliminate the ...

An issue has been identified with the functionality of an Ajax request within a partial view that is loaded through another Ajax request specifically in

Here is the current scenario within my ASP.NET MVC application: The parent page consists of 3 tabs, and the following javascript code has been implemented to handle the click events for each tab: Each function triggers a controller action (specified in t ...

What may be causing my Ajax call to get stuck at readystate 1 and not progress further

I've double-checked all my code, yet I'm not getting any response from my Ajax call. Can someone help me figure out what's wrong? function fetchPokemonData() { const apiRequest = new XMLHttpRequest(); apiRequest.open('GET', &apo ...

When an error message is displayed in the textField, the Material UI button shifts downward

I have incorporated the material UI error message display into my text field. When I click on the button without entering any text in the field, an error message pops up which pushes down the button. I am looking for a way to prevent this behavior but have ...

Utilize open-source tools for website design

Can anyone suggest an open source toolset for WYSIWYG HTML/CSS design? What tools have you found most helpful in your projects? ...

Is it possible to dynamically adjust items in jquery?

Here is some jQuery code that I am working with: $(function() { $('div#slideshow').append('<img src="images/forsiden/grans_julebrus.jpg" /><img src="images/forsiden/cappelen_hippo.jpg" /><img src="images/forsiden/capplen_gran ...

Transmitting information via Ajax, jquery, Node.js, and Express

Seeking assistance as I struggle to comprehend the process while trying to implement it based on various online resources. My carousel directs users right after signing up, and I aim to gather information about their profile through simple input questions. ...

Achieving a horizontal alignment of these four div elements containing images

I need some assistance with aligning the divs using the .acontainer class. It was working perfectly before adding the images, but now it's all messed up. I've tried various adjustments and searches online, but I'm still stuck. Thank you for ...

What's causing the text not to wrap when using float?

I've tried setting float:left for the image and float:right for the text, but the image is still overlapping the text. What I really want is for the text to wrap around the image - to be on the right side of the image at the beginning and below the i ...

What is the best way to implement a dropdown menu with JavaScript or jQuery?

I'm looking to create a dynamic dropdown list using Javascript or JQuery that mirrors the functionality of selecting countries, states, and cities. Can someone provide guidance on the best code to achieve this? Below is a snippet of my existing code: ...

Drop delete method not functioning as expected

Currently, I am trying to work on a JavaScript delete function that is supposed to delete a row from an SQL table. However, it seems like the function is not working properly. Let me share the code with you: $(document).ready(function(){ $( "#draggabl ...

Using AJAX to submit a PHP form without refreshing the page

Currently, I am facing an issue with my PHP and AJAX code for posting data without redirecting the page. Surprisingly, the script works perfectly on the login page but not on other pages. The main difference I observed is that the login page uses if (empty ...

Navigate to the element by clicking on the link, then apply a class to that specific element

I'm currently working with this HTML code: <div id="main"> <p>some text</p> <a id="goto-notes" href="#">see notes</a> <p>some text...</p> <div id="notes"> <p>notes here< ...