Is there a way to effortlessly scroll an element when the CSS overflow property is designated as auto?

I need help with creating a testimonial section where two divs automatically scroll inside a fixed-height container.

Currently, only one div is scrolling while the other remains static in my code.

I have implemented JavaScript for automatic scrolling within one div, but it doesn't seem to work for the second div.

Answer №1

In this code snippet, there are multiple errors related to variable names and duplicates. Specifically, having two onLoad properties on the body tag is incorrect, and setting scrollTop as a variable is also erroneous since it is an attribute of the respective element.

let scrollRate = 100;

function initialize() {
  initScrollDiv();
  initScrollDiv2();
}

function initScrollDiv() {
  let divElement = document.getElementById("MyDivName");
  let reachedMaxScroll = false;
  
  divElement.scrollTop = 0;
  let previousScrollTop = 0;
  
  let scrollInterval = setInterval(function() {
    scrollDiv();
  }, scrollRate);
}

function scrollDiv() {
  // logic for scrolling
}

// More functions...

Document

Answer №2

ScrollRate = 50;

function initScrollDiv() {
  DivElmnt = document.getElementById("MyDivName");
  DivElmntSecond = document.getElementById("MyDivName2");

  ReachedMaxScroll = false;
  ReachedMaxScrollSecond = false;

  DivElmnt.scrollTop = 0;
  DivElmntSecond.scrollTop = 0;

  PrevScrollTop = 0;
  PrevScrollTopSecond = 0;

  ScrollInterval = setInterval("scrollDiv()", ScrollRate);
}

function scrollDiv() {
  if (!ReachedMaxScroll) {
    DivElmnt.scrollTop = PrevScrollTop;
    PrevScrollTop++;

    ReachedMaxScroll =
      DivElmnt.scrollTop >= DivElmnt.scrollHeight - DivElmnt.offsetHeight;
  } else {
    ReachedMaxScroll = DivElmnt.scrollTop == 0 ? false : true;

    DivElmnt.scrollTop = PrevScrollTop;
    PrevScrollTop--;
  }

  if (!ReachedMaxScrollSecond) {
    DivElmntSecond.scrollTop = PrevScrollTopSecond;
    PrevScrollTopSecond++;

    ReachedMaxScrollSecond =
      DivElmntSecond.scrollTop >=
      DivElmntSecond.scrollHeight - DivElmntSecond.offsetHeight;
  } else {
    ReachedMaxScrollSecond = DivElmntSecond.scrollTop == 0 ? false : true;

    DivElmntSecond.scrollTop = PrevScrollTopSecond;
    PrevScrollTopSecond--;
  }
}

function pauseScrollDiv() {
  clearInterval(ScrollInterval);
}

function resumeScrollDiv() {
  PrevScrollTop = DivElmnt.scrollTop;
  PrevScrollTopSecond = DivElmntSecond.scrollTop;

  ScrollInterval = setInterval("scrollDiv()", ScrollRate);
}
body {
  /* background-color: #ffffff; */
}
.container {
  /* ... */
}

... (CSS content shortened for brevity)

<!DOCTYPE html>
<html lang="en">
  <head>
    ... (Head content, stylesheets and meta tags)
  </head>
  <body onLoad="initScrollDiv()">
    <div class="container mt-5">
      ... (Main content with test boxes, cards, images, text)
    </div>
    <script src="script.js"></script>
  </body>
</html>

This unique code snippet helped me achieve my desired functionality successfully.

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

Generate a JSON file using Node.js

I've been honing my skills with Node.js and express by working on a project involving a JavaScript object that generates a dropdown list. The process has been smooth so far. Recently, I integrated mongoose to create a model and saved it. Now, in my co ...

My goal is to generate four HTML buttons that trigger specific functions: addition, subtraction, multiplication, and division

I am currently learning JavaScript, and I am facing some challenges with my assignment. The task is to create four buttons in HTML that trigger different functions - addition, subtraction, multiplication, and division. The goal is for the user to input two ...

Unable to successfully create a dynamic anchor tag and open it in a new tab

I am receiving a PDF link in an AJAX response and I would like to manually trigger the download. Here is the code that I am currently using: var fileLink = document.createElement('a'); fileLink.href = responseData.PDF_LINK; fileLink.setAttribute ...

