Place the div's scrollbar at the beginning of its content

Recently, I put together a custom CSS modal that includes a scrollable div (without the modal itself being scrollable). Interestingly enough, when I initially open the modal, the scrollbar of the div starts at the top as anticipated. However, if I scroll down, close the modal, and then reopen it, the scrollbar returns to its previous position instead of resetting to the top like I would prefer.

In case you're curious, here is the CSS code for the modal:

.create-modal-content {
        position: relative;
        background-color: #F7F7F8;
        margin: auto;
        padding: 0;
        border: 1px solid #888;
        width: 95%;
        height: 95%;
        border-radius: 5px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        animation-name: createAnimation;
    }

    @keyframes createAnimation {
        from {
            right: 0;
            opacity: 0
        }
        to {
            right: 0;
            opacity: 1
        }
    }

And here's the code snippet for the scrollable div:

.create-container {
        background-color: #e7e7e7;
        margin: 35px 15px 0 15px;
        border-radius: 10px;
        max-height: 70vh;
        overflow: auto;
    }

If you're interested in reviewing the complete HTML and CSS code for the modal and the div, take a look below:

<div class="create-modal-content" >
  <div class="ml-12 mt-24 font-size-18 font-weight-700">Create Organization</div>
  <div fxLayout="row wrap" fxLayout.xs="column" fxLayoutGap="15px" class="pl-48 pt-12 pb-12 create-container">
    <input type="file" accept="image/*" hidden #fileInput (change)="imageSelected($event )" />
   </div>
               
   <div fxLayout="column" fxLayoutAlign="end center">
    <button class="mt-12 mb-12" mat-raised-button color="accent" (click)="saveOrganization()">
         Edit
    </button>
   </div>
  </div>

To tackle the issue, I tried using both querySelector and getElementByClassName in the TypeScript section below without success:

const modal = document.getElementById('organization');
        modal.style.display = 'block';
        const createContainer = modal.getElementsByClassName('create-container')[0];
        // const createContainer = modal.querySelector(".create-container");
        modal.addEventListener('click', function() {
            createContainer.scrollTop = 0;
        });

Answer №1

Try using the scrollTo(x, y) method

window.addEventListener('click', function() {
    container.scrollTo(100, 200);
});

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

Option in the dropdown menu will not appear on the user interface if the data contains the "&" sign

Below is an example dataset I'm using for my dropdown selection: [ { "value":"example1", "name":"example name 1" }, { "value":"example2", "name":"example & name 2" ...

Unable to load models in face-api.js even though attempting to infer the models

Currently, I am working on developing an application for face detection. However, I am encountering an error stating "SsdMobilenetv1 - load model before inference" despite loading the models. The Front End HTML File is being sent from the server, ...

AngularJS controller experiencing scope() function returning undefined issue

I've been working with a function inside the controller: $scope.passValues = function (param1){ return "foo"; }; console.log($scope.passValues()); It logs foo, but then I tried this: $scope.passValues = function (param1){ return param1; ...

How can you assign a unique number to each item within the same category in order to differentiate them?

const productData = [ {size: 1180160, category: "Keswick", index: 1}, {size: 1059328, category: "HCOPA", index: 2}, {size: 30720, category: "HCOPA", index: 3}, {size: 493056, category: "Darhan Saluja", index: 4}, {size: 267776, category: "CRSA", index ...

Accessing PageController through XmlHttpRequest is not allowed in Shopware 6

I'm encountering an issue when attempting to send a request to my customized StorefrontController on the most recent version of Shopware 6.2.2. The error message I'm receiving is: PageController can't be requested via XmlHttpRequest. I&apos ...

Having trouble with implementing the .addclass function in a dice roller project

I'm looking to have the element with id=die load initially, and then on a button click event labeled "click me," apply the corresponding CSS class such as 'die1,' 'die2,' and so forth. function roll() { var die = Math.floor(Ma ...

The request payload appears as [Object Object] when initiating the AJAX post request

Currently, I am making an AJAX request with the specified data. The data being sent is an object and my intention is to send the object itself without converting it to a JSON string. However, when viewing the request in the browser, the payload is shown as ...

sending an array via xmlhttprequest

I am attempting to send all the marks entered by the user to another page for processing, but only the value of the first mark entered in the table is being sent. Is there a way to send all the values of the marks entered rather than just the first value ...

What is the best way to adjust the width of floating divs to completely fill the space they occupy?

On the first picture, there are six equal divs displayed. As the screen size increases, the width of the divs also grows to fill up their space, like a table cell or another div. If there is enough space in the first row to accommodate the fourth div, it s ...

What steps can I take to ensure that the text content stays within the borders and does not overlap with the photo?

click here for the image description Html <p><img src="index-foto1.jpg" align="left" width=65% style="border-radius: 10px;" margin-right: 2px> <div class="tant"><h3>More detailed inform ...

Avoiding HTML (JSX) tag duplication in React Component

Having the same code in my React component multiple times is becoming repetitive and inefficient. Is there a way to follow the DRY principle and avoid repeating this code? While creating a landing page with Sass styling, I noticed that I am duplicating t ...

What is the process of combining two identical objects in Angular JS?

Is it possible to merge and sort two objects in Angular JS that have the same fields, notifications.fb and notifications.tw, based on their time field? ...

Show the layout of the table in a visual format

I am struggling to showcase a table created using <ul> tags. I want the content to be displayed one after the other. Here is my code: CSS .activity-list-header > li { display: inline-block; text-align: left; width: 15.666%; list- ...

Utilizing server-side caching middleware with tRPC version 10

Currently, I am working on a Next.js project and exploring the possibility of incorporating in-memory caching for tRPC results. Each tRPC procedure should have the option to set a custom TTL for caching purposes. My initial thought is that utilizing tRPC&a ...

The dropdown menu displaying the img-dropdown-content with a display set to none is not functioning as anticipated

Attempting to hide drop down menu with img-dropdown-content but it's not working, possibly being overridden by another element - only using html and css I have experimented with visibility: hidden; both with and without !important as well as display: ...

Transform a flat 2D shape into a dynamic 3D projection using d3.js, then customize the height based on the specific value from ANG

Currently, I am utilizing d3.js version 6 to generate a 3D representation of the 2D chart shown below. Within this circle are numerous squares, each colored based on its assigned value. The intensity of the color increases with higher values. My goal is t ...

Move a <div> using a handle (without using JQuery)

I devised a plan to create a moveable div with a handle and came up with this code snippet: var mydragg = function() { return { move: function(divid, xpos, ypos) { divid.style.left = xpos + 'px'; divid.style.top = ypos + &apo ...

Why is it important to have specific property names?

Here is some code and HTML for triggering a radio button. It seems to work fine without any issues, but I'm wondering if the presence of a name property is necessary. When I remove the name property, it stops working. Can someone explain why? <inp ...

What is the best way to make an image clickable in Next.Js?

Is there a way to turn an image into a link in Next.Js? I have the Instagram logo that I want to use as a link to my Instagram page. I want it to open a new tab and redirect the user to my Instagram account when clicked. I would really appreciate any guid ...

Selenium automation tool encountering difficulty in finding an element within a dropdown menu

I encountered an issue with Selenium where it was able to click on the button that reveals a dropdown list, but unable to locate the list itself after it appears. Despite trying various methods such as find_element_by and Select module, along with switch_t ...