Problem encountered when trying to view full-size images by clicking on array thumbnails, along with the option to navigate through them

After successfully implementing a feature to replace background images with a button click, I now aim to create a gallery of thumbnail images that users can also click on. While I have managed to make the images change with "next" and "previous" buttons, integrating thumbnail clicks has proven to be challenging. Whenever I try to incorporate clicking on thumbnails, it disrupts the functionality. My goal is to allow users to click on a thumbnail image and update the index number so that when they press "next," it moves to the next image instead of reverting back to the second one regardless of the chosen thumbnail.

The main issue arises when clicking on a thumbnail correctly displays the corresponding image, but pressing "next" always leads to the second image in line, irrespective of the current position. I need a solution to ensure that the "next" function reads the index number of the current image accurately.

Thank you in advance for any help!

// Initiating the background slideshow function
$(function() {
    // Defining arrays for background images and thumbnails
    var colorBackgrounds = new Array(
        'url(backgrounds/photoTest/20140714-_MG_0604.jpg)',
         'url(backgrounds/photoTest/20140714-_MG_0860.jpg)',
         'url(backgrounds/photoTest/20140716-IMG_1296.jpg)'
    );
     var backgroundThumbs = new Array(
        '/backgrounds/photoTest/20140714-_MG_0604_TN.jpg)',
        '/backgrounds/photoTest/20140714-_MG_0860_TN.jpg)',
        '/backgrounds/photoTest/20140716-IMG_1296_TN.jpg)'
    );

     var colorCurrent = 0;
     var tnCurrent = 0;

    // Populating the div with thumbnails and their respective click function (working fine)
     backgroundThumbs.forEach(function(value,index) {
        $('#thumbID_' + index).html('<img src="' + value + '" class="thumbImage" />');
        $('#thumbID_' + index).click(function() {
            var tnCurrent = index;
            var colorCurrent = tnCurrent;
        $('.bodyBackground').fadeOut(500, function() { 
            $('.bodyBackground').css({
                'background':colorBackgrounds[colorCurrent],
                'background-position':'center center',
                'background-repeat':'no-repeat',
                'background-attachment':'fixed'});
                $('.bodyBackground').fadeIn(500);           
            }); 
        });
    });

    // Function to move to the next image (currently working, but not considering changes from thumbnail clicks)
    $('.nextImageWrapper').click(function() {
        $('.bodyBackground').fadeOut(500, function() { 
            $('.bodyBackground').css({
                'background':colorBackgrounds[colorCurrent = ++colorCurrent % colorBackgrounds.length],
                'background-position':'center center',
                'background-repeat':'no-repeat',
                'background-attachment':'fixed'});
                $('.bodyBackground').fadeIn(500);

        }); 
    });
});

Answer №1

$(document).ready(function() {

 var currentColor = 0;
 var currentThumbnail = 0;

 // additional code

backgroundThumbs.forEach(function(thumb, index) {

 // more code    

 currentThumbnail = index;
 currentColor = currentThumbnail;

 // additional code

});

});

Give it a try and see if it functions properly.

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

Spinning Earth orbits around the Sun

My goal is to create a 3D Solar system, starting with the Sun and Earth using THREE.SphereGeometry() var sphere2 = new THREE.Mesh(new THREE.SphereGeometry(50, 100, 100), new THREE.MeshBasicMaterial({side: THREE.DoubleSide, map:txt2})); sphere2.position.se ...

Placing additional text beside a nicely aligned box

I am struggling to figure out how to position text in the center left of a box that is located in the top right corner of my HTML template. <table class="label"> <tr> <td class="sign">F</td> ...

Activating a dynamic item on the Bootstrap navbar: Step by Step Guide

I am facing an issue with highlighting the current page on a navbar. I have a navigation bar that consists of 4 pages - index2.html, page1, page2, and page3.html. Whenever I click on a page link, it briefly turns red to indicate it is active but then rev ...

Using JQuery to emphasize selected radio button area

