Connecting HTML element using onscroll or onchange does not function

Is there a way to change the width of a div .progress container when the height of div #myContact reaches 100% on scroll?

I've attempted to link the HTML element with onscroll and onchange, but nothing seems to work.

<div onscroll="scrollProgress()" id="containter" class="snap">
        <div class="progress-container"> //need to adjust width of this container
          <div class="progress-bar" id="myContact"></div> //when this one hits 100% height
    </div>

    </div>
    <script src="scroll.js"> </script>
#containter {
  width: 100%;
  height: 3000px; // original setting is 100vh, this value just for scrolling without pasting all other elements
  scroll-behavior: smooth;
  overflow: scroll;
  scroll-snap-type: y mandatory;
}

.progress-container {
  position: fixed;
  width: 20%;
  height: 100%;
}

/* The progress bar (scroll indicator) */
.progress-bar {
  width: auto;
  background: #000;
  height: 0%;
  max-height: 100%
}
// Scroll function
function scrollProgress() {
      var elem = document.getElementById('containter');
      var winScroll = elem.scrollTop;
      var height = elem.scrollHeight - document.documentElement.clientHeight;
      var scrolled = (winScroll / height) * 100;
      document.getElementById("myContact").style.height = scrolled + "%";
};
// Expand function

/* When 'myContact' reaches 100% height then 'progres-container' expands left, and cover the entire screen*/

function expandMe() {
    let trigger = document.getElementById('myContact').style.height;
    if (trigger = 100) {
      document.getElementById('progress').style.width = trigger + "%";
    }
  };

Currently, nothing happens and no errors are displayed.

Your assistance would be greatly appreciated!

Answer №1

While this solution may not be perfect, it addresses the mistakes in your code by binding an event listener to the window object for proper rendering. Additionally, I have incorporated some visual effects to enhance the width changing style.

window.addEventListener('scroll', function () {
      var winScroll = window.pageYOffset;
      var height = document.getElementById('container').scrollHeight - document.documentElement.clientHeight;
      var scrolled = (winScroll / height) * 100;
      document.getElementById("myContact").style.height = scrolled + "%";
      document.getElementById("myContact").parentElement.style.width =(scrolled >= 100) ? scrolled + "%" : "20%";
});
#container {
  width: 100%;
  height: 3000px;
  scroll-behavior: smooth;
  overflow: scroll;
  scroll-snap-type: y mandatory;
}

.progress-container {
  position: fixed;
  background: #0f0;
  width: 20%;
  height: 100%;
  transition: width 1s;
}

/* The progress bar (scroll indicator) */
.progress-bar {
  width: auto;
  background: #f00;
  height: 0%;
  max-height: 100%
}
<div id="container" class="snap">
        <div class="progress-container"> 
          <div class="progress-bar" id="myContact"></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 is the best way to make an exit pop up disappear when clicking on any area?

I'm new to programming in JavaScript and jQuery and I wanted to implement an exit pop-up feature. While I came across a helpful tutorial online, I encountered an issue where users could only exit by clicking on the "X" button. I want them to be able t ...

Cannot Properly Import JSX Elements in TailwindCSS and React

