Difficulties encountered while attempting to produce a carousel animation

I’ve been working on implementing a carousel effect for my website, but I’m encountering some issues. When I attempt to click on one of the buttons, an error message pops up:

Uncaught ReferenceError: slide is not defined
at HTMLButtonElement.onclick (index.html:22)

function slide(dir){
                var item_width = $('#carousel_ul li').width();
                if(dir == 'left'){
                    var original_indent = $('#carousel_ul').css('left');
                    var new_indent = parseInt(original_indent) + item_width;
                    $('#carousel_ul').css('left': new_indent);
                }else if(dir == 'right'){
                    var original_indent = $('#carousel_ul').css('left');
                    var new_indent = parseInt(original_indent) - item_width;
                    $('#carousel_ul').css('left': new_indent);
                }
            }
#carousel_inner{
                float:left;
                width:600px;
                overflow:hidden;
                }
                #carousel_ul{
                position:relative;
                left:-200px;
                list-style:none;
                margin:0px;
                padding:0px;
                width:9999px;
                }
                #carousel_ul li{
                float:left;
                width:200px;
                padding:0px;
                height:200px;
                background:transparent; margin:0px;
                }
                #left_scroll,#right_scroll{
                float:left;
                margin-left:5px;
                margin-right:5px;
                height:150px;
                width:50px;
                }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            <div id="carousel_container">
                <div id="left_scroll"><button onclick="slide('left');">prev</button></div>
                <div id="carousel_inner">
                    <ul id="carousel_ul">
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                        <li><img src="http://via.placeholder.com/150x150"></li>
                    </ul>
                </div>
                <div id="right_scroll"><button onclick="slide('right');">next</button></div>
            </div>

I was also hoping to set this up as an infinite loop, but finding it challenging. Here’s what I have in mind:

$('#carousel_ul li:first').after($('#carousel_ul li:last'));
$('#carousel_ul li:last').before($('#carousel_ul li:first'));

Even though there are many ready-made carousels available, I prefer trying to solve this on my own.

Answer №1

Here is the updated JS file with an infinite loop feature:

function slide(dir){
    var item_width = $('#carousel_ul li').outerWidth();
    if(dir == 'left'){
        var original_indent = $('#carousel_ul').css('left');
        var new_indent = parseInt(original_indent) + item_width;
        $('#carousel_ul li:first').before($('#carousel_ul li:last'));
    }else if(dir == 'right'){
        var original_indent = $('#carousel_ul').css('left');
        var new_indent = parseInt(original_indent) - item_width;
        $('#carousel_ul li:last').after($('#carousel_ul li:first'));
    }
}

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

Customizing Material UI themes with CSS variables is a powerful feature of the createMui

In an attempt to create a Material UI theme using existing colors defined as CSS variables in my-palette.scss, the following code is utilized: :root { --primary-color: '#FF0000'; ... } The goal is to incorporate these colors like so: import ...

Troubleshooting: WordPress failing to launch Bootstrap Modal

I recently transformed my PHP website into a WordPress theme, but unfortunately, I am facing an issue with my Bootstrap modal not opening in WordPress. ***Header.php*** <a id="modal_trigger" href="#modal" class="sign-in-up"><i class="fa fa-user ...

How can I change the direction of the NgbModal so that it slides in from the right instead of the default top to bottom?

I'm currently using NgbModal and the Modal is opening from Top to Bottom by default. Is there a way to make it appear from right to left instead? I have already set up the positioning so that it appears in the right corner of the screen, but I can&apo ...

React Container failing to Re-Render despite Redux State Change

I have encountered an issue while working on Redux and React. I am developing a book list where clicking on a book will display its details. Upon selecting a book, the action is properly handled and the state is updated as expected. // reducer_active_boo ...

Delete the form tag from the extracted HTML content of the remote page requested through an AJAX call

I am trying to retrieve the HTML content from a remote aspx page and display it on another page using an ajax request. I need to strip out the form tag from the remote page HTML and also ensure that any scripts included on the remote page are executed. Re ...

