JavaScript horizontal slider designed for a seamless user experience

Currently, I am in the process of developing a slideshow that includes thumbnails. While my slideshow is functional, I am facing an issue with limited space to display all thumbnails horizontally. I am considering implementing a horizontal slider for the thumbnails upon hover or when clicking on previous/next buttons. It's important that my solution be written solely in JavaScript without the use of libraries.

If you're curious about where I'm currently at, feel free to check out this link as the complete code is contained within a single page.

-- EDIT

Below is the HTML snippet:

<div class="controls">
    <a class="previous" href=""><img src="images/fleche_g.jpg" alt=""></a>
    <div class="thumbs">
        <ul id="thumbs">
        </ul>
    </div>
    <a class="next" href=""><img src="images/fleche_d.jpg" alt=""></a>
</div>

The CSS styles applied are as follows:

.controls {
    width: 658px;
    height: 76px;
    margin-top: 10px;
}

#thumbs {
    display: inline-block;
    overflow: hidden;
    height: 76px;
    position: relative;
    z-index: 99;
}

.controls .previous, .controls .next {
    float: left;
    width: 51px;
    height: 76px;
    position: relative;
    z-index: 999;
}

.controls .previous {
    background: transparent url('images/fleche_g.jpg') 0 0 no-repeat;
}


.controls .next {
    background: transparent url('images/fleche_d.jpg') 0 0 no-repeat;
}

In addition, here are the two functions I attempted to call onClick of the previous/next buttons:

    // Move thumbs to the left
function left() {
    document.getElementById('thumbs').style.left = '-124px';
}

// Move thumbs to the right
function right() {
    document.getElementById('thumbs').style.right = '-124px';
}

Despite my efforts, nothing seems to be working properly. Any insights on what could potentially be missing?

Answer №1

If you're looking to solve your issue, consider adding a new function called getLeft(). The rest of the adjustments involve tweaking existing functions and CSS styles.

To allow for more images, the client may need to press the right-button multiple times to reach the end. You could also implement additional calculations to limit the range so that all images stay within the boundaries of the .wrapper-thumbs (don't hesitate to reach out for further assistance).

Note: if you prefer the left and right buttons to behave differently (some designs switch them), simply interchange the values 500 <-> -500.

Alternatively, do you want just enough images so that one press of the button takes you completely to the left or right?

CSS:

#slideshow .controls .wrapper-thumbs {
  overflow: hidden;
  position: relative;  
  float: left;          
  margin: 0;
  width: 556px;
  height: 76px;
}

#thumbs {
  display: inline-block;
  width: 900px;
  height: 76px;
  position: absolute;  
  z-index: 99;
}

Script:

// Retrieve the numeric value of style.left by removing 'px'
function getLeft() {
 var left = document.getElementById('thumbs').style.left;
 return Number(left.substr(0, left.length - 2));
}

// Move thumbnails to the left
function left() {
 var currentLeft = getLeft();
 var newLeft = currentLeft - 500;
 document.getElementById('thumbs').style.left = newLeft + 'px';
}

// Move thumbnails to the right
function right() {
 var currentLeft = getLeft();
 var newLeft = currentLeft + 500;
 document.getElementById('thumbs').style.left = newLeft + 'px';
}

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

Using JavaScript/jQuery to tally characters

Here is the code snippet that I am currently working with: PHP <input style="color:red;font-size:12pt;font-style:italic;" readonly="" type="text" name="q22length" size="3" maxlength="3" value="50"/> <textarea onkeydown="textCounter(doc ...

Employing Isotope/jQuery for organizing posts on Tumblr in columns with the ability to infinitely scroll

