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

Error found in Google's privacy page HTML code

Reviewing the html code of Google's Privacy Page, I observed the following header: <!DOCTYPE html> <html lang="en"> <meta charset="utf-8> <title>Google Privacy Center</title> <link rel="stylesheet" href="//www.g ...

Differentiate between minimum and maximum values on ion-range sliders with discrete color options

I am currently working on customizing the theme of my ionic app, specifically trying to assign different colors to the min and max knobs on an ion-range component. At the moment, I am only able to apply a single HTML property called "color" to the selector ...

Positioning a centered div with absolute positioning may result in offset issues on Android devices when the content exceeds the viewport size

Encountering a peculiar issue on Android browsers, particularly when the following elements are present on a page: A div element larger than the device's viewport size. (In this case, set at 1200px.) One or more other div elements styled for centeri ...

Arranging asynchronous functions using async/await in Node.js/JavaScript

When it comes to organizing my code in js/nodejs, I frequently rely on this pattern. (async function(){ let resultOne = await functionOne(); let resultTwo = await functionTwo(); return { resultOne: resultOne, resultTwo: resul ...

Uploading an image via Chrome and transferring it to a server using PHP: a step-by-step guide

In my web application, I am looking to allow users to easily upload images to a server. My idea is for the user to simply paste the image into their Chrome browser (e.g. a print screen), then have the image stream posted to a PHP page where it will be conv ...

Obtaining table data and HTML elements using the correct code - Selenium and Python

I've been attempting to extract the ticker symbol, stock name, price, sector, and market cap columns from this website. I'm facing challenges in identifying the correct HTML elements using the appropriate code. Although I've used Selector Ga ...

Creating diverse options using attribute-value pairs

I am in need of creating an array that contains all possible combinations of attribute values. Here is an example of my attributes/values object: let attr = { color: ['red', 'green', 'blue'], sizes: ['sm&apo ...

Utilizing FCKEditor to incorporate dimensions of width and height into image elements

I'm currently facing an issue while attempting to specify width and height for images within an old WYSIWYG FCKEditor. The challenge arises when trying to retrieve the naturalWidth/naturalHeight properties, as they return values of 0. What could I be ...

Tips for implementing a CSS override for a block element's relative date

I am looking to customize this source code by using a CSS override to ensure that the "lightning-relative-date-time" element is not utilized. I want to keep it displaying the timestamp information by default. Can you guide me on how to achieve this throu ...

Tips for concealing validation errors in React Js when modifying the input field

I have recently started working with ReactJs, and I've implemented form validation using react-hook-form. After submitting the form, the errors are displayed correctly. However, the issue arises when I try to update the input fields as the error messa ...

What is the best method to calculate the total of multiple input values from various cells and display it in the final cell of an Angular table?

Hey there! I have a challenge where I need to calculate the sum of input values for each cell and display it dynamically in the last cell of the row. Take a look at the image below: https://i.stack.imgur.com/0iKEE.png In the image, you can see that the nu ...

List style image does not serve its intended purpose

I attempted to personalize my webpage by adding an image to a list, but I couldn't get the list-style image to work. You can view the page at Below is the code I used: CSS /* Fleet List */ ul#flotta li { list-style-image:url("http://ctp.serviziew ...

Issue with ReactiveVar causing React component not to re-render

I am a beginner with React in Meteor. To summarize: When I change the value of my ReactiveVar in my data container, the view does not update. Here is the code snippet: import React, { Component, PropTypes } from 'react'; import ReactDOM from & ...

Send the output of MPDF back to the browser by utilizing JSON in combination with ExtJS

I am currently using mpdf to generate a PDF report in my PHP code. I have successfully been able to save the PDF file and view it by using Output($pdfFilePath), but now I need to send it back to the browser using JSON without saving it on the server. To ac ...

Using the map function with a React onClick handler

After tirelessly scouring the internet for answers to my query, I've hit a dead end. My goal is to implement a basic react click handler on my button, but for some reason, it just won't cooperate. It's likely something incredibly straightfor ...

Querying Techniques: Adding an Element After Another

CSS <div id="x"> <div id="y"></div> <div> <p>Insert me after #y</p> The task at hand is to place the p tag after '#y', and whenever this insertion occurs again, simply update the existing p tag instead of ...

The replacer argument of the JSON.stringify method doesn't seem to work properly when dealing with nested objects

My dilemma is sending a simplified version of an object to the server. { "fullName": "Don Corleone", "actor": { "actorId": 2, "name": "Marlon", "surname": "Brando", "description": "Marlon Brando is widely considered the greatest movie actor of a ...

Building a family tree with JavaScript's Document Object Model

My goal is to use jQuery to construct the following DOM setup: <li> <label> user <input type=radio> </label> </li> Below is the jQuery code I am trying to use. $('<input>') .attr({type:'radio&ap ...

What is the best way to alter an HTML element using Ruby with Sinatra and Bootstrap?

Below is the content of my index.erb file: <a class="btn" href='javascript:TestFunction()' role="button">TestFunction</a> <script language="javascript"> function TestFunction() { $.post("test_function", { action ...

Guide on using JavaScript to automatically scroll a HTML page to the top on any mobile browser

Can JavaScript be utilized to smoothly scroll an HTML page to the top? I am looking to achieve this with a stylish animation that functions correctly on all mobile browsers. jQuery is the library I am using on this particular page. Thank you, ...