The jQuery scrollLeft function seems to be stuck and not working correctly

I have a container div with a ul inside of it set up like this CodePen: http://codepen.io/example/123

Why won't the scroll feature work?

HTML:

<div id="container">
  <ul class="list">
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
  </ul>
</div>
<button id="left">←</button>
<button id="right">→</button>

CSS:

#container {
  width: 200px;
  height: 50px;
  overflow: scroll;
}
.list {
  width: 1000px;
}
.list li {
  background: blue;
  width: 99px;
  height: 40px;
  border-right: 1px solid black;
  float: left;
  padding: 5px;
}

JavaScript:

var position = $(".list").scrollLeft();
$(document).ready(function() {
  $("#right").on("click", function() {
    $(".list").animate({
      scrollLeft: position + 100
    }, 1000);
  });
});

Answer №1

To make the content scroll horizontally, you should use the scrollLeft property on an element with the overflow set, like the element with the id of #outside.

Check out a live demo on jsBin

$(function() {  // Shortened version for DOM READY
  
  $("#right, #left").click(function() {
    var dir = this.id=="right" ? '+=' : '-=' ;
    $("#outside").stop().animate({scrollLeft: dir+'100'}, 1000);
  });

});

In this code snippet, both buttons, with ids of #right and #left, are linked to the same click event handler. Depending on which button is clicked, the value of dir will be either "+=" or "-=". This allows for intuitive scrolling functionality.

Answer №2

Everything is functioning flawlessly

To achieve optimal results, be sure to include either offset() or position(). Additionally, remember to animate the left property.

$(document).ready(function() {
  $("#right").bind("click", function() {
    var position = $("#inside").position();
    $("#inside").animate({
      left: position.left - 100
    }, 1000);
  });
});
$("#left").bind("click", function() {
  var position = $("#inside").position();
  $("#inside").animate({
    left: position.left + 100
  }, 1000);
});

Adjustments Needed

  1. When applying float: left;, ensure you clear your floats properly. Please await the completion of the fiddle...
  2. JQuery has not been loaded in your fiddle.
  3. Remember to assign position: relative; to the UL.

Fiddle Link: http://jsfiddle.net/BQPVL/9/

Answer №3

For a helpful solution, consider utilizing the following code: http://jsfiddle.net/BQPVL/10/

$("#right").on("click", function () {
  $("#inside").stop(true, true).animate({
    left: '-=100'
  }, 1000);
});
$("#left").on("click", function () {
  $("#inside").stop(true, true).animate({
    left:'+=100'
  }, 1000);
});

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

What is the best way to include overflow-hidden in the image displayed by the tailblock component?

I have implemented the following Tailblock component, which is built on tailwind.css. My goal is to achieve a hover effect where the image scales up within its original dimensions. I want the image to zoom in without changing its height and width. I atte ...

The React task list updates the todo items on change, rather than on submission

As a newcomer to React, I have embarked on the classic journey of building a todo app to learn the ropes. Everything seems to be functioning smoothly except for one minor hiccup: When I input a new todo and hit "submit", it does get added to my array but d ...

What is the process for transferring information from HTML to Python and then receiving the output in HTML?

I am completely unfamiliar with the connection between HTML and Python, so I am reaching out for some assistance. I hope that someone here can lend me a hand. Currently, my HTML is hosted on an Apache server, and I access the website using the address "". ...

What is the process for refreshing and creating new markers on a Leaflet map?

Recently, I came across Leaflet and decided to use it instead of Google Maps. I have an API that I want to use for generating and updating map markers to prevent having multiple markers. I've been working on a project which you can view in this fiddle ...

Showing small previews of images using php

I'm struggling with getting linked images to display. The code I have is currently located in a .php file. Its purpose is to retrieve all images from the directory where the file resides and show them using a "for" loop. However, at the moment, it onl ...

The issue of the keyboard disappearing on Chrome for Android while switching between different input

Issue with Input Switching on Chrome for Android I am facing difficulty when trying to switch between two input fields. The HTML code is as follows: <input type="text"/> <input type="text"/> When I enter text in the first input and click on ...

PHPMailer issue with rendering HTML in email body

Our email system utilizes PHPMailer to format emails, but when the emails are sent and opened in email clients like Gmail and Yahoo, they display as text instead of HTML. We have tested different email clients, such as Outlook, which allows us to attach a ...

I encountered a difficulty trying to assign a value to @Input decorators in the spec file

While writing a test case for form submission, I encountered an issue where the Input decorator (e.g. station) value is reassigned during form submission. However, when attempting to define this Input value in the spec file, the component.station value is ...

Enhancing the performance of jquery selection speed

I am currently working on a code that manipulates the HTML code of an asp.net treeview. Since this code runs frequently, I need to ensure that it executes with maximum speed. I am interested in expanding my knowledge on jQuery selectors and optimizing th ...

Increase in JQuery .ajax timeout not effective

My website has a process where JavaScript sends a POST request to a PHP server using the .ajax() function. The PHP server then communicates with a third-party API to perform text analysis tasks. After submitting the job, the PHP server waits for a minute b ...

When scrolling through a page, the MySQL data being loaded via an ajax/jquery call is not displaying the accurate information

Currently, I am facing an issue with loading data from a MySQL database onto a webpage using AJAX/jQuery call. The problem lies in receiving duplicate data that has already been fetched and displayed on the page. I believe there is nothing wrong with the s ...

Utilize React to float the Material UI SwipeableDrawer component to the right side of the screen

I'm currently working on a project that involves using material UI alongside React/Redux. There has been a recent change in the Figma file where the SwipeableDrawer component is now positioned on the right side of the screen instead of the left. I&apo ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

Tips for creating a section that perfectly stretches to cover the entire view port in HTML

After browsing through this website, I was inspired by its design and wanted to replicate something similar. The unique approach they took to ensure each section fits 100% of the viewport caught my eye. They utilized a "div section-background" within each ...

The Ion-button seems to be malfunctioning

I am interested in using special buttons for my ionic 1 project, specifically the ion-button feature outlined on this page: Ionic Buttons. I attempted to create a Round Button and an Outline + Round Button: <h2 class="sub-header" style="color:#4 ...

Having trouble making elements in Tailwind CSS take up the entire page? Seems like they're getting stuck in smaller containers instead

I've been working on creating a layout for my webpage that is similar to the one found here: However, despite using Tailwind CSS, I'm having trouble with the flex styling and getting it to display correctly. import { ChevronRightIcon, HomeIc ...

Incorporate an onclick event to the elements within the array

I'm currently working on iterating over an array and adding an onclick event to each element. My goal is to be able to click on each div and have them log their values in the console. However, I am facing a challenge in applying the onclick event to e ...

Use javascript to modify a specific section of a link

I have a set of datatable rows with links that contain data, and I need to modify part of the link text. For instance, if the link is 200.0.0.10, I want to change it to 160.11.10.12. Despite attempting the following code, the link does not change: var ...

Creating interactive checkboxes dynamically using form data - a step-by-step guide

Scenario When a user fills out an order form, they have the option to choose a bike type and select from the corresponding options available for that bike type. Current Issue I am facing a situation where I need to include a checkbox with all possible op ...

How to detect the Back Button or Forward Button events in a single-page application

Currently, I am developing a single-page application that utilizes ASP.NET MVC, Backbone.js, and JQuery. My task involves capturing the browser's back and forward button events for breadcrumb implementation. I previously attempted to use the hashchan ...