Guide on transforming an array into a Collection

I have a text file in the format shown below. ID;SubID;No.;Name;Min;Max;Default;Factor;Unit 101;5;0;Gas flow time;0;100;0.1;10;s 101;30;1;Start speed;20;200;120;1;m/s ;;2;Start current;0;999;1.0;10;A I am reading this text file using the npm package & ...

Leveraging Angular's Observables to Defer Multiple API Requests within a Specified Timeframe

In my Angular 4 app, I have a setup like this (for illustration purposes, I've omitted some code) @Injectable() export class SomeService { constructor( private http: Http ) { } get(id: number) { return this.http.get( ...

arranging JSON data while printing

When I send sorted data from a hashmap in JSON string form, the printed JSON data does not match the original form. The JSON data is: { "409": "A T (C T)", "397": "A T (Government Model School)", "407": "A T (Junior)", "420": "A T (L T Ad ...

Tips for adjusting the vertical position of an image within a bootstrap column by a small amount

Apologies in advance if this question has already been addressed, and I am struggling to adapt it to my specific scenario. My objective is to adjust the positioning of the two images shown in the screenshot example below so that they align with the grey b ...

Guide to integrating a Jquery third-party plugin in Angular 7

Recently, I integrated jQuery and a third-party plugin called "stiffChart" into my Angular 7 project. After installing jQuery and the plugin in my project, I declared them in angular.json file as well. However, when trying to call a method from the plugin, ...

What is the best way to integrate a CommonJS module into an ES6 module within a Node.js

I am working on a node app and I want to adopt the ES6 module format without transpiling down to ES5. However, I still want to integrate older libraries like express and socket.io that use CommonJS / require format. What solutions are available for combi ...

Text using Bootstrap positioned to the right of the breadcrumb navigation bar

Currently, I am using the default theme with Bootstrap 3. I have implemented breadcrumbs as shown below: <ol class="breadcrumb"> <li><a href="#">Home</a></li> <li><a href="#">Library</a></li> < ...

Which prop type should I verify when needing to specify the source?

Currently, I am working on a project that involves react native. To ensure the proper type checking of components, I am utilizing prop-types. My current task is to encapsulate the Image component from react-native within my own custom Image component. Belo ...

Stylish web page tabs with diverse visuals

Is it possible to have the background image become focused when a user clicks on one of the images? I want all icons except the clicked one to be greyed out, with the colored version showing only for the selected icon. Currently, the color image of each i ...

Set up a targeted version of the winston software package for installation

Can I download a specific version of winston through npm by using the command: npm install winston=2.2.0 The reason I am asking is because I am encountering an issue with my existing code written in version 2.2.0 when I download the latest version. The ...

Unable to append Jquery attribute to a div component

My code snippet is creating a div with specific classes and elements: '<div class="ctrl-info-panel col-md-12 col-centered">'+ '<h2>You do not have any projects created at the moment.</h2>'+ '<div id="t ...

What is the best way to pass the setState value to the useEffect hook?

After watching a Youtube video, I took on the challenge of creating my own recipe app using React.js as a beginner. I have been troubleshooting for the past 2 days and seem to have hit a roadblock. The issue lies in passing the value of my state to the use ...

Aligning Cards using Bulma Styles

Currently in the process of developing a Pomodoro Timer application and utilizing Bulma as my CSS Framework. I am really enjoying working with it thus far, although still getting the hang of Flexbox. I am seeking advice on the best approach for this projec ...

Using a Django HTML variable embedded within a JSON string

My json string has a variable embedded in it. "country": { "name":"England", "city":"London", "description":"This country has a land mass of {{ info.1 }}km² and population is as big as <span class=\"colorF88017\">{{ info.2 }} ...

Achieve a full-width background video without adjusting the height, preferably without the use of JavaScript

I have scoured various sources in search of the answer to this question but have come up empty-handed. While I am aware that similar queries have been made before, please hear me out. I am interested in incorporating a video background on a website where ...