Guide to navigating through an element with JavaScript

Would you be able to guide me on scrolling an element using JavaScript? I have developed a demo application where the scroll works when "left is positive" but not when it's negative.

I've set up two cases, with the first one working perfectly. Clicking the button scrolls to 100px. However, in the second case, I'm facing issues with scrolling in the negative direction. Here's the code snippet:

function handler(){
    document.querySelector(".p").scroll({
        left: 100,
        behavior: 'smooth'
    });
}

document.getElementById("button").addEventListener("click", handler, false);

function handler2(){
    document.querySelector(".p2").scroll({
        left: -100,
        behavior: 'smooth'
    });
}

document.getElementById("button2").addEventListener("click", handler2, false);

Link to view the code

  1. Case 1: Scrolls properly

*

1. 2. Case 2: Facing issues with scrolling

*

Any insights on how to tackle this scrolling problem?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  Case 1:
  <div class="p">
    <div class="b r">1</div>
    <div class="b g">1</div>
     <div class="b bl">1</div>
  </div>
  <button id="button"> move</button>
  <hr/>
  
  Case 2:
  <div class="p2">
    <div class="b r2">1</div>
    <div class="b g2">1</div>
     <div class="b bl2">1</div>
  </div>
  <button id="button2"> move left</button>
</body>
</html>

Answer №1

For those looking to create a carousel effect, it is more effective to utilize the transform: translate() function instead of relying on scrolling. https://jsbin.com/kiditahado/edit?html,css,js,output check out this modified snippet based on your original code to see how it can be done.

let start = 0;
function handler(step){
 const p = document.querySelectorAll(".p div");
  start+=step
  p.forEach(el=>{

  el.style.transform =`translate(${start}px,0)`

  })

}

Answer №2

The issue at hand is the inability to scroll into negative values. One potential workaround that comes to mind is reversing the blocks and scrolling from the right side.

Here is the updated CSS:

.p2{
  position:relative;
  width:100px;
  height:100px;
  overflow:hidden;
  left:50%;
}
.b2{
  width:100px;
  height:100px;
  position:absolute
}
.r2 {
  background-color:red;
  left:200px;
}

.bl2 {
  background-color:blue;
  left:100px;
}

.g2 {
  background-color:green;
  left:0px;
}

And the revised JS code:

function handler2(){  
  document.querySelector(".p2").scroll({
    left:100,
    behavior: 'smooth'
  })     
}

// start from the other side
document.querySelector(".p2").scroll({left:200});

document.getElementById("button2").addEventListener("click", handler2, false)

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

Passing a selected value from the child to the parent component through state in React using hooks

I'm currently working on a Dropdown component that includes a select tag. When the user changes the value in the select, the state is updated to reflect the selected option. The StyledDropdown component is used for styling the select element. const D ...

Tips on changing font color within an email body in Outlook using a C#.NET Windows application

While working on a c#.net windows application, I encountered an issue with setting font color for specific lines in the email body when sending mail to Outlook. Despite trying various methods, I was unable to find a solution. Below is a snippet of my code: ...

The JQuery autocomplete feature is providing a data array with labels and values, however, it is not displaying the labels as suggestions

Currently, I am implementing an AJAX call using Jquery-autocomplete on a WordPress website. The call returns a list of airports with their corresponding Airport ID as the value. However, I am encountering an issue where the input is not displaying anythin ...

Progress Bar Flexbox Animation - Width Issue

I'm struggling to implement a progress bar with animation. It seems to work fine when there is no inner text, but as soon as I add some text, the width calculations get messed up. I've experimented with different width() functions and tried addin ...

Encase the icon within a box within a Bootstrap alert

There is a strange border around the icon in the alert, as indicated by the title. Here is an image of what it looks like: https://i.sstatic.net/waavo.png However, everything appears fine elsewhere: https://i.sstatic.net/rJDq4.png This code file contai ...

Transforming a JavaScript chained setter into TypeScript