Next.js manages 0Auth authentication using MoneyButton for authorization

I have been working on implementing 0Auth user authorization for my Next.js application using the MoneyButton API. Successfully, I am able to initiate the authorization request with client.requestAuthorization('auth.user_identity:read','http ...

Steps to reposition an element with absolute positioning to the upper left corner of the screen

I am currently in the process of creating a hamburger menu without using JavaScript, which should drop down from the top to the bottom when clicked. The burger menu is situated at the top right corner of the screen, but every time I try to drop down the na ...

Is it possible for me to utilize code blocks within the head content and link tags to make reference to specific themes

I am currently in the process of developing an ASP.NET application using themes. I have successfully assigned a theme to the application via the web config file. One issue I am facing is trying to reference a bookmark icon located within the themes direct ...

Unable to view images that have been uploaded through express.js

In my current project with a REST Api, I have been facing an issue while uploading images. The folder structure of the project can be seen here: https://i.sstatic.net/1PN0q.png Under the public folder, there is a subfolder named images where all the image ...

Ensuring the footer stays at the bottom of the page using Tailwindcss and ReactJS

I've read numerous articles and posts about achieving this, but none of the methods seem to be effective for me. My goal is to have the footer stick to the bottom of the page when viewed on mobile devices. It displays correctly on larger screens, but ...

Restangular is throwing an error because it cannot find the reference for _

I encountered an issue with Restangular where I'm getting the error message: Uncaught ReferenceError: _ is not defined from restangular when trying to use it. Code Snippet: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angula ...

Organizing and recycling React styled-components and native elements

As a backend engineer transitioning to frontend development, I am currently learning React and exploring styled-components for styling components. However, I am facing challenges in using styled-components with native React components and updating them in ...

I'm completely baffled as to why the client console is unable to locate my JS file

I have a question that may seem basic, so please bear with me! I'm having trouble adding a js file to an html page and it seems to be related to the file path. In my HTML page, I have this code to link the file: <script src="../src/utils/mapbo ...

What is the significance of the "rc" within the version structure of an npm package?

Can someone help me understand what the rc in 2.2.0-rc.0 signifies? I'm curious if it indicates that this version is ready for production use. ...

What is the best way to alter the text of a button when the mouse hovers over it?

I'm looking to create a button that can dynamically change the text inside it when the cursor hovers over it, and then revert back to the original text when the cursor moves away from it. I attempted this in VScode using "document.getElementById().inn ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Checkbox does not appear in Internet Explorer

I'm sorry, but I have never encountered this issue before. It seems that my checkboxes are not showing up in Internet Explorer but they do in Firefox. I am unsure why this is happening as I don't know of another way to create checkboxes. &l ...

Generating a PDF file from an HTML and CSS page can be done without relying on APIs or libraries, by leveraging the

Is there a way to directly convert an HTML page generated using PHP and CSS into a PDF format without the use of libraries? I have come across multiple libraries that can accomplish this task, but I would prefer not to rely on any external libraries and i ...

The Shopify Pixel Extension has encountered an issue - error code 1

Looking to develop a web pixel extension for my Shopify app, I followed the official guide: While building the app, encountered this error: extensions | my-app-pixel (C:\projects\shopify\my-app-pixel\node_modules\.bin\shopify ...

The menu-item is unable to be centered in its location

I've exhausted all my efforts, but still can't seem to center this menu-item. Here is the code I've been working with: <div class="nav nav-tabs mb-2" id="nav-tab" role="tablist"> <a class="nav-item nav-link active" id="na ...

What is the best way to reduce the width of the preformatted tag?

Is there a way to adjust the width of the pre tag in order to avoid affecting the q-card when resizing the browser window? I am trying to create a q-card that can display code similar to what you see in ChatGPT. However, the q-card's size is not respo ...

Is it achievable through CSS alone to eliminate the bullet point in a UL list when it consists of just a single item?

Is it feasible to achieve a solution using CSS alone that remains functional even on IE8, ensuring the following criteria: If a <ul> list contains 2 or more items, then maintain the bullets in front of each item as per usual. When there is only one ...