Ways to move the scroll to the bottom of the div by clicking

Can anyone help with why my scroll code isn't working? I'm trying to make the scroll go to the bottom when I click on $('#openDiv').

For example, when you enter a chat room on stackoverflow, the scroll is automatically at the bottom, and that's what I want.

I've also attempted putting the scroll code outside of the click function, but it's still not functioning...

$(document).ready(function() {

  $('#openDiv').on('click', function(){
    $('#openDiv').hide();
    $('#closeDiv').show();
    
    ///////// NOT WORKING /////////
    $("#mainDiv").animate({
      scrollTop: $('#mainDiv').prop("scrollHeight")
    }, 1000);
    ///////// NOT WORKING /////////
    
    $("#mainDiv").show();
  });
  
  $('#closeDiv').on('click', function(){
    $('#closeDiv').hide();
    $("#mainDiv").hide();
    $('#openDiv').show();
  });
  
});
#mainDiv {
  height: 150px;
  background-color: #2A2A2A;
  overflow-y: auto;
  overflow-x: hidden;
  display: none;
}

#divText {
  color: #fff;
  background-color: #555555;
  height: 20px;
}
#divTextEnd{
  color: #fff;
  background-color: red;
  height: 20px;
}

#openDiv{
  background-color: green;
  border: none;
  color: #fff;
}

#closeDiv{
  display: none;
  background-color: red;
  border: none;
  color: #fff;
}
<button id="openDiv">Open div</button>
<button id="closeDiv">Close div</button>

<div id="mainDiv">

  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divTextEnd">
    <h5>text</h5>
  </div>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

Answer №1

When you make the call

$('#mainDiv').prop("scrollHeight")
, keep in mind that #mainDiv is currently hidden with display: none;. In this state, the element will have a scrollHeight value of 0.

To successfully scroll #mainDiv, first ensure it is visible:

$(document).ready(function() {

  $('#openDiv').on('click', function() {
    $('#openDiv').hide();
    $('#closeDiv').show();

    $("#mainDiv").show();

    $("#mainDiv").animate({
      scrollTop: $('#mainDiv').prop("scrollHeight")
    }, 1000);
  });

  $('#closeDiv').on('click', function() {
    $('#closeDiv').hide();
    $("#mainDiv").hide();
    $('#openDiv').show();
  });

});
#mainDiv {
  height: 150px;
  background-color: #2A2A2A;
  overflow-y: auto;
  overflow-x: hidden;
  display: none;
}

#divText {
  color: #fff;
  background-color: #555555;
  height: 20px;
}

#divTextEnd {
  color: #fff;
  background-color: red;
  height: 20px;
}

#openDiv {
  background-color: green;
  border: none;
  color: #fff;
}

#closeDiv {
  display: none;
  background-color: red;
  border: none;
  color: #fff;
}
<button id="openDiv">Open div</button>
<button id="closeDiv">Close div</button>

<div id="mainDiv">

  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divText">
    <h5>text</h5>
  </div>
  <div id="divTextEnd">
    <h5>text</h5>
  </div>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

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

Difficulty achieving gradient effect with partial background color change in table-cell

In order to achieve a unique look for this particular design, I am seeking ways to alter the red background color of each cell in varying degrees - such as 100% for the first cell, 100% for the second cell, and 50% for the third cell. Update: After experi ...

Move the components over to the right

How can I vertically align the user and the search bar on the right side of the page? #search { float: right; margin-top: 9px; width: 250px; } .search { width: 230px; height: 30px; position: relative; line-height: 22px; } ...

Assistance with MVC WebAPI Layout

Can anyone offer guidance on how to customize the layout of automatically generated WebAPI help pages? I am looking to replace @Html.DisplayFor(m => m.SampleResponses, "Samples") with Jquery UI tabs, but I'm having trouble getting it to function prop ...

Using Wordpress's built-in feature for displaying images in the admin panel can enhance the user experience and make

