Problem with sliding image to the left

Is there a way to slide my image to the left when I click on the right arrow of the image slider? The current behavior is that the current image hides and the next image shows up, but without any sliding animation.

$('#image-content img:first').addClass('active');
        //ON CLICK ON RIGHT ARROW DISPLAY NEXT
        currentIndex = $('#image-content img').index(this) + 1;
        
        $('.right-arrow').on('click', function() {
        if($('#image-content img.active').index() < ($('#image-content img').length - 1)){
        currentIndex++;
        $('#image-content img.active').animate({width: 'toggle'}).removeClass('active').next('#image-content img').addClass('active');     
        }
        else {
        currentIndex = 1;
        $("#image-content img").removeClass("active");
        $('#image-content img').eq(currentIndex - 1).addClass('active');          
        }
        });
        //ON CLICK LEFT ARROW DISPLAY PREVIOUS IMAGE
    
       $('.left-arrow').on('click', function() {
        if ($('#image-content img.active').index() > 0) {
        currentIndex--;
        $('#image-content img.active').removeClass('active').prev('#image-content img').addClass('active');
        } else {
        currentIndex = $('#image-content img').length;
        $("#image-content img").removeClass("active");
        $('#image-content img').eq(currentIndex - 1).addClass('active')
        }
        });
#image-content{
    height: 400px;
    width: 100%;
}
#image-content img{
    position: absolute;
    height: auto;
    width: auto;
    max-height: 380px;
    max-width: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: none
}
#image-content img.active{
    display: block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
                <div class="col-1 text-right nav-direction">
                   <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/136304-200.png" class="img-fluid left-arrow" alt="">
                </div> 
                  <div class="col-9 text-center">
                    <!-- Main Images -->  
                    <div id="image-content"> 
                     <!--(start foreach)-->
                       <img src="http://letssunday.com/assets/upload/product/5aa63613ea17a107011.jpg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97dc88ad258479.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97d45e75220450.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97dcf94f110046.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97e6268c505542.jpeg" class="img-fluid"> 
                     <!-- (end foreach)-->
                    </div> 
                    <!-- End main Images-->  
                  </div>   
                   <div class="col-1 text-left nav-direction">
                   <img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png" class="img-fluid right-arrow" alt="">
                </div> 
              </div>

I'm looking for a solution to add a left side slide animation to the current functionality of the image slider. Everything works fine except for the animation. Can anyone assist with this issue?

Answer №1

Have you tried another approach? You can utilize bootstrap:

div#myCarousel, div#myCarousel > div.carousel-inner, div#myCarousel > div.carousel-inner > div.item{
 /* CSS selectors to style specific divs */
}
div#myCarousel > div.carousel-inner > div.item > img{
 /* CSS selectors to style images */
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>  
  <div class="row">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img class="img-responsive col-xs-12" src="http://letssunday.com/assets/upload/productImageGallary/5a5da97d45e75220450.jpeg" alt="Los Angeles" style="width:100%;">
      </div>
      <div class="item">
        <img class="img-responsive col-xs-12" src="http://letssunday.com/assets/upload/productImageGallary/5a5da97dcf94f110046.jpeg" alt="Chicago" style="width:100%;">
      </div>
      <div class="item">
        <img class="img-responsive col-xs-12" src="http://letssunday.com/assets/upload/productImageGallary/5a5da97e6268c505542.jpeg" alt="New york" style="width:100%;">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>
</div>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

This method is highly effective!

Answer №2

It looks like the CSS is functioning correctly, but there may be an issue with your JavaScript code.

$('#image-content img:first').addClass('active');
        //ON CLICK ON RIGHT ARROW DISPLAY NEXT
        currentIndex = $('#image-content img').index(this) + 1;
        
        $('.right-arrow').on('click', function() {
        if($('#image-content img.active').index() < ($('#image-content img').length - 1)){
        currentIndex++;
        $('#image-content img.active').animate({width: 'toggle'}).removeClass('active').next('#image-content img').addClass('active');     
        }
        else {
        currentIndex = 1;
        $("#image-content img").removeClass("active");
        $('#image-content img').eq(currentIndex - 1).addClass('active');          
        }
        });
        //ON CLICK LEFT ARROW DISPLAY PREVIOUS IMAGE
    
       $('.left-arrow').on('click', function() {
        if ($('#image-content img.active').index() > 0) {
        currentIndex--;
        $('#image-content img.active').removeClass('active').prev('#image-content img').addClass('active');
        } else {
        currentIndex = $('#image-content img').length;
        $("#image-content img").removeClass("active");
        $('#image-content img').eq(currentIndex - 1).addClass('active')
        }
        });
#image-content{
    height: 400px;
    width: 100%;
}
#image-content img{
    position: absolute;
    height: auto;
    width: auto;
    max-height: 380px;
    max-width: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: none;
    transition: 1s;
}
#image-content img.active{
    display: block;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
                <div class="col-1 text-right nav-direction">
                   <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/136304-200.png" class="img-fluid left-arrow" alt="">
                </div> 
                  <div class="col-9 text-center">
                    <!-- Main Images -->  
                    <div id="image-content"> 
                     <!--(start foreach)-->
                       <img src="http://letssunday.com/assets/upload/product/5aa63613ea17a107011.jpg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97dc88ad258479.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97d45e75220450.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97dcf94f110046.jpeg" class="img-fluid"> 
                       <img src="http://letssunday.com/assets/upload/productImageGallary/5a5da97e6268c505542.jpeg" class="img-fluid"> 
                     <!-- (end foreach)-->
                    </div> 
                    <!-- End main Images-->  
  
                  </div>   
                   <div class="col-1 text-left nav-direction">
                   <img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-right-128.png" class="img-fluid right-arrow" alt="">
                </div> 
              </div>