I am facing an issue with importing a JSX Element into my main file, App.js. The element I want to load is in a separate file called element.js and contains the following code: const element = () => { return ( <div className="text-gr ...

Passing data between child components using Vuejs 3.2 for seamless communication within the application

In my chess application, I have a total of 3 components: 1 parent component and 2 child components. The first child component, called Board, is responsible for updating the move and FEN (chess notation). const emit = defineEmits(['fen', 'm ...

I am having success posting data through a form, however, the fetch API is not functioning as expected for me

Currently, I am working on a React Project and focusing on creating a signup form. Everything seems to be fine when posting form data using the Form, but things get stuck when I try to use onSubmit={handleSubmit} along with fetch APIs such as axios or just ...

Difficulty altering link hover background color

I'm having trouble changing the hover effect background-color on the tweets of this page: Despite my efforts, all the links except for the latest tweets are working perfectly. What am I missing? Here's what I've been trying... <script& ...

Error at line 63 of app.js: GLTFLoader is not defined

Whenever I try to use const loader = new GLTFLoader(); in three.js, I encounter the error app.js:63 Uncaught ReferenceError: GLTFLoader is not defined. Even though it is imported in index.html, this error persists <script src="./three.min.j ...

PHP results can be manipulated with jQuery and JavaScript

I have a unique jQuery script that is designed to insert custom content after each paragraph in news articles. Here's an example: $(document).ready(function() { $("<a>The link inserted in jQuery</a>") .insertAfter("p") ...

Does the Node Schedule library create new processes by spawning or forking them?

Is the node-schedule npm module responsible for spawning/forking a new process, or do we need to handle it ourselves? var cron = require('node-schedule'); var cronExpress="0 * * * *"; cron.scheduleJob(cronExpress, () => { //logger.info(" ...

Tips for creating dynamic row styles in a fluid table layout?

Could someone help me figure this out? I'm not familiar with JavaScript and need assistance as I've never created it, only edited existing scripts. I have a layout with two tables side-by-side. On mobile devices, the second table is pushed below ...

Tips on customizing the CSS responsiveness of your Bootstrap carousel

I have recently installed a Bootstrap theme on my Wordpress site and I am trying to make some CSS adjustments. One issue I am facing is with the carousel. Currently, when I resize the website or view it on a mobile device... The carousel maintains a lar ...

The concept of transparency in CSS being utilized on a nested div element

Seeking clarity on the opacity property in CSS. My dilemma involves a header with a title, tinted with an opacity of .3 (in addition to a transition effect - irrelevant to the inquiry). Referencing the gif below: https://i.sstatic.net/LXwPv.gif I apprec ...

Dropzone.js encounters a post 500 error (Internal Server Error) when attempting to upload files larger than 5mb

This is the code I'm working with: var previewNode = $("#template"); previewNode[0].id = ""; var previewTemplate = previewNode.parent().html(); previewNode.remove(); var documentsDropzone = new Dropzone("#AddDocumentModal #AddFiledr ...

What is the best way to split the vertices of a mesh in Three.js?

Is there a way to disassemble the vertices of a Three.js mesh? I am interested in tearing apart a 3D object using mouse input. Recently, I developed a GLSL vertex shader for this web application that shifts the vertices of a mesh based on the brightness of ...

Sorting in Postgres is about organizing data in a specific order

I've been working on organizing my Postgres questions database by ID. Currently, the order seems random, but I want it to be sorted by ID in ascending order. Since I'm new to Postgres, I'm not entirely sure about what I need to do... All ...

Dynamic Image Grid Created Using JavaScript Glitches

My dilemma involves using JQuery to create an HTML grid of images styled with Flex Boxes. The setup works perfectly on desktop, but when viewing it on mobile devices, the images begin to act strangely - jumping, overlapping, and even repeating themselves i ...

Is it possible to halt code execution in Node JS after sending a response?

I am currently learning Node JS and practicing to develop a MERN app. I have implemented two middleware functions named getUserById and isSignedIn. router.param("userId",getUserById) const getUserById = (req,res,next,id)=>{ User.findById( ...

triangular shape at the top and rounded shape at the bottom combined with various colors

Is there a way to create a triangle with a rounded bottom section in a different color? #box { content:""; width: 0; height: 0; border-style: solid; border-width: 0 150px 150px 150px; border-color: transparent yellow red blue; ...

How can you select a variable using Jquery?

$.get('sampleurl.com',function(result){ target = $(result #specificID).attr("href") // Can someone help me with this??? }) I fetch content from an external website using $.get and store it in the result variable, now I'm trying to figure ou ...

Challenge with Angular *ngFor: Struggling to Access Previous Elements

In my angular and node application, I am using socket.io. When a user joins the room, they can see their username in the user list. If another user joins, the first user can see both usernames but the new user can only see their own. This pattern continues ...

What could be causing the sudden glitch in my sidebar animation?

I'm encountering issues with the sidebar opening smoothly. The width of the column seems to change as it opens and there's a choppy jump. I've attempted to resolve this by applying transitions to the CSS and using flex-grow / shrink. Unfort ...