Alright so, here we have the classic dilemma where scripts are running before images load. And to make matters more complicated, on Tumblr, there's no way to access image dimensions before they're loaded into the DOM... $('#thumbnails&apos ...

What is the process for getting involved with npm commands?

Here is an excerpt from my package.json file: "scripts": { "cpFile": cp ../template/index.js /src/view/home/ } When I try to run the command: npm run cpFile fileName.js I expect it to execute: cp ../template/index.js /src/view/home/fileName.js How ...

I possess a dataset and desire to correlate each element to different elements

[ { "clauseId": 1, "clauseName": "cover", "texts": [ { "textId": 1, "text": "hello" } ] }, { "clauseId": 3, "clauseName": "xyz", "te ...

The function createReadStream completes execution without resolving

Currently, I am in the process of developing a program that is aimed at iterating through files within a specified directory in a Linux environment. I have implemented a custom function called "myReadFile" which returns a new promise, and it is expected ...

Firefox only loads images and jquery after a refresh of the page

Upon initially accessing my site after clearing the cache (a jsp page from a spring app), I noticed that the images and styles were not being applied. However, upon refreshing the page (ctrl-r), everything loaded perfectly. In Firefox's console outpu ...

Mastering the correct usage of the submitHandler method in the jQuery validation plugin

Here is a snippet of documentation from the jQuery validation plugin: "Use submitHandler to execute some code before submitting the form, without triggering the validation again." submitHandler: function(form) { $.ajax({ type: 'POST&apos ...

gulp - synchronized gulp.pipe(gulp.dest) execution

Here's my challenge: I have two tasks, where Task B depends on Task A. In Task A, one of the requirements is to loop through an array and then use gulp.dest, but it seems like Task B will be executed before Task A is completed. The main goal of Task ...

Using ThreeJs and TweenJS to insert a delay between tweens using a for loop

Struggling with animating a bin packing problem using Three.JS and Tween.JS. I am having difficulty getting the animation to play successively rather than all at once within my loop. Attempting to use the setTimeout solution has not been successful. Does a ...

Looping through items using ng-repeat according to the chosen option in a select dropdown

After expanding my project, I encountered an issue with the main object structure: { screens: [{ id: 0, name: 'Screen0', sections: [{ id: 0, sectionItems: [] }, { id: 1, sectionItems: [] }, { i ...

Can $.ajax be used as a replacement for $(document).ready(function()?

After conducting an extensive search, I am still unable to find a clear answer to my assumption. The code I used is as follows: <?php session_start(); if (isset($_SESSION['valid_user']) && $_SESSION['from']==1) { ?> ...

The module 'pouchdb' appears to be missing, functioning correctly on Mac but encountering issues on Windows

I have taken over a project built with Ionic that was originally developed on a Mac by my colleague. I am now trying to run the project on my clean Windows 10 PC with Ionic, Cordova, and Python installed. However, I am encountering an error that my colleag ...

Converting JSON data types into TypeScript interface data types

Struggling to convert data types to numbers using JSON.parse and the Reviver function. I've experimented with different options and examples, but can't seem to figure out where I'm going wrong. The Typescript interface I'm working with ...

Access the JSON file, make changes to a specific value, and then save the

In my JSON data file, I have the following information: [ { "key" : "test1", "desc": "desc1" }, { "key" : "test2", "desc": "desc2" }, ] I have written a script to retrieve this data from the file using AJAX and display it in an HT ...

The search button is malfunctioning after I submit search data and generate dynamic HTML using axios

When a user clicks on the search button, I retrieve the input value and then use axios to send a GET request with the search data. Everything works fine, but when I query the database and dynamically create data from the mongoose data, the page reloads w ...

Enhancing the session helper in Silex with additional values

Hey there, I'm currently working on a basic shopping cart using an MVC framework called Silex. However, I've run into a JavaScript/AJAX issue that I could use some help with. My problem arises when trying to add a product to the basket. The issue ...

Step-by-step Guide to Setting pageProps Property in Next.js Pages

When looking at the code snippet provided in .../pages/_app.js, the Component refers to the component that is being exported from the current page. For instance, if you were to visit , the exported component in .../pages/about.js would be assigned as the ...

The (ReactJS + Redux) form fails to load when the /new segment is appended to the URL

I'm currently working on a project using React, Redux, and Rails. I've encountered an issue with loading the form. In my App.js file, I have set up a Router that defines the routes. import React, { Component } from 'react'; import { Br ...

Instructions on dynamically positioning a collection of div elements at the center of a webpage container

I am looking to create a set of divs that are centered on the page and adjust in size according to the length of the username. .Container1 { display: table; width: 100%; padding:0; margin:0; -webkit-box-sizing: border-box; -moz-box-sizing: bor ...

Difficulty in detecting state changes without refreshing the component when using Redux and React

I'm encountering difficulties detecting state changes from my Redux reducer in a React application. When I modify the state within one component, the other component in the app does not get the update unless the component is reloaded or refreshed. Let ...