Reduce the speed of horizontal scrolling in a div element

I have a container with 3 lines of text, and each line is wider than the container itself. This causes a horizontal scrollbar to appear, but I've hidden it. My goal is to slow down the scrolling speed for this particular element.

Here is a fiddle: https://jsfiddle.net/ehp3qnty/576/

Here is the code:

.parent {
  width: 400px;
  height: 200px;
  border: 1px solid #aaa;
  overflow: hidden;
}

.child {
  height: 100%;
  margin-bottom: -50px;
  /* maximum width of scrollbar */
  padding-bottom: 50px;
  /* maximum width of scrollbar */
  overflow-y: hidden;
  overflow-x: scroll;
}

p {
  white-space: nowrap;
}
<div class="parent">
  <div class="child">
    <p>
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
      sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
      rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
      At vero eos et accusam et justo duo dolores et ea rebum.
    </p>
    <p>
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
      sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
      rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
      At vero eos et accusam et justo duo dolores et ea rebum.
    </p>
    <p>
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
      sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
      rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
      At vero eos et accusam et justo duo dolores et ea rebum.
    </p>
  </div>
  <!-- .child -->
</div>
<!-- .parent -->

How can I achieve slower scrolling?

I'm looking for a solution using pure CSS or JavaScript without any external plugins or libraries.

Answer ā„–1

$(document).ready(function(){
    $('#your_div').hScroll(150); // Adjusted scrolling amount
});
Behold the newly improved version of jquery.hscroll.js:

jQuery(function ($) {
    $.fn.hScroll = function (amount) {
        amount = amount || 150;
        $(this).bind("DOMMouseScroll mousewheel", function (event) {
            var oEvent = event.originalEvent, 
                direction = oEvent.detail ? oEvent.detail * -amount : oEvent.wheelDelta, 
                position = $(this).scrollLeft();
            position += direction > 0 ? -amount : amount;
            $(this).scrollLeft(position);
            event.preventDefault();
        })
    };
});

Answer ā„–2

There are numerous methods to accomplish this task. One straightforward approach is to elongate the scroll length and adjust the content position relative to the new scroll.

HTML:

<div class='container'>
  <div class='content'>
    <p>...</p>
    <p>...</p>
    <p>...</p>
  </div>
</div>

If w represents the container width and s is the content width (or original scroll width).

To slow down the scroll by a factor of f, the scroll must be made f times longer. The new scroll width will be s * f.

When reaching the end of the scroll, the scrollLeft will be s * f - w. And the left position of the content should be s * f - s.

Therefore, the ratio will be calculated as (s * f - s) / (s * f - w).

JavaScript:

let f = 5
let s = container.scrollWidth

container.addEventListener('scroll', () => {
  let w = container.offsetWidth
  let r = (s * f - s) / (s * f - w)
  let x = container.scrollLeft
  content.style.transform = `translateX(${x * r}px)`
})

This solution may not be perfect but hopefully, it provides some assistance.

View a live example here.

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

Achieve a disabled scroll bar feature on Firefox

I am experiencing an issue with a Javascript tabbed dialog where the pages have varying heights. Some of them are larger than the browser window. In Internet Explorer, there is always a scrollbar present on the right side. When it is not needed, it appear ...

Utilize Pseudo Elements for Designing a Unique Background Overlay

I am aiming to create a div with a customizable background, and then utilize a pseudo element to generate a semi-transparent white overlay that will lighten the background of the div. The crucial requirement is for the "overlay" to appear beneath the conte ...

Converting a JavaScript function to asynchronous with callback functionality

