Employ the power of AJAX to selectively display or conceal a specific group of images

Have you ever thought about showcasing a large number of images on a non-CMS website? Say you have 50 images that can't all be displayed at once. One idea is to display the first 15, then allow users to click "Next" to reveal the next batch of 15, and so on.

How can this be achieved using AJAX, PHP, and CSS?

Perhaps counting, adding a hidden class, and toggling visibility could work?


Alternatively, in PHP or jQuery, how can you target the first 15 elements within a div with the class "image," add a "showimage" class to them, and then upon clicking a "NEXT" button, remove the "showimage" class from all and reveal images 16 to 30?


A sample code snippet may look like the following:

<div id="gallery2"> <!-- Photo Gallery --> 

<a href="ia/new_01.jpg" class="highslide" onclick="return hs.expand(this)">
<img src="timthumb.php?src=ia/new_01.jpg&amp;h=<?php echo $size ?>" title="Click to enlarge" 
alt="Pont Neuf - Ile de la Cité - Paris - France - Watercolor <br> Personal design"/></a>

<a href="ia/new_02.jpg" class="highslide" onclick="return hs.expand(this)">
<img src="timthumb.php?src=ia/new_02.jpg&amp;h=<?php echo $size ?>" title="Click to enlarge" 
alt="Studebaker Hawk - Retro concept <br> Personal design"/></a>

<a href="ia/new_03.jpg" class="highslide" onclick="return hs.expand(this)">
<img src="timthumb.php?src=ia/new_03.jpg&amp;h=<?php echo $size ?>" title="Click to enlarge" 
alt="Corvette - Retro concept <br> Personal design"/></a>

<a href="ia/new_04.jpg" class="highslide" onclick="return hs.expand(this)">
<img src="timthumb.php?src=ia/new_04.jpg&amp;h=<?php echo $size ?>" title="Click to enlarge" 
alt="Thunderbird - Retro concept <br> Personal design"/></a>

<a href="ia/new_05.jpg" class="highslide" onclick="return hs.expand(this)">
<img src="timthumb.php?src=ia/new_05.jpg&amp;h=<?php echo $size ?>" title="Click to enlarge" 
alt="My Father's Cars <br> Personal design"/></a>

Repeat the process for the remaining images up to 50.

Answer №1

There is no need for ajax in this scenario. Instead, you can store all the image URLs in a JavaScript array. Then, when the user clicks on a "next" button, you can simply hide the current 15 images and replace them with the next 15 images from the array. This allows the browser to render the images on the fly.

var images=[]; // array to store images
var ptr=0;
var length = 15;
function show(offset, count){
    for(var i=offset;i<offset+count;i++){
        // add image images[i] to the image container
    }
}
function next(){
    // clear the image container
    ptr+=length
    show(ptr, length);
}

show(0, length); // display first set of images initially

To enable the functionality for the "next" button, simply attach the next function as a click handler.

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 method for eliminating underscores from text that is hyperlinked in a locally hosted HTML file?

I recently encountered an issue with my HTML file where the text "Indian" became underlined after saving the file. Here is the code snippet: <a href="file:///G:\work files\project\indian.html" target="_blank"> <div class="button ...

Is it possible to apply identical css styles to multiple ids at once?