I have been utilizing this idiom in JavaScript to facilitate the creation of chained setters. function bar() { let p = 0; function f() { } f.prop = function(d) { return !arguments.length ? p : (p = d, f); } return f; } ...

Is it possible to generate a JSON file from a flowchart with the help of d3.js?

Situation: I'm currently in the process of developing a tool that will enable us to generate flow charts and then export the data into a JSON file for use in other services. Being new to JavaScript, I've come across d3 quite often. Can d3 handle ...

Mastering Puppeteer: Tips for Successfully Submitting Forms

Can you use puppeteer to programmatically submit a form without a submit input? I have been successful with forms that include a submit input by using page.click('.input[type="submit"]'), but when the form does not have a submit input, focusing o ...

Attempting to design a layout with flexible elements for the header, main content, and footer

https://i.sstatic.net/PURno.png I'm currently working on creating a layout that has specific boundaries for each section. The header and footer need to be centered vertically, while the content should extend to the bottom of the viewport. Please keep ...

How can I set the text to be in the center of a VML

For my email template, I need to center text over an image. Although I attempted a solution from this source, the image is not being displayed. Below is the code snippet: <div align="center" style="margin: 0; padding: 0;"> <table border="0" c ...

Wide website design doubled in size

I'm struggling to understand why the width of our website is so large. Take a look at it here: https://85.25.242.165/lunatic/default/index You'll notice that there's a horizontal scroll bar at the bottom and the overall width seems to be do ...

Is it feasible to have both the Probe and Zoom features activated at the same time on iScroll?

I am currently using iscroll-probe.js on my website to handle specific scroll-position functionality. However, I've encountered an issue where the zoom feature is not available with this script. If I switch to iscroll-zoom.js instead, the zoom functio ...

When I separate the div into its own line in the HTML/CSS code, the behavior changes significantly

I'm encountering an issue with my HTML/CSS structure: .im-wrapper { width: 100%; height: 100%; position: fixed; overflow: hidden auto; top: 0; left: 0; } .im-backdrop { background-color: var(--darkblack); opacity: 80%; width: 1 ...

Div with a fixed position that is displayed on top of another following div

Having trouble with my code on this fiddle: https://jsfiddle.net/0vau6psp/. It seems to be related to the position:fixed property, but I can't seem to solve it. The issue is that the header is overlapping the main content, making it invisible. I want ...

Using Bootstrap Dropdown on Internet Explorer versions 7 and 8

My application requires support for IE7 and IE8, so I am using Bootstrap 2.3 and Jquery 2.0. However, I am facing an issue where the Bootstrap Dropdown is not opening at all. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"> ...

Error with Jquery validation in Internet Explorer 8

I am working on integrating Jquery validation into my project using this plugin. In researching this issue, I came across three helpful links: Link 1, Link 2, Link 3 The validation is functioning perfectly on all browsers except for Internet Explorer (IE ...

Press the slide button to reveal hidden text using HTML and CSS

I'm having an issue with my website where I want a button to slide down and reveal text, but currently it's only showing the button with the text already displayed below it. The button isn't functioning as intended when clicked. My goal is f ...

Retrieve all colors from the style attribute

I'm on the hunt for a method to extract all CSS colors from a website. Currently, I have been able to manage internal and external stylesheets by utilizing document.styleSheets. However, my issue lies in the fact that any styles directly assigned to a ...

Utilizing html5 mode in AngularJS alongside an external NodeJS server

Despite reading numerous SO answers and questions on this topic, I still find myself with lingering uncertainties. To outline the issue: I am utilizing an AngularJS app with html5 enabled to eliminate the '#' sign. $locationProvider.html5Mode( ...

nested objects within a JSON structure

If you have the following scenario: var test = '{"0":"1", "2":"3"}'; it results in an object with key-value pairs 0: 1 and 2: 3 How can I create an object that looks like this: object: 0: 1 2: 3 object: 4: 5 6: 7? I've attempted the fo ...