div container overflowing with numerous unordered list elements

I am having an issue with a div and ul in my HTML code. The height of the div is smaller than the combined height of all the li elements within it, so I have set overflow to auto in the CSS for the div.

Here is an example of the HTML code:

<div class="points-area">
    <ul class="points-list">
        <li class="point selected" id="startPoint">Start</li>
        <li class="point" id="endPoint">End</li>
        <li class="point" id="N">Nasion</li>
        <li class="point" id="S">Stella center</li>
        <li class="point" id="P">Porion</li>
        <li class="point" id="ar">Artikulare</li>
        <li class="point" id="T1">T1</li>
        <li class="point" id="Me">Me</li>
        <li class="point" id="Gn">Gnathion</li>
        <li class="point" id="T2/MT1">T2/MT1</li>
    </ul>
</div>

Here is the CSS code:

.points-list{
    list-style: none;
    padding: 0;
    margin-top: 0;
}

li.point{
    border-bottom: 1px solid black;
    padding: 0.1em;
}

.points-area{
    overflow: auto;
    height: 20em;
    border: 1px solid black;
}

li.point.selected,
li.point:hover{
    background-color: blue;
}

I use some JavaScript that selects the next li element when a user adds a circle on a kineticjs stage (toggles the 'selected' class).

if (! $("li.point.selected").is(":last-child")){
    prevLi = $("li.point.selected");
    prevLi.next().toggleClass('selected');
    prevLi.toggleClass('selected');
    toBeAdded = prevLi.next();
}

My problem arises when the scrollbar doesn't move as I navigate through the li elements because there are more points than can be displayed within the div. I need the scrollbar to automatically scroll down when changing from one point to the next. How can I achieve this?

Answer №1

Learning how to adjust the scroll position in your HTML page is straightforward. By referencing elements such as 'div height', 'li height', and utilizing scrolltop and scrollto functions in JavaScript or jQuery, you can easily accomplish this.

