Carousel with Horizontal Scrolling (Pause)

I stumbled upon this incredible "Pause Scroll (Horizontal) Parallax" created by Khaled on Codepen.

One thing that has been puzzling me is how to make it run multiple times. It appears that when you try to apply it more than once, the duplicates don't work properly, only the first one does. I've experimented with different approaches and researched about running a script multiple times on the same page, but I haven't found a solution that works.

Here is a replica of the initial link, where I simply duplicated the same HTML 3 times. I would greatly appreciate any help in adjusting the JS for this.

HTML

<div class="bumper">
  <h2>There should be a horizontal scroll area just below</h2>
</div>

<section class="container">
  <div class="space-holder">
    <div class="sticky">
      <div class="horizontal">
        <section role="feed" class="cards">
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
        </section>
      </div>
    </div>
  </div>
</section>

<div class="bumper">
  <h2>Scroll up to see a horizontal scroll section</h2>
</div>
...

CSS

*, *::before, *::after {
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-size: 100%;
}

body {
  margin: 0;
  font-family: sans-serif;
}

.bumper {
  text-align: center;
  padding: 128px 16px;
  background-color: #efefef;
}

.container {
  position: relative;
  width: 100%;
  min-height: 100vh;
}
...

JS

const spaceHolder = document.querySelector('.space-holder');
const horizontal = document.querySelector('.horizontal');
spaceHolder.style.height = `${calcDynamicHeight(horizontal)}px`;

function calcDynamicHeight(ref) {
  const vw = window.innerWidth;
  const vh = window.innerHeight;
  const objectWidth = ref.scrollWidth;
  return objectWidth - vw + vh + 150; // 150 is the padding (in pixels) desired on the right side of the .cards container. This can be set to whatever your styles dictate
}
...

Answer №1

I have revised the JavaScript code by implementing the forEach() method.

When referencing a collection, it is necessary to utilize a reference in the form of querySelectorAll.

const spaceHolder = document.querySelectorAll('.space-holder');
const horizontal = document.querySelectorAll('.horizontal');
const container = document.querySelectorAll('.container');
const sticky = document.querySelectorAll('.sticky');

function calculateDynamicHeight(ref) {
  const vw = window.innerWidth;
  const vh = window.innerHeight;
  const objectWidth = ref.scrollWidth;
  return objectWidth - vw + vh + 150;
}

container.forEach(function(current, i) {
  spaceHolder[i].style.height = `${calculateDynamicHeight(horizontal[i])}px`;
    window.addEventListener('scroll', () => { 
      horizontal[i].style.transform = `translateX(-${sticky[i].offsetTop}px)`;
    });
    window.addEventListener('resize', () => {
      spaceHolder[i].style.height = `${calculateDynamicHeight(horizontal[i])}px`;
    });
});
*, *::before, *::after {
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-size: 100%;
}

body {
  margin: 0;
  font-family: sans-serif;
}

.bumper {
  text-align: center;
  padding: 128px 16px;
  background-color: #efefef;
}

.container {
  position: relative;
  width: 100%;
  min-height: 100vh;
}

.space-holder {
  position: relative;
  width: 100%;
}

.sticky {
  position: sticky;
  top: 0;
  height: 100vh;
  width: 100%;
  overflow-x: hidden;
}

.horizontal {
  position: absolute;
  height: 100%;
  will-change: transform;
}

.cards {
  position: relative;
  height: 100%;
  padding: 0 0 0 150px;
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: center;
}

.sample-card {
  position: relative;
  height: 300px;
  width: 500px;
  background-color: #111f30;
  margin-right: 75px;
  flex-shrink: 0;
}
<div class="bumper">
  <h2>There should be a horizontal scroll section just below</h2>
</div>

<section class="container">
  <div class="space-holder">
    <div class="sticky">
      <div class="horizontal">
        <section role="feed" class="cards">
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
          <article class="sample-card"></article>
        </section>
      </div>
    </div>
  </div>
</section>

<div class="bumper">
  <h2>Scroll up to view the horizontal scroll area</h2>
</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 choose CSS class attributes using Mootools and the getStyle()

Seeking to duplicate an object, I am trying to figure out how to retrieve class CSS attributes from Mootools. css: .card { width: 109px; height: 145px; } html: <div id="cards"> <div class="card" id="c0"> <div class="face fron ...

What could be the reason for event.stopPropagation() not functioning properly with a switch statement

Could you please explain why the function event.stopPropagation() is not working on the switch element? Whenever I click on the switch, it prints the console log for the switch. However, when I click on the surrounding area (row), it logs the row event in ...

extracting the value of a chosen radio button in AngularJS using HTML

When attempting to retrieve the selected value from a radio button, I am encountering an issue where choosing "teacher" still shows as "student." Here is the HTML file: <!DOCTYPE html> <html ng-app="phase2"> ... (HTML code continues) Rige ...

Troubleshooting: Page Unable to Import/Execute Linked JavaScript on WebStorm with Node.js Backend

I've been following W3School's jQuery tutorial, but I'm encountering some issues with importing scripts to an HTML document hosted on a Node server in WebStorm. I have properly installed and enabled the jQuery libraries under Preferences &g ...

A guide on showcasing items on an html webpage through the use of javascript

Currently, I have a series of hardcoded HTML elements in my code snippet. <!-- Reciever Message--> <div class="media w-50 ml-auto mb-3"> <div class="media-body"> ...

Tips on placing an li element into a designated DIV

Just starting out with jquery and working on a slider project. Here's what I have so far: <ul> <li> <img src="image.jpg"><p>description of the current image</p></li> <li> <img src="image.jpg"> ...

CSS :hover problem, text appearing unexpectedly

I'm currently working on designing a logo that displays hidden text when hovered over, providing instructions to users. However, I've encountered an issue where the hidden text reveals itself even before the logo is hovered over. This is frustrat ...

Support for kendo UI is not available for jQuery version 1.10.1

Currently, I am integrating kendo UI controls into my application using jquery 1.8.3 or lower versions. I am curious to know if it's possible to access the controls with jquery 1.10.1 version. ...

Creating a custom navigation bar that elegantly fades away with a smooth animation as you scroll down the page is a must-have

How can I create a navigation bar that disappears when scrolling, with a smooth animation? This is the progress I have made so far. HTML: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css/style.css" type="tex ...

Checking if multiple text fields with identical names are valid using BootstrapValidator

I'm facing an issue where I am unable to validate multiple text fields with the same name. The validation only works for the first input text field, but not for the rest. <form id = "frm_org_id"> <input type="text" name="email_address[]" pla ...

The issue of background-attachment: fixed not functioning properly on Safari

Currently, I have the following code implemented on an element that extends 100% of the browser: #section_white { background-attachment:fixed; background-image:url(image_url_here.jpg); background-position:100% 100%; background-repeat:no-repeat no- ...

Yet another inquiry about the inexplicable failure of my jQuery Ajax in Internet Explorer

Just need to double-check if my test code below is correct. I've done some research and it appears that html syntax errors can cause the jquery ajax to not work in Internet Explorer? There are several includes where this code is located, so could the ...

Menu Toggle with Bootstrap 3

I have implemented two Bootstrap 3 navigation menus on a single page. One menu works fine when the toggle button is activated for responsiveness, but the other one doesn't work as expected. How can I resolve this issue? Below are the code snippets fo ...

A div element with the class name "draggable" is

One of my functions sends notifications to a page by prepending a main div with the notification. Here is the function: function displayNotification(notificationTitle, notificationContent, notificationColor, notificationSize) { console.log('Attem ...

Tips for Preventing Ant Design from Overriding Existing CSS Styles

Currently, I am working on a ReactJS project. Initially, the entire project was designed using pure CSS. However, I decided to incorporate a small Antd component into my project. Following the provided instructions, I imported the component like this in on ...

Tips for using JavaScript to consume a complex type returned by a WebMethod on the client side

I am new to Webmethods and Services and I find myself in a bit of a predicament here. The webmethod I have returns an instance of the StatusViewModel class. [WebMethod()] public static StatusViewModel GetTime1() { Debug.Write("Timer") ...

If using conditional SCSS, consider overriding the variables

When working with React state, I am passing a list and calling the component where needed. comments: { elementLabel: "Comments", elementType: 'textarea', className: "form-control", ...

Ways to sort data based on dropdown selection in MVC Razor

I am facing a requirement where I have a list of vendors that are displayed on the page load. Additionally, there is a dropdown menu which acts as a filter for these vendor records when its value changes. This is my view: <div class="half-left"> ...

Tips for incorporating a Font Awesome icon <i> within a select dropdown and customizing its appearance

I am facing an issue while trying to insert an icon into a select option. The error message I received is: Warning: validateDOMNesting(...): cannot appear as a child of <option> To indicate that certain fields are required, I am using asterisk ic ...

Adjust the image's brightness by using the value from the input range slider

I am currently utilizing fabricJS to manipulate images on a canvas. My goal is to allow users to increase or decrease the brightness of an image using a checkbox and a range selector. However, I'm encountering an issue where the image turns white when ...