Is there a way to navigate to a specific element inside a div with overflow-y enabled?

I have a div with a max-height of 300px, causing a scrollbar to appear when the content exceeds this limit. I am seeking a way to click a button and automatically scroll to a specific element within that div. While I know how to manipulate the main browser scrollbar, I am uncertain if it is feasible to control the scrollbar generated for my div container.

This is the structure of my HTML:

<div style="min-height: 300px; max-height: 300px; overflow: hidden" class="card-block pt-0 pb-0">
        <div class="row" style="min-height: 300px; max-height: 300px;">
            <div class="col-5" style="overflow-y: auto; min-height: 300px;border-right: 1px solid rgba(0, 0, 0, 0.125); min-height: 300px; max-height: 300px;">
                <div class="pt-3 pb-3" style=" max-height: 300px;">
                    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
                    <div id="scroll-here">content</div>
                </div>
            </div>
        </div>
    </div>

Is it feasible to directly scroll to the div identified by the id "scroll-here"?

I am working with Angular 5 and Bootstrap 4, in case that information is relevant.

Answer №1

Here is a helpful code snippet for scrolling to a specific span on a webpage:

$(#id_of_div_with_scroll).scrollTop($("#your_span_id").offset().top);

Answer №2

Here is a simple way to achieve that:

<a href="#scroll-here">Click me</a>

<div style="min-height: 300px; max-height: 300px; overflow: hidden" class="card-block pt-0 pb-0">
  <div class="row" style="min-height: 300px; max-height: 300px;">
    <div class="col-5" style="overflow-y: auto; min-height: 300px;border-right: 1px solid rgba(0, 0, 0, 0.125); min-height: 300px; max-height: 300px;" id="scroll-container">
      <div class="pt-3 pb-3" style=" max-height: 300px;">
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
        <div id="scroll-here">content</div>
      </div>
    </div>
  </div>
</div>

And if you prefer an animated effect:

$('button').click(function() {
  $("#scroll-container").animate({ scrollTop: $('#scroll-here').offset().top });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>

<div style="min-height: 300px; max-height: 300px; overflow: hidden" class="card-block pt-0 pb-0">
  <div class="row" style="min-height: 300px; max-height: 300px;">
    <div class="col-5" style="overflow-y: auto; min-height: 300px;border-right: 1px solid rgba(0, 0, 0, 0.125); min-height: 300px; max-height: 300px;" id="scroll-container">
      <div class="pt-3 pb-3" style=" max-height: 300px;">
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
        <div id="scroll-here">content</div>
      </div>
    </div>
  </div>
</div>

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

Sending data from Node.js to PHP using POST method

Currently, I am attempting to send data from a Node.js application to a PHP script using a POST request. I am in the process of creating a proof of concept, but I am encountering an issue where the data does not seem to be reaching the PHP side. Although t ...

Enhance the appearance of the og:image by applying CSS styling

Can CSS styling be applied to a dynamic facebook og:image used for Facebook shares? Instead of using a static image, I'd like to add some custom styling such as a colored block in the top right corner with styled text. Currently, my tag simply grabs ...

Styling applied exclusively to the field for mobile devices

As I work on creating a navigation bar using Bulma and Vue.js, I encounter an issue with the dropdown menu behavior. When the navbar collapses into the hamburger menu, the dropdown list remains in display: block; mode. To address this, I attempted a workar ...

Guide on inserting a button within a mat-select choice upon selection

My goal is to include a URL button next to a dropdown option that will open a new tab with relevant information when clicked. However, I only want the button to appear for the selected option, not all options in the dropdown list. The issue I'm facin ...

Header reference differs from the document referrer

this is the request header: GET / HTTP/1.1 Host: localhost:3002 Connection: keep-alive sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96" sec-ch-ua-mobile: ?0 User-Agent: ...

JavaScript does not recognize Bootstrap classes

I am utilizing Bootstrap 5.3.0 and have included it through CDN links in my JS script. This is how I am injecting it using my js file within the existing website's DOM via a chrome extension named Scripty, which serves as a JS injector extension for c ...

Tips for displaying Ajax response in a modal popup

I have a link that, when clicked, sends an AJAX request and successfully receives a response in the form of an HTML file, which I then append to a div. However, I want to display this div as a modal popup and I have attempted the following: In the HTML fi ...

Is there a way to expand "row" elements to the left side in Bootstrap?

<div class="container-fluid"> <div class="row"> <!-- Left side --> <div class="col-xl-10 style"> <div class="row" > <div class="col-xl-12 style">Box1< ...

Potential Unresolved Promise Rejection (ID: 0): The object 'prevComponentInstance._currentElement' is undefined

Attempting to fetch JSON data in react native using axios.get(my_url_path), then updating the state with response.data under the key 'urldatabase'. When attempting to access this state key and read the data from the JSON, an error is encountered: ...

Troubleshooting: Issues with window.location.href and window.open within an iframe

Code Update <div> <button type="button" class="submit btn btn-default" id="btnSubmit">Submit </button> <button type="button">Cancel</button> </div> <script> $("#btnSubmit").click(function(e) { ...

Ways to insert space between tags in jade compiled through webpack

My Angular template is structured like so: ul li(ng-repeat-start="item in items") {{item.name}} br(ng-repeat-end) Can I add a space or newline character between li and br? Currently, it looks like this: { test: /\.jade$/, loader: 'raw!jade ...

Auto resizing images with Bootstrap may not function correctly when placed in the left column

I have a standard 3-column layout with similar images in the left and right columns. When I resize the page, the right image becomes smaller but the left image does not. This causes the middle container to overflow past the left image. Here is the CSS for ...

What steps can I take to pinpoint the exact error location when running assetic:dump in Symfony2?

This error message indicates an issue with assetic:dump in Symfony2. [Assetic\Exception\FilterException] ...

Utilizing HTML across various components

Can one component's HTML be utilized in another component? If I opt for Selector, it will come with the TS Component functionality as well. Is there a way to reuse that specific HTML page in multiple locations? ...

What steps should I take to retrieve a value from a Headless-UI component?

I have integrated a Listbox component from Headless-UI in my React project. The Listbox is contained within its own React component, which I then include in a settings form. How can I extract the selected value from the Listbox component and save it to th ...

Controlling the back DIV while maintaining overlapping layers

I successfully positioned my right hand content on top of the leaflet map in the background using CSS properties like position and z-index. However, I am looking for a way to make the overlapping div not only transparent but also non-interactive while stil ...

Unable to include a JavaScript file within another JavaScript file

Currently, I am working on a project where I am utilizing Django for the backend and HTML/CSS/JS for the frontend development. On a specific HTML page, I am including two JS files: dom_creator.js and console_page.js. My goal is to access the functionaliti ...

The variable in Vue.js is failing to update, resulting in a "variable is not defined" error

I've been attempting to display the updated value of the totalQuestions variable in the HTML, but I keep encountering the following error. Can someone point out where I went wrong? https://i.sstatic.net/mEEMS.jpg HTML <label><span class="r ...

Retrieve a specific attribute from every row within an HTML table

I have an HTML table and I'm looking to extract the call_id attribute from each row using JavaScript. Here's a snippet of the table in my editor: Simplified view: https://i.sstatic.net/MlyNj.png After accessing the table using the code below, I ...

Best approach to inform pages about a variable update in Ionic

Imagine a scenario where we have a page called ListItemPage displaying a list of items: Within the ts file, there is a variable defined as items: any = []; In the html file, we find <ion-item *ngFor="let item of items"> Users can click on a (+ ...