Adjust Element Width Based on Scroll Position

I'm attempting to achieve a similar effect as seen here: (if it doesn't work in Chrome, try using IE).

This is the progress I've made so far: http://jsfiddle.net/yuvalsab/op9sg2L2/

HTML

<div class="transition_wrapper">
    <div class="transition">1</div>
    <div class="transition">2</div>
    ...
</div>
...
<div class="pagewrap">
    <div class="some_text_1">Lorem ipsum dolor sit amet.</div>
    <div class="some_text_2">Lorem ipsum dolor sit amet.</div>
    ...
</div>
...

CSS

div[class^="some_text"] {   
    padding:50px 0;
    border-bottom:1px solid;
}
div.transition_wrapper {
    width:100%;
    position: fixed;
    left:100%;
    background:red;
}
.transition {
    background: red;
    position: absolute;
    width:100%;
}

jQuery

function animate(element) {
    var lastScrollTop = 0;
    ...
});

jQuery(document).ready(function(){
    jQuery('.transition_wrapper .transition').each(function() {
        var element = jQuery(this);
        ...
});

Answer №1

Here's a solution that does the job well. It could use some polishing, but it gets the job done.

http://jsfiddle.net/example/1a2b3c4d/

$(document).on('scroll', function(e) {
    var bodyHeight = $(document).height()
    var windowHeight = $(window).height();
    var scrollTop = $(document).scrollTop();
    var whereAmI = scrollTop + windowHeight
    
    //Ensuring correct calculation of percentage
    if (scrollTop < windowHeight) {
        var heightPercentage = scrollTop / bodyHeight * 100;
    } else {
        var heightPercentage = whereAmI / bodyHeight * 100;
    }

    $('.scroll-progress').css('width', heightPercentage + '%');
    $('.progress').text(Math.round(heightPercentage));
});

This code is relatively straightforward. Essentially, it tracks your position in the scroll and updates .scroll-progress with new width and text information.

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

Utilize Vuetify to showcase a vertical layout consisting of a header and a v-card, occupying the full height

<template> <div> <div style="height: 20px; color: yellow"> test </div> <v-card class="markdown-preview" tile> <v-card-text v-html="previewText"> </v-card-text> ...

Whenever I attempt to use for or while loops in my application, it consistently crashes

I've recently started learning reactjs and I'm working on a simple app. I created a decrement button that should only work if the item count is greater than or equal to zero. However, when I tried using while and for loops in my code, my app cras ...

ShadowBox replacement for IE8

After much experimentation, I am on the verge of achieving an inset box shadow for IE8 without relying on JavaScript. Take a look at the screenshot below: https://i.sstatic.net/2kM3R.png Since Internet Explorer versions 5.5 through 8 only support Micros ...

Adjusting the page size in the material table to display all items

I am currently working with React and Material Table. Objective My goal is to provide the user with the option to set the pageSize to 'All' in order to display all rows in the material table. Steps Taken I utilized the useState hook to create ...

What is the best way to arrange these divs one on top of another?

I have combed through numerous resources but still haven't found a solution to my problem. I am struggling with figuring out how to stack the "top_section" div on top of the "middle_section" div. Currently, "middle_section" is appearing above "top_sec ...

How can I turn off autocomplete in MUI textfields?

Currently, I am working with the latest version of mui. Within my user contact info form, there is a zip code field that I do not want to be auto completed if the value is null. However, despite my efforts, it continues to autocomplete with the email saved ...

Struggling to understand the relationship between SetInterval and closures

Is there a way to continuously update the contents of a div using setInterval I found inspiration from this question on Stack Overflow: How to repeatedly update the contents of a <div> by only using JavaScript? However, I have a couple of questions ...

Dividing image styles within css

I am trying to incorporate 4 arrow images into a CSS class (up, down, right, left). I started with a basic arrow css class: .arrow { background-repeat: no-repeat; background-position: center; } Next, I created subclasses like: .arrow .up { ...

PHP file upload error: Angular JS form submission issue

I am currently working on creating an upload method using Angular and PHP. Here is what I have come up with so far... HTML <form class="well" enctype="multipart/form-data"> <div class="form-group"> <label for ...

Is there a way to incorporate a component into Particle.js?

I attempted to encase the Particle around the component but it's not functioning correctly import React from "react"; import { render } from "react-dom"; import Particles from "./Particles"; import "./index.css" ...

What is the best way to combine two arrays into a single array using AngularJS?

Here is a code snippet: $scope.studentDetails=[]; $scope.studentDetails=[0][id:101,name:one] [1][id:102,name:two] [2][id:103,name:three] $scope.studentMarks=[]; $scope.studentMarks=[0][id:101,marks:78] ...

How to Retrieve the Following Element in a Table Using Javascript

https://jsfiddle.net/en6jh7pa/1/ I am encountering an issue with accessing the next element, as it is returning null. When I pass "this" as the onclick parameter, I expect to be able to grab the next element using this keyword. However, it seems to be re ...

The concept of asynchronicity and callbacks in JavaScript

As a newcomer to the world of JavaScript and Stack Overflow, I have been learning about synchronous, asynchronous, and callbacks through various videos and blogs. However, I still have a lingering doubt. If synchronous code means following a sequential ord ...

Tips for accessing the ID in a specific row of a datatable

My module displays a list of customer details in a datatable with a button for action. Why am I unable to retrieve the id of the button added to each row? When I click the button, no alert is displayed. What could be causing this issue? Below is my ajax ...

An issue has occurred: TransformError SyntaxError: Unexpected keyword 'const' was encountered

While practicing programming with React-Native, I encountered a problem that I couldn't figure out how to solve. I attempted to use solutions from various forums, but none of them worked. import { StyleSheet, Text, View, Image } from 'react-nativ ...

Ensure each alternate div has a unique background hue

While attempting to use CSS to assign a different background color to every second div element, I encountered an issue where both divs were affected when using (odd) and none when using (even). How can this be explained? .hoverDiv:nth-child(odd) { ba ...

What are the best methods for concealing the URL or video source in JWPlayer 7 or Flowplayer version 6.0.5?

Is there a way to conceal the URL of a video from being viewed in the browser's inspect element? I'm looking for a method to encrypt it and prevent IDM from downloading the video. flowplayer("#fp-hlsjs", { key: "$**********", logo: "<?= Y ...

Updating several inputs programmatically in VueJSLet's explore how to

I am working on a form that requires updating multiple other fields when one field is updated. For instance, I have a contact name field and depending on the name entered, I need to update the email and phone number fields as well. <template> < ...

Leverage ConvexGeometry within the npm release of THREE

Is there a way to utilize ConvexGeometry in the npm version of THREE? It appears that it was introduced in r85: https://github.com/mrdoob/three.js/releases I noticed that it's only present in the example folder. Is there a method to incorporate it ...

The functionality of jQuery's toggleClass is not functioning properly when clicking on a different thumbnail

My objective: I want to display a set of thumbnails. When a thumbnail is clicked, I want to toggle the hiddenElement class and show a large image inside a hidden div. If anywhere in the DOM is clicked, it should hide the current image or allow the user t ...