Calculate the scrollTop by determining the number of list items (li's) you want to scroll through, then implement scrollTo to navigate to a specific location on the scroll bar!

If you have your code hosted on platforms like Plnkr or JSFiddle, feel free to share it with me for further assistance.

Answer №2

Success! Check out the latest fiddle

I made a key addition to the code snippet below:

var divHeight, liHeight, lisPerScrollPage;
divHeight = $(".points-area").height(); //determine div's height
liHeight = $("li.point").outerHeight(true);// determine li's height with padding and margin included
lisPerScrollPage = parseInt(divHeight / liHeight); //figure out how many lis can fit on one scroll page

Next, I implemented logic that checks if the selected li is within the scroll area. If it isn't, the scroll area is adjusted accordingly. If the scroll area is already showing the selected li, no action is taken (in case the user manually scrolled).

index = $("li.point").index(toBeAdded) + 1;
if ($(".points-area").scrollTop() < liHeight *(index - lisPerScrollPage))
{
    $(".points-area").scrollTop(liHeight *(index - lisPerScrollPage));

}

Seems like everything is functioning as intended.

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

I'm completely baffled as to why the client console is unable to locate my JS file

I have a question that may seem basic, so please bear with me! I'm having trouble adding a js file to an html page and it seems to be related to the file path. In my HTML page, I have this code to link the file: <script src="../src/utils/mapbo ...

Using jQuery to specifically target elements of a specific class that are nested within another element

I am currently using a jQuery function that toggles a drop-down navigation menu by changing its class when clicked: $(function () { $('.nav-titles').on('click', function() { $('.nav-dropdown').toggleClass(& ...

datepicker in bootstrap 4 obscured by the div

I am currently utilizing Bootstrap datepicker v4. However, I am facing an issue where the datepicker view is getting hidden behind the 'map' div. I have attempted to solve this problem by adding overflow: visible, but unfortunately, it does not s ...

Angular does not delay for promises to settle

Issue I am facing is related to Angular not waiting for promises to be resolved. The console inspection reveals that the provider and skills objects are not retrieved before the promises are returned. I have included the key parts of the code below. The s ...

What steps should I take to handle an ArrayIndex out of bound error in JavaScript?

Consider the following array: var john = ['asas','gggg','ggg']; If an attempt is made to access john at index 3, i.e. john[3], it results in an error. Is there a way to show a message or trigger an alert indicating that ther ...

Utilize Google Maps to receive directions to a specific destination and discover current traffic conditions along the route using their comprehensive API

I am currently working on implementing the Google Maps direction identifier functionality with traffic details. However, I am able to obtain directions but not specific traffic details for a selected path; it seems to apply overall traffic data instead. ...

Is the button not aligned properly with the email input field?

<div class="subscribe__form--action--btn"> <form action="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32545b5e5b5746545C5B5356477F54585850"> </a>" meth ...

An array comparison function in JavaScript

Similar Question: Using jQuery to compare two arrays I'm looking for a Javascript or jQuery function that can check if one array contains all the values of another array. The second array may have more values than the first. The function should ...

"Utilize the CSS inheritance feature to inherit properties from parent

I am looking to add a border-bottom to a header element with the same color as its child font. Can you please provide me with the HTML code and guide me on how to proceed? <header> <div class="cblt-panel"> <header> <a ...

Steps for extracting a portion of the current page's URL

Can someone help me extract a specific part of the current URL using JavaScript? The URL looks something like this: I need to isolate the number "3153038" from the URL and store it in a JavaScript variable. Thank you! ...

Currently, I am in the process of constructing a DSpace repository on an Ubuntu 16.04

I have successfully set up a DSpace repository on Ubuntu 16.04 LTS by following the installation instructions provided in this manual. After deploying the webapps, I encountered an issue when trying to add new items. Upon clicking the search link, I recei ...

Ajax: client dilemma created by the interaction of two functions

My university homework assignment requires me to develop a small e-commerce website. After logging in, the user will be directed to the homepage where they will receive a JSON object from the server containing product information to dynamically generate th ...

Leveraging Angular's capability to import files directly from the assets

I recently installed a library via npm and made some modifications to one of the modules. python.js If I delete the node_modules folder and run npm install, I am concerned that I will lose my changes. Is there a way to preserve these modifications by mov ...

Find out if OpenAI's chat completion feature will trigger a function call or generate a message

In my NestJS application, I have integrated a chat feature that utilizes the openai createChatCompletion API to produce responses based on user input and send them back to the client in real-time. Now, with the introduction of function calls in the openai ...

React's memo and/or useCallback functions are not functioning as anticipated

Within my Home Component, there is a state called records, which I utilize to execute a records.map() and display individual RecordItem components within a table. function Home() { const [records, setRecords] = useState<Array<RecordType>>(l ...

The error message "MVC JS deletethisproduct is not defined at HTMLAnchorElement.onclick (VM457 Index:40)" indicates that there

Upon clicking the button, I encounter this error: "deletethisproduct is not defined at HTMLAnchorElement.onclick" While I understand that using onclick is not the ideal approach, I am employing it because I need to retrieve the product id from the mode ...

Jquery and adaptability

I currently have a scrollTo anchor link script on my website that has an offset of -35px to account for my 35px high navigation bar. However, I find that this offset may need to be adjusted for different screen resolutions, such as smartphones or tablets ...

How to send props from a Vue.js component tag in an HTML file

I'm facing an issue with passing props from the HTML to the JavaScript code and then down to a Vue component. Here's a snippet of my index.html file: <div id="js-group-discounts"> <div class="form-group required"> <datepick ...

Tips for properly nesting a "carousel" div within an "oe_structure" without any overlapping issues

Having trouble with placing a "carousel" div inside the "oe_structure" in my odoo template. I am currently working on editing the "website_sale.products" template. As a newcomer to Odoo, can I use inheritance in my direct code to make edits? I'm stil ...

Adjusting the dimensions of the image carousel

Here is my code for an Image carousel: <div class="container"> <div class="row"> <div class="col-md-12 col-md-offset-0"> <div id="imageCarousel" class="carousel slide" data-interval="4000" data ...