Can someone help me modify the code to highlight the checked radio button by adding or removing a class from the <span class " ui-message ui-state-highlight"> element? Below is the HTML and JS code: $(document).ready(function(){ $('# ...

Failure to send chat form

I have been developing a PHP chat application and everything is working well. However, I am looking to implement AJAX in order to prevent the page from refreshing. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></s ...

Building a Category Directory in WordPress

Hello, I am currently working on creating a list of Categories for my website's Sidebar. However, the information I have found about displaying this category list does not allow me to style it as I want. Initially, I aim to showcase only the top 5 mos ...

Can someone guide me on the process of creating responsive text overlay on an image using Bootstrap 4?

Seeking a solution for making the letters inside an image responsive without using media queries, only utilizing Bootstrap grid. Currently, when resizing the screen, the letters overlap each other instead of maintaining their proportions. Below is the CS ...

Eliminate distinct objects during touchend event in a multi-touch web application

Despite my best efforts and endless searching online, I am struggling to find a solution. While the touchstart and touchmove events allow me to retrieve a unique id from touches.identifier, it seems impossible to achieve this with the touchend event. M ...

having trouble with developing a dropdown menu using jquery

I'm currently creating a drop-down menu for my website and here is the code I'm using: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head> <met ...

Using Ajax-loader functionality with Thymeleaf and Spring Framework

In my Thymeleaf/Spring MVC application, I am using a POST request. $('#rpp').on('change', function() { var url = [[${url_current}]]; var rpp = $('#rpp').val(); var data = { rpp: rpp }; $('#p ...

Tips for implementing jQuery validation with CakePHP 3.0

Just starting out with Cake PHP and I have a question. Can someone show me how to implement custom validation using jQuery in Cake PHP 3.0? Here is the code from my add.ctp file: <h2>Add New User</h2> <!-- link to add new users page --&g ...

Not all the files are being successfully inserted into the database when using the Multiple File Upload feature

I'm currently facing an issue with inserting multiple images into the database. For some reason, only the first uploaded image is being inserted. Below is my PHP code snippet: function reArrayFiles(&$file_post) { $file_ary = array(); $fi ...

In IOS, the Bootstrap FormGroup/Input appears partially hidden by certain layers, impeding full visibility

I've encountered an issue with a form group containing an input using Bootstrap. While it works perfectly on desktop platforms and Android, it's not displaying correctly on iOS. The input field is either partially hidden or not fully shown. Below ...

Browse through and review all information - utilizing python selenium

I am currently working on a project that involves scrolling and retrieving every post made by users. However, I am facing an issue where my code is only reading 2 or 3 posts before moving on to the next one. I have tried adjusting the sleep() duration, p ...

What is the best way to trigger a modal to appear when dynamically generated items are clicked?

My objective is to retrieve specific data from the server and present it in a list format on my webpage. The list displays correctly, but I want users to be able to view additional details for each list item by clicking on it, triggering a modal popup. How ...

Adjust the height of the box based on the content within its flexbox

I have a flexbox div nested inside another div, and I'm trying to adjust the height of the outer box based on the content of the inner flexbox. Here is the link to the current fiddle. The issue I am facing is that the text in the second box overflows ...

Tips for displaying two dynamic variables that are interdependent

For instance: Controller: AllBooks[{ book1:{ hardcover{price:25.99},e-book{price:1.99} } }, book2:{ hardcover{price:60.00},e-book{price:2.99} }]; $scope.bookchoice = function(selectedBook) { $rootScope.choice = selectedBook;} $scope.booktype = functio ...

The image appears to be overlapping within the navigation bar

I'm having trouble understanding why the image is overlapping on this website. The image is enclosed in a link tag, and upon inspecting it, the link tag seems to be correctly positioned while the image appears slightly below. I can't seem to ide ...

Retrieve the value of another ID within a form using the .innerHTML property

I would like to be able to show the value of a text field when the Search button is clicked. Currently, I have the following code on the search button: onclick="document.getElementByid('output').innerHTML = this.value" This code successfully r ...

Restrict the Number of Columns in GridView

I am currently working with a gridview that displays product information. I'm trying to find a way to limit the description field so that only the first few lines are shown if it is too long, and also resize the image to fit into the image column whil ...