Having several horizontal scrollbars at once on a single webpage

I stumbled upon a great example showcasing how to scroll a div on my page:

Check out this Codepen - https://codepen.io/zheisey/pen/xxGbEOx

Although, I am looking to extend this functionality to allow multiple divs to scroll together. Any suggestions?

I attempted adding '.scrolling-wrapper2', but unfortunately, the scrolling function didn't work on my second wrapper.

const slider = document.querySelector('.scrolling-wrapper, .scrolling-wrapper2');

Find the full code below -

  <div class="scrolling-wrapper">
  <div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div>
  </div>

  <div class="scrolling-wrapper scrolling-wrapper2">
  <div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div><div class="card"><h2>Plain Text</h2></div>
  </div>

<style>
.scrolling-wrapper {
  display: flex;
  flex-wrap: no-wrap;
  overflow-x: auto;
  cursor: grab;
}

.scrolling-wrapper.active {
  cursor: grabbing;
}

.scrolling-wrapper[data-dragging="true"] a {
  pointer-events: none;
}

.card {
  color: white;
  background-color: tomato;
  font-family: monospace;
  width: 200px;
  height: 200px;
  margin-right: 1rem;
  flex: 0 0 auto;
  /* Centering text only */
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
</style>

<script>
const slider = document.querySelector('.scrolling-wrapper, .scrolling-wrapper2');
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
  let rect = slider.getBoundingClientRect();
  isDown = true;
  slider.classList.add('active');
  // Get initial mouse position
  startX = e.pageX - rect.left;
  // Get initial scroll position in pixels from left
  scrollLeft = slider.scrollLeft;
  console.log(startX, scrollLeft);
});

slider.addEventListener('mouseleave', () => {
  isDown = false;
  slider.dataset.dragging = false;
  slider.classList.remove('active');
});

slider.addEventListener('mouseup', () => {
  isDown = false;
  slider.dataset.dragging = false;
  slider.classList.remove('active');
});

slider.addEventListener('mousemove', (e) => {
  if (!isDown) return;
  let rect = slider.getBoundingClientRect();
  e.preventDefault();
  slider.dataset.dragging = true;
  // Get new mouse position
  const x = e.pageX - rect.left;
  // Determine distance the mouse has moved (new mouse position minus initial mouse position)
  const walk = (x - startX);
  // Adjust the scroll position of the slider from the left (amount the mouse has moved minus the original scroll position)
  slider.scrollLeft = scrollLeft - walk;
  console.log(x, walk, slider.scrollLeft);
});
</script>

Answer №1

Update

  <div class="scrolling-wrapper2">

to

  <div class="scrolling-wrapper2 scrolling-wrapper">

There is no need to create a new class for the second <div>, but if you decide to do so, make sure to include the appropriate CSS.

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

jquery single click doesn't trigger on mouse press or hover

http://jsfiddle.net/vd6vG/ When you press the mouse button over the td's, they change color to red. However, when you click on them, nothing happens. This leads me to believe that a click event does not trigger a mousedown event. How can I make it w ...

Issue encountered while transferring files between a web platform and an API

Currently, I am working on two separate projects. The first project involves a website where users can upload files, and the second project is an API designed for file uploads due to the limitations of the old website. I am utilizing Laravel (6) to develo ...

Searching for a jQuery plugin that can dynamically rearrange tables while automatically updating their corresponding IDs

Is there a way in jQuery to dynamically move tables around on a webpage? Currently, I have implemented a button that clones a hidden table when needed and another button to delete unwanted tables. Now, I am looking to incorporate a feature that allows the ...

Having problems with Javascript and CSS not playing well together?

I have implemented a button from this source, but it does not appear correctly on my page. You can view the screenshot here. It seems like there is a conflict between the saved changes and the CSS. How can I resolve this issue? In addition, I am facing ...

Explore the nearest sibling element, then switch the class with toggleClass command

I'm attempting to showcase a variety of categories for users to select multiple items from on a webpage. When a category is clicked, a checkbox is marked. However, I'm encountering an issue where the covering mask's class cannot be found upo ...

Attempting to create a JavaScript class within an HTML document

Having an issue with instantiating a JavaScript class in a separate HTML file. The JavaScript class looks like this: class Puzzle { constructor(fenStart, pgnEnd) { this.fenStart = fenStart; this.pgnEnd = pgnEnd; } } module.exports = Puzzle; ...

"Discover the process of sending a JSON value from a PHP page to an HTML page using AJAX, and learn how to display the resulting data

I am currently validating an email ID using PHP and AJAX, with the intention of returning a value from the PHP page to the HTML in JSON format. My goal is to store this return value in a PHP variable for future use. All of this is being executed within C ...

What is the best way to assign an active class to a specific footer ID on an HTML page using AngularJS?

I tried using the activeLink directive to apply an active class to a specific id on the page, but it didn't work as expected. .directive('activeLink', ['$location', function (location) { return { restrict: 'A', ...

Importing JavaScript into an Angular component: A beginner's guide

Within my Angular-11 project, I have included the following JavaScript file: "node_modules/admin-lte/plugins/bs-stepper/js/bs-stepper.min.js", I have added it to the angular.json configuration as detailed above. import Stepper from '.. ...

Displaying nested JSON data in a user interface using React

I have a complex nested JSON structure that I am using to build a user interface. While I have successfully implemented the first part, I am encountering difficulties with the second part. My Objective The nested JSON displays parent elements and now I a ...

Looking for guidance on sending data from a JS file to HTML using Nodejs? Seeking advice on various modules to achieve this task effectively? Let's

Looking for advice on the most effective method to transfer data from a JS file (retrieved from an sqlite db) to an HTML file in order to showcase it in a searchable table. My platform of choice is NodeJS. As a beginner, I am willing to put in extra time a ...

What is the best way to add border-radius to an image while incorporating padding?

I am facing an issue with images that have white padding at the top and bottom. Please refer to the attached image. Whenever I try to apply the border-radius property to these images, the edges appear distorted. Is there a way to resolve this problem wit ...

What is the process for incorporating a new task into my HTML/CSS/JavaScript to-do list application when the Enter key is pressed?

Currently, I am working on developing a to-do list application using HTML, CSS, and JavaScript. The application includes a text input field for users to enter their tasks, along with an "Add Task" button. However, I want to enhance the user experience by a ...

What are the steps for implementing responsive design animation in my particular scenario?

Can someone please help me with a strange bug in Chrome? I am trying to create a responsive design for my app. The width of the 'item' element should change based on the browser's width. It works fine when the page first loads in Chrome, b ...

Is there a way to set the background of the first sibling element to blue, but change it when another sibling element is hovered over?

I have a list of names: <div>Timothy Gruns</div> <div>Lawrence Fishly</div> <div>Jackson Crolya</div> What I want is for the background color to change to blue when any name is hovered over. However, if no names are be ...

It appears as though the promise will never come to fruition

I am currently developing an application that is designed to search for subdomains related to a specific domain and store them in a database. The application retrieves data from crt.sh and threatcrowd. One of the functions within the application parses th ...

The Material UI Menu does not close completely when subitems are selected

I am working on implementing a Material UI menu component with custom MenuItems. My goal is to enable the closure of the entire menu when clicking outside of it, even if a submenu is open. Currently, I find that I need to click twice – once to close the ...

In IE9, users can select background elements by clicking on specifically positioned links

Trying to turn an li element into a clickable link by overlaying it with an a element set to 100% height and width. While this solution works in Chrome and FF, IE9 is causing issues as other elements behind the link remain selectable, rendering the link un ...

Struggling to Enforce Restricted Imports in TypeScript Project Even After Setting baseUrl and resolve Configuration

I am facing challenges enforcing restricted imports in my TypeScript project using ESLint. The configuration seems to be causing issues for me. I have configured the baseUrl in my tsconfig.json file as "src" and attempted to use modules in my ESLint setup ...

The image remains unchanged in the JavaScript function

My attempt to swap images using jQuery has hit a snag. Upon running the page, it appears that the chase() method finishes executing before the animation has completed. The goal was to create an illusion of chasing between two images by repeatedly replaci ...