What is the best way to enlarge an element when scrolling downwards within the element?

I am looking to create a div that dynamically adjusts its height based on the user's scrolling behavior. The goal is for the div to expand to the very top as the user scrolls downward, and stop when it reaches 70% of the container/parent element. Is there a library available to achieve this smoothly without using incremental adjustments to the height property? Please see the example below:

<div id='container'>
Scroll up to here
<div id='panel'>
As the user scrolls down in this div, expand it to the top incrementally.</br></div>
</div>

#container {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: red;
}

#panel {
  position: fixed;
  width: 100%;
  height: 70%;
  bottom: 0;
    background-color: green;
    overflow-y: scroll;
}

https://jsfiddle.net/1ekpx3sd/

Answer №1

Here is my innovative solution that dynamically adjusts the height of a div element as the user scrolls on the page by detecting the scroll event. This script calculates the new height based on the user's scrolling behavior, setting it to 70% plus the distance scrolled but constrained within a certain limit which can be modified to suit your needs:

function adjustHeight() {
    var mainDiv = document.getElementById("main");
    var wrapper = document.getElementById("wrapper");
    
    var scrollableHeight = wrapper.offsetHeight;
    var newY = mainDiv.scrollTop;
    
    // DETERMINE THE MAX HEIGHT BASED ON PERCENTAGE OR FIXED AMOUNT
    maxHeight = scrollableHeight * 0.9;
    
    var newTargetHeight = Math.min(maxHeight, 0.7 * scrollableHeight + newY);
    
    mainDiv.style.height = newTargetHeight + "px";
    
    
    document.getElementById("content").style.marginTop = Math.min(newY, scrollableHeight - maxHeight) + "px";
}

document.getElementById('main').addEventListener("scroll", adjustHeight);
#wrapper {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: red;
}

#main {
  position: fixed;
  width: 100%;
  height: 70%;
  bottom: 0;
  background-color: green;
  overflow-y: scroll;
}

#content {
}
<div id='wrapper'>
Keep scrolling up for more content...
<div id='main'>
<div id='content'>
Responsive dynamic div height according to scroll behavior in action... Insert your lengthy text or content here...
</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

What are some best practices for managing object-level variables in TypeScript and Vue.js?

Uncertain about the optimal approach, I am looking to create a component and leverage some object level variables. Consider the example below: import Vue from "vue" import * as paper from "paper" export default Vue.extend({ template: ` <d ...

Encountering parameter issues while working with Google Maps React in TypeScript

Currently, I'm utilizing TypeScript in this particular file. import React, {Component} from 'react' import {Map, InfoWindow, Marker, GoogleApiWrapper, mapEventHandler, markerEventHandler} from 'google-maps-react'; import { coordina ...

Can the default position of the scrollbar be set to remain at the bottom?

I have a select option tag with a scrollbar to view the contents in the dropdown. I am looking for a way to automatically position the scroll at the bottom when an item is selected from the dropdown. jquery code $('document').ready(func ...

Appears as though time is slipping away during date conversions

I seem to be experiencing a strange issue where I lose a day when transitioning between MySQL and my JavaScript code, and I can't seem to figure out why. When I insert a date into the database (for example, 10/14/12), it appears as 10/13/12 in the dat ...

Guide to packaging TypeScript type declarations with an npm JavaScript library

I'm facing an issue with providing TypeScript type definitions for a JavaScript library. The library itself is written in TypeScript and transpiled by Babel, although this detail shouldn't affect the outcome. The problem lies in the fact that ne ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...

Passing a div's ID value using AngularJS AJAX to store it in a PHP variable

I have a collection of div elements, each with a unique id. My goal is to trigger a JavaScript function when a specific div is clicked. This function should send the id value of the clicked div to PHP, assign it to a PHP variable, and then receive a respon ...

Leveraging PHP for parsing an HTML document

I am currently working on a PHP application that involves parsing HTML content. My goal is to extract a particular column from a table and store it in PHP variables. Below is the code I'm using: $dom = new domDocument; @$dom->loadHTML($html); $d ...

Transform array sequences into their own unique sequences

Reorder Array of List to Fit My Custom Order Current Output: [ { "key": "DG Power Output", "value": "6.00", "unit": "kWh", }, { "key": "DG Run Time", "value": "5999999952", "unit": "minutes", }, { "key": "Fuel Level (Before)", "value": "8.00" ...

Is it possible to remove htmlescape from the custom options in Magento? Can you specify which file showcases the custom

Looking to incorporate HTML into custom option labels for products on my Magento store. Unfortunately, Magento does not parse HTML by default when it is placed in the custom options interface. I suspect there may be a line like "$this->htmlEscape()" wit ...

Determine whether an element is currently focused using a Vue.js directive

I'm attempting to verify if an element is currently in focus, specifically an input field, and then apply a class to another element. This is the code I have been working on, but for some reason the hasFocus() function isn't functioning as expect ...

Is there a more efficient method to achieve the desired effect without making multiple calls to jQuery ajaxSuccess?

Currently, I am working on creating an effect that involves a quick fade-out followed by a fade-in of the element once the request is successful. Since jQuery processes elements in a routine manner (top to bottom), I have managed to achieve my desired eff ...

Display only the initial word in a hidden input value of an HTML document using NodeJS Express

I have a Node.js application that utilizes EJS as its template engine. On multiple pages, I incorporate hidden inputs and retrieve their values using req.body. Although my body parser in Express is configured correctly, I've noticed an issue where hid ...

Unable to locate a contact within the state

Currently, I am diving into the world of React by exploring its documentation and challenging myself to build a small chat application. While I have a good grasp on the basics, I am encountering some issues with states. Within my code, there is an object ...

centered text positioned perfectly on a dynamically adjusting background

Seeking help to center text both vertically and horizontally within a circle background link that needs to be responsive. Setting the line-height equal to the height of the link is not an option, here's my code: html <a class="Previous" href="#"& ...

Ensure that the <aside> elements span the entire width of their containing parent element

I am currently designing a website and I am aiming for a specific layout. Any advice on how to achieve this would be greatly appreciated. The header of the page consists of: a logo aligned to the left within an <a> tag 2 widgets placed in <asid ...

The React component is failing to display updated data from the Redux store

I've been working with React and Redux, trying to update my counter value in the React view. I can successfully log the latest state of my Redux store, but the changes are not reflecting in my React view. const counter = (state = 0, action) => { ...

How can `localePath()` be utilized to create a dynamic route with Nuxt i18n?

I currently have this route set up in my i18n.config.js: { pages: { 'rental/_id': { nl: '/verhuur/:id', en: '/rental/:id', de: '/mietbestand/:id', }, } } When attempting to link to this page in my ...

css: text not enclosed by a span tag

I created an HTML-CSS demo with two simple span tags. I added some content to each span, and applied the same CSS code to both. Surprisingly, the content of the right span goes beyond the border, while the content of the left span stays within the border. ...

Styling each individual page in R Shiny with CSS

I've created an R shiny application and am in the process of adding styling to it. I've incorporated some HTML code and want to customize elements like the background color using CSS. After doing some research online, I learned that I should use ...