I'm currently in the process of developing a unique Wordpress plugin for one of my customers and I really want to incorporate a modal popup feature on the admin page. After experimenting with ThickBox, LightBox, and Fancybox, none of them seem to do t ...

Styling drop caps using CSS on Wordpress posts

I've successfully implemented a customized dropcap for my Wordpress blog posts using CSS. Is there a way to limit this effect to just the first paragraph, leaving subsequent paragraphs unaffected? Any guidance on achieving this would be highly apprec ...

Retrieving data from mySQL database to display search suggestions using jQueryUI autocomplete

I'm trying to set up an autocomplete feature for a field that connects to an external source and retrieves data from an SQL database: $("#venuename").autocomplete({ source: function(req, add){ $.getJSON("../autocomplete/venu ...

Load a distinct JSP file from the sidebar in JSP

I have an index.jsp file containing a sidebar: <div id="sidebar"> <li ><a href="create">Create</a></li> <li ><a href="view">View</a></li> <li ><a href="Delete">Delete</a></li> & ...

Updating and managing the CSS style for button states

In my asp.net application, I have 5 list items functioning as tabs on a Masterpage. When a user clicks on a tab, I want to redirect them to the corresponding page and visually indicate which tab is active by changing its class. What is the most effective ...

Styling a fixed sidebar in Vue.js

I am currently working on a layout design that includes a fixed sidebar on the left side, positioned beside a container with its own grid structure. Utilizing Vue.js means each template contains <div> elements where I attempt to incorporate additiona ...

How to ensure your content is always visible on Mobile Safari iOS 8 by making sure it stays

Although minimal-ui is no longer supported on iOS8, I've encountered an issue with some fixed content at the bottom of a webpage. The page height is set to 100vh to cover the entire viewport, but some of the content at the bottom is extending below t ...

Hide the menu when a menu link is selected

After conducting extensive research, I have come across several scripts that can achieve what I am trying to do. However, I am unsure of how to implement them with my particular WordPress theme. Therefore, I am seeking assistance here: The theme I am usin ...

Enhancing ajax requests with headers in Ember.js

My goal is to configure request headers for my emberjs application. However, when setting it up in the initializer, the client_id appears as [object Object] instead of the actual value. This is the initializer that runs when the application starts. Apikey ...

Reduce the amount of times append is used in JQuery

Whenever I click the button in Jquery, I aim to limit the number of appends that occur. function standardRoom() { var counter = 0; if ($('select#selectBoxStandard option').length > 1) { counter++; $('#selectBoxStandard&ap ...

Stop the border from curving along with the container's border radius

Currently, I am working on a tabs component that features a container with border radius. When a tab item is selected, it should display a bottom border. However, I'm facing an issue where the border curves along with the border radius instead of rem ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

Navigating Parent Menus While Submenus are Expanded in React Using Material-UI

My React application includes a dynamic menu component created with Material-UI (@mui) that supports nested menus and submenus. I'm aiming to achieve a specific behavior where users can access other menus (such as parent menus) while keeping a submenu ...

What is the best way to send an inline SVG string to my controller?

I am trying to send an inline svg string along with some other properties to my controller. When I replace the svg string with a normal string like "blabla", it successfully reaches my controller. However, with the actual svg string, it never makes it to ...

Issue with updating input value during keyup event in Android Chrome specifically

Check out this straightforward jsfiddle: https://jsfiddle.net/gq9jga4q/33/ <input type="text" id="kbdhook" /> <div id="result" >1: </div> <div id="result2" >2: </div> <div id="result3" >3: </div> $('#kbdh ...

javascript create smooth transitions when navigating between different pages

As a newcomer to JS, I am currently working on creating a website with an introduction animation. My goal is to have this animation displayed on a separate page and once it reaches the end, automatically redirect to the next webpage. <body onload="setT ...

The Kenburn zoom image effect fails to function

I want to implement a zoom effect on an image using the Ken Burns effect when it is clicked. However, the code I have written does not seem to be working as expected. Here is the code snippet: $(document).ready(function () { $('.kenburns').on( ...