Is there a way to accomplish this? .apple, .orange, .kiwi h1 {color: #892828;} I'm having trouble making it work. Any suggestions? ...

Unlocking the Power of JavaScript: Implementing the $.getJSON Function Without jQuery

I had originally planned to only use the jQuery $.getJSON function in my demo, but now I have realized that I need to import jQuery. Therefore, I am interested in writing the jQuery $.getJSON function using native JavaScript instead. This is the code I ca ...

Plugin for creating a navigation bar with a jQuery and Outlook-inspired style

Our team is considering transitioning our asp.net forms application to MVC. Currently, we utilize outlook-style navigation with ASP controls such as listview and accordion, using code referenced here. Are there any jQuery/CSS controls available for MVC t ...

Menu only appears with a double click on the label button

Currently, I'm facing an issue where I have to click the label "button" (hamburger menu) twice in order to show the menu for the second time. My belief is that there might be a conflict between CSS and jQuery causing this behavior. The desired funct ...

Switching colors on SVG when hovering

When I hover over the code below, I want both eyes and eyebrows to change color. I've managed to get it working for the left eye, but not the right eye. Additionally, the fill on the right eyebrow looks strange when hovered over. I suspect this issue ...

Incorporating multiple CSS style sheets within a single HTML document

As part of my assignment, I have successfully created two different CSS style sheets. The next step is to link both style sheets to a single HTML page and provide users with the option to switch between the two styles. I am wondering how to achieve this. ...

Disorganized jQuery Animation

Looking for some help with an animation I have on my website. The animation is supposed to smoothly move in and out when the mouse cursor hovers over it, but as you can see, it's a bit messy. This project involves HTML, CSS, and jQuery <!DOCTYPE ...

What is the best way to position a small square box over each td element containing text using css?

Currently, I am in the process of developing an HTML table where I would like to incorporate a square box around each td element using CSS <tr> <td style="width:8%; text-align:center; border: 1px solid #000; margin-left: 30px;"><?php e ...

What is the reason that custom styling for a single react component instance does not function properly?

In my current view, I have integrated a react component as shown below: <Jumbotron> Lots of vital information </Jumbotron> I am looking to give this particular instance a unique style, like using a different background image. However, addin ...

Discover the outcome of two asynchronous ajax requests using XHR's onprogress event-handler

My current challenge involves seeking out images within an ajax request response and extracting the src URL for utilization in another ajax request. I am aiming to monitor the loading progress of each image and display the resulting progress in a designat ...

What is the best way to allow a number to be editable when clicked on in a React application

Currently, I am trying to find a solution for making a number editable when clicked without having to use form inputs or other React libraries that don't fit my requirements. The provided screenshots showcase the desired interface. https://i.stack.im ...

What is the process for retrieving data on the server side using node.js that has been sent from the frontend via the post method

After diving into learning node.js with the express framework, I encountered a roadblock. I'm experimenting with a basic query search webapp and trying to send some data (a boolean named all) from front-end plain javascript via ajax to the server sid ...

I am looking to implement user authentication using firebase, however, the sign-in page is currently allowing invalid inputs

<script> function save_user() { const uid = document.getElementById('user_id'); const userpwd = document.getElementById('user_pwd'); const btnregister=document.getElementById('btn-acti ...

Angular 6 CSS spacing dilemmas

We recently made the switch from Angular 5 to Angular 6 and noticed that there is no spacing between the buttons/icons, etc. We are looking to bring back the spaces between the buttons and icons. I have recreated the issue below. As you can see in the Ang ...

Employing CSS selectors to target the subsequent element that is accessible

Here is the structure of my HTML: <input type = "checkbox" style = "display:none" id = "select"> <label for = "select" id = 'click'> click </label> <div class = 'next'> </div> I am trying to style t ...

Monitor fetch() API calls and responses in JavaScript

I’m looking to intercept fetch API requests and responses using JavaScript. Specifically, I want to be able to capture the request URL before it is sent and also intercept the response once it has been received. The code below demonstrates how to inter ...

Issues with CSS properties not functioning correctly within the class.col function

When attempting to apply specific CSS properties to a particular area (e.g., "Dashboard") by assigning the class name "main" to the div.col function in the HTML, I encountered an issue where the CSS property applied affected the entire page. The following ...

What is the best way to determine the size of the URLs for images stored in an array using JavaScript?

I am working on a project where I need to surround an image with a specific sized 'div' based on the image's dimensions. The images that will be used are stored in an array and I need to extract the height and width of each image before disp ...

Develop a PHP polling system with a limit of one vote allowed per unique IP address

My website features an Ajax Poll where users can vote multiple times after refreshing the page, with results being recorded. However, I want to restrict voting to once per IP address and show the results upon page refresh. Below is the code: The HTML Pag ...