In the past, I had implemented certain methods in MyClass: MyClass.prototype.method1 = function(data1) { return this.data111.push(data1); }; MyClass.prototype.method2 = function(i) { var data = this.method1(i); if (data.condition1 != null ...

Tips on deleting a section of text inside a div

I have a DataTables implementation that is displaying an input inside a label element like this: <label> Search: <input type="search" class="" placeholder="" aria-controls="example"> </label> My goal is to remove the "Search:" t ...

What is the best way to align a center column of a precise width that is responsive to different screen

As much as I hate to ask for help with a "how do I do this" question, I'm at my wit's end trying to figure it out on my own. I'm working on a page layout that requires a center column of exactly 960px width to be horizontally centered on th ...

Retrieve all default and user-defined fields associated with contacts in the CRM 2011 system

In CRM 2011, the contact entities come with a variety of default fields, and I have also included some custom fields. My goal is to retrieve all field names in a JavaScript list. When creating an email template in CRM, you can select fields from a dialog. ...

Is there a way to animate a div using toggle?

I attempted to integrate Jquery UI in order to apply animation effects using the toggleClass function. However, I encountered an issue - the animation only occurs when adding the class, but not when removing it (or reverting back to its original position). ...

session failed to register the third variable

I have successfully implemented a login system in codeigniter. However, I am facing an issue where the session is not storing the user_type variable when I log in. I have reviewed my code multiple times but cannot seem to figure out what is causing this pr ...

transmitting comfort through events

To enhance my application, I am working on publishing a solace message to a topic and then passing it along to another part of the app for further processing using events. This entire process is happening within a single node.js process. While I understand ...

Modifying the cursor appearance during object dragging is a simple task that can enhance

html .container, .container * { cursor: url('img/arrowPointer.png'), auto; } javascript $('html').on('dragstart', function () { $('html').addClass('container'); }); $('html').on('d ...

Steps for executing a function within a fs.stat callback

Utilizing npm papercut for image uploading has been a game-changer. With the following function, saving an image is flawless: uploader.process('image1', file.path, function(images){ console.log(images.avatar); // '/images/uploads/image1 ...

Using PHP and jQuery to add a class to an array

I am having trouble making the desks that are available stand out based on the information from a form that includes the day, start time, and end time. Although I can display all the available desks, I am struggling to implement the jQuery functionality. ...

"Enhance your browsing experience with Chrome by automatically changing the background when

I am currently using Chrome with a CSS3 background. <h4 class="title-name clearfix"><a href="****************">Fairy Tail - LUCY</a> </h4> My issue is that when I hover my mouse over the title, the ba ...

Tips for using AngularJS to upload files to Cloudinary

I'm a newcomer to Cloudinary and Iā€™m looking to enhance my web application by uploading images using AngularJS. However, I've been struggling to find a comprehensive example that demonstrates how to integrate Cloudinary with AngularJS effective ...

Header alignment issue in JQGrid

Utilizing 4.4.4 - jQuery Grid and <= jquery 1.8.2, this is how I set up my jqgrid: function FAGrid() { var url1 = 'URL'; $("#FAList").jqGrid({ url: url1, datatype: 'json', mtype: 'POST', ...

Step-by-step guide for implementing tooltips using jQuery

I've been attempting to implement a tooltip using jQuery UI. However, when I use var_dump I am not getting any output. Here is the code snippet: <a href="#"><span id='11111_22222'>text_here</span></a> And this is ...

JQuery Keyup event is not functioning as expected

For my blog navigation, I have set up an event where pressing the 'J' key takes me to the previous post and the 'K' key takes me to the next post. However, I am facing an issue where the event works initially but stops working after the ...

What could be causing the script to display the object's content inaccurately?

Below is the code for the client side: import {useEffect,useState} from 'react'; import io from 'socket.io-client'; import Peer from './Peer'; export default function TestMeeting(){ let peerName; const [peerList,setPee ...

Issue with implementing setCallBack for updating variant prices in Shopify

I'm currently attempting to update the price of a product when a variant is chosen. I've tried implementing the following code, but haven't been successful in getting it to function properly. The script has been added to the theme.liquid fi ...

Executing a JavaScript function without passing arguments does not yield the desired result

Within my JS file, I encountered an issue when trying to call one function from another. Initially, I called the function with no arguments using only its name handleResponse. However, when attempting to add arguments by changing the function signature, I ...