For smooth sliding effects, consider moving content in and out of the page rather than simply sliding objects.

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

The server encountered an unexpected error while processing the request, possibly due to a

My JavaScript file includes an interval function that calls the following code: setInterval(function() { $.getJSON('/home/trackUnreadMsgs', function(result) { $.each(result, function(i, field) { var temp = "#messby" + result[i].from; ...

I am currently utilizing react-admin, however, I am encountering an issue with the heavy build causing errors

Currently, I am working on developing the frontend using react-admin. Initially, I intended to utilize NextJS and found a helpful reference article. Reference: When attempting to run next dev, everything worked smoothly without any errors. However, when ...

Renaming ngModel in an AngularJS directive's "require" attribute can be achieved by specifying a different alias

I need help with a basic AngularJS directive: <my-directive ng-model="name"></my-directive> I want to change the "ng-model" attribute to "model", but I'm unsure how to pass it to the "require" option in the directive. Here is the full co ...

Freeze the headers of multiple tabulators on a single page without restricting the height

My website contains more than 3 tabs and several charts interspersed between tables. Based on the requirement, we have not set a fixed size for the tables. Therefore, the height of the table varies depending on the data. I am looking to fix the headers o ...

Height not being generated by Div element

I am encountering an issue with a div element that contains an h2 and three nested div elements. Despite not explicitly setting a height for the container div, it is not expanding to accommodate the varying heights of the inner divs. This problem is causin ...

Dynamic Fill Colors in SVG File Through Vue Components

I am currently developing a unique social media platform using VueJS 3. For the design aspect, I incorporated Twitter's iconic logo saved as an .svg file, easily displayed using the <img /> tag. To modify its color, I utilized the fill="#f ...

Changes made to attribute values through Ajax success function are not immediately reflected and require a manual page refresh to take effect

Whenever I attempt to rename my object using an Ajax request triggered by a click event, followed by updating its element's attribute with the new name in the success function, I encounter a partial issue. Upon inspecting the element on Chrome, post- ...

Determine the type of element existing in the table cell

Currently, I am utilizing protractor to iterate through table cells in an attempt to verify the presence of a checked checkbox. var elements = element.all(by.css(columncssname)); elements.each(function (cell, index) { <--need to confirm if check ...

Adding incremental values to a variable using JavaScript within the framework of jQuery and AJAX

In my JavaScript code that utilizes jQuery and AJAX, I have created a dynamic array containing multiple values for AJAX requests. The array is structured as follows: <script type="text/javascript> var array = Array("y", "y", "x", "y", "y", "y"); fu ...

The functionality of the Google API JavaScript client is sporadic, intermittently working and occasionally

Having an issue with the Google API JavaScript client that I can't seem to solve. Here's the script in my HTML file: <script src="https://apis.google.com/js/client.js?onload=init"></script> This is what my JavaScript code looks lik ...

Interactive live URL preview feature using AngularJS

Currently, I am developing a form and looking to display a preview of a URL similar to Facebook. As an AngularJS beginner, I lack sufficient knowledge in this area. If you are familiar with any Angular API or library that could assist me with this task, ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

Experiencing problems with CSS compatibility while transitioning from vanilla JavaScript to React

Currently, I am working on revamping the frontend of an app that was initially built with vanilla javascript and transitioning it to a React framework. However, I'm encountering some challenges with styling the HTML elements. Specifically, the textare ...

What is the best way to dynamically update the selected option in a dropdown menu within a Rails application using a variable?

Working on a project that involves a select drop-down menu containing a list of currencies. Want to enhance user experience by automatically selecting the default value in the dropdown based on the user's country (will be utilizing the geoip gem). To ...

no data retrieved from YouTube API query

I'm puzzled as to why the items array is coming up empty. Can someone point out what's causing this issue? Thank you. jQuery(document).ready(function ($) { $('#infl-yt-label').on('click', function() { //$('#infl-inp ...

Tips for combining enable and disable properties in flatpickr.js?

I'm facing a challenge with using both the enable and disable properties in flatpickr.js. The enable property returns a range of dates that should be enabled, but I specifically want to disable certain days within that range, like weekends. datePicker ...

Utilize an Ajax function to call the BOT listening URL within a Web method

My recently developed web application features a submit button that triggers an Ajax call to communicate with the BOT. The code snippet used for the Ajax function is as follows: <script> $(document).ready(function () { $("#btnSend& ...

Achieving uniform card height and positioning the shop button at the bottom of each card

https://i.sstatic.net/2i7hc.png The first card in the image above has a stretched height due to its long title, causing the second card's height to also stretch. However, the shop button is not placed at the bottom. I aim to position the button at t ...

Troubleshooting Issue with Bootstrap 4 Modal Varying Content Display

Dealing with a minor issue where I'm unable to get a modal to fetch data from an HTML table and populate it into a Bootstrap 4 Modal. I've attempted various methods as evident in the code snippets below. Currently in a learning phase, I need to ...

Checkboxes within div elements are unresponsive to clicks

As a novice, I am facing a simple issue. When I place an input checkbox inside the #container div, it functions correctly. However, when I try to put them within the #container div > #section div, they fail to work. *, html, body { box-sizing: b ...