Retain the scroll location even after deleting items

Imagine a container holding various elements that can be removed one by one. As you scroll to the bottom of the container and remove an element, the container should dynamically adjust its size and position itself upwards.

function remove(target) {
    target.remove();
}
body {
  margin: 0;
}
.container {
  overflow-y: scroll;
  height: 200px;
  background-color: gray;
}

.el {
  height: 60px;
  background-color: cadetblue;
  margin: 5px;
  text-align: center;
  vertical-align: middle;
  line-height: 60px; 
  font-size: 20pt;
}
<div class="container fill">
  <div class="el" onclick="remove(this)">1</div>
  <div class="el" onclick="remove(this)">2</div>
  <div class="el" onclick="remove(this)">3</div>
  <div class="el" onclick="remove(this)">4</div>
  <div class="el" onclick="remove(this)">5</div>
  <div class="el" onclick="remove(this)">6</div>
  <div class="el" onclick="remove(this)">7</div>
</div>

My goal is for the container to expand its height after removing an element while maintaining the scroll position without leaving any extra space at the end determined by CSS. This additional space should only show up as needed, disappearing once scrolled back up so it cannot be reached again.

Is there a way to achieve this using only HTML5, CSS, and JS?

Answer №1

Do you wish for the container to automatically adjust its height based on the content size when there are only a few items remaining? In that case, you can replace height: 200px; with max-height: 200px;.

function exclude(target) {
    target.exclude();
}
body {
  padding: 0;
}
.container {
  overflow-y: scroll;
  max-height: 200px;
  background-color: gray;
}

.item {
  height: 60px;
  background-color: cadetblue;
  margin: 5px;
  text-align: center;
  vertical-align: middle;
  line-height: 60px; 
  font-size: 20pt;
}
<div class="container fill">
  <div class="item" onclick="exclude(this)">1</div>
  <div class="item" onclick="exclude(this)">2</div>
  <div class="item" onclick="exclude(this)">3</div>
  <div class="item" onclick="exclude(this)">4</div>
  <div class="item" onclick="exclude(this)">5</div>
  <div class="item" onclick="exclude(this)">6</div>
  <div class="item" onclick="exclude(this)">7</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

The process of importing mongoose models is not functioning properly

Initially, I had no issues when defining my schema and models in the same file where they were being used. However, when I tried to import them following the examples I found online like this... index.js var mongoose = require('mongoose'); var ...

Encountering problems with Node Sass versions while trying to import custom scss files into app.js

I've encountered several articles on stack that didn't quite solve my issues. The problem arose when I tried to import a custom SCSS file into my React app.js to override bootstrap theme colors. Here are the style imports in my App.js: import &q ...

Pass back the jQuery deferred object from the error section of an Ajax request

I am facing a challenge with loading a file using jQuery Ajax. When the initial location fails, it should attempt to retrieve it from another path. Although the request redirects to the alternate URL, I am not receiving the necessary jQuery deferred from l ...

What is the process for creating a personalized <a> tag using CSS?

On my website, I am looking to differentiate between two types of <a> tags - one for inline links and the other for standalone links. This means that I want these two variations of <a> tags to have distinct colors and styles. For inline links, ...

Whenever I try to access a specific position within a JSON array, I receive the value of 'undefined'

After executing a database query in PHP and returning the results through AJAX in a JSON array, I am facing an issue where the data is being accessed as 'undefined'. Why is this happening? Below is my PHP code snippet: <?php $tipo_prod ...

Adding data dynamically to XSLT through jQuery upon button click: A step-by-step guide

Can you provide guidance on how to dynamically append data in XSLT upon button click using jQuery? This is what I have tried so far: <button onclick='clickBtn()'>append</button> JavaScript function function clickBtn(){ cons ...

Tips for arranging divs in a row to switch to stacking when the screen size decreases

Sorry if this question seems repetitive, but as a newbie, deciphering posts from others has been overwhelming! I have two divs placed side by side within a wrapper, but I need them to stack on top of each other when viewed on a tablet or phone. Below is t ...

Creating an HTML string and then displaying its outer HTML in IE10 can be easily achieved. Just write the

My task is to write an HTML string to an element and then retrieve the resulting outer HTML from that element. This needs to be operational in IE10, latest versions of FF, Chrome, Safari, Android, iOS Safari but does not have to support any older browsers. ...

Guide to transforming an array into a custom JavaScript object illustrating designated names for each key

Trying to utilize JavaScript to transform the given array into a JavaScript object, but unsure of the process. The input array is structured such that each key corresponds to a day of the week (Sunday to Saturday); where key 0 represents Sunday, key 1 rep ...

using boostrap modal leads to background displacement

Just starting out with bootstrap and encountering a modal bug: when I click on a modal button, the background shifts to the right by 15px. My current bootstrap template is version 3.3.0. I attempted to update to the latest 3.3.4 version, but unfortunately ...

Is it possible to alter Bulma Sass variables dynamically by referencing the class of an HTML element?

I am currently working on implementing a dark and light theme using bulma. My plan is to dynamically assign classes to elements using vue (such as .dark-theme or .light-theme) and then apply different colors based on these themes. However, I am facing an i ...

What is the method for creating a hover line that perfectly mirrors the length of text above it?

I'm interested in creating a hover effect where a line appears above my text when hovering over it. The twist is, I want the hover line to match the length of each individual text. This effect will be applied to the navigation section of my HTML. Her ...

Matching current URL and links by checking the div class

How can I verify if the current URL matches a link on my page, but also check if that link has a specific div class? For example: jQuery(document).ready(function($){ // find anchor tag on page with matching current location URL jQuery("*").find("a[h ...

HTML code for adding a dropdown menu inside a table cell

I am trying to set up a table where one cell functions as a dropdown menu. The data.field is being fetched from the backend and I want it to be displayed during rendering. Additionally, a fList is also retrieved from the backend. When a user clicks on th ...

Calling sequence of AWS Lambda function with Node.js

Here is a simplified version of AWS lambda code snippet: 'use strict'; var aws = require('aws-sdk'); var docClient = new aws.DynamoDB.DocumentClient(); exports.handler = (event, context, callback) => { var inputorder = initIn ...

Steps for implementing "show less" and "show more" functionality in the cells of a reactive table within a Meteor

customTableSettings : function () { return{ rowsPerPage: 5, showNavigation: 'auto', showColumnToggles: false, fields: [ {key: 'para',label: 'Para',sortable: false}, { ...

The variable in my scope is not reflecting the changes in the HTML view

Here is my code for handling file attachments in AngularJS: $scope.attachments = []; $scope.uploadFile = function(files){ for(var i=0; i<files.length; i++){ $scope.attachments.push(files[i]); console.log($scope.attachments.length); } } ...

Transferring data between two "Data" elements using Jquery Datatable

Utilizing the JQuery datatable, I have made the first column of my table clickable, which is labeled as RequestNo. Here's the code snippet: "ajax": { "url": "/Request/Search/LoadData", "type": "POST", "datatype": "j ...

The final input is the only one that functions

I am currently working on a large form, but I am encountering a major issue with it. In certain sections of the form, only the last input field seems to be functional. For example: [...] <div class="main_data"> <span class="info">main dat ...

Using `gulp` to send arguments to tasks invoked in `runSequence`

In my gulp task configuration, I have the following setup: gulp.task('concatenate_client_js', function(){ return merge2( gulp.src('./gulp/buildConfig.json') .pipe(plugins.ngConfig('app.envConfig', { ...