swap out the main picture for a new one once the thumbnail is selected (gallery feature)

Hey everyone! I recently decided to create a personal portfolio website to showcase my new passion for photography.

Everything is set up at the moment and you can check it out here.

However, I've been struggling to get the image replacement to work in CSS. Despite my best efforts, I couldn't figure it out and ended up failing miserably. So, I'm thinking of using jQuery instead.

This is where I need your expertise. I have zero knowledge of jQuery and I require assistance in making the large image change to the thumbnail that is clicked, almost like a button. Do you think this is possible? Your help would be greatly appreciated. Thank you!

Answer №1

If you're looking to enhance your webpage with a dynamic image gallery, consider incorporating some jQuery code like the example below:

$(document).ready(function(){
    $('.gallery img').click(function() {
        var img = $(this).attr('src');
        $('.content img').attr('src', img);
    });
});

Remember to include the jQuery core library in the <head> section of your HTML document:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

For a visually appealing effect, you can also change the cursor style for the thumbnails to indicate they are clickable. This can be achieved using CSS or with the help of jQuery:

$(document).ready(function(){
    $('.gallery img').click(function() {
        var img = $(this).attr('src');
        $('.content img').attr('src', img);
    }).each(function() {
        $(this).css('cursor', 'pointer');
    });
});

Check out the demos here to see these features in action.

Answer №2

Absolutely, utilizing Javascript/jQuery is essential for achieving your desired outcome.

Don't forget to insert the jQuery script on your webpage within the <head> section.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>

This straightforward function will cater to your requirements...

$(document).ready(function() {
$('.img img').hover(function() {
    $img = $(this)
    $('.content img').attr('src',$img.attr('src'))
});
});

Expanding your knowledge of Javascript/jQuery will undoubtedly prove beneficial as it's an impressive technology and a valuable skill.

Explore more jQuery tutorials here.

Answer №3

Instead of relying on JQuery or any JavaScript, consider using pure CSS2 to achieve the desired effect. Take a look at this NOT SUITABLE FOR WORK site:

When you hover over the thumbnails on that site, you'll notice a simple and effective transition without the need for JavaScript. As someone who prefers minimal JS, I believe in keeping things as light as possible.

-- pete

I advocate for minimal use of JS, especially when it comes to basic links that can be easily implemented without unnecessary scripting!

Answer №4

Is there a conflict-free version of this code for Joomla 1.5.1? I have a thumbnail linking to a main image that works in Dreamweaver with jQuery 1.2.3, but it's not functioning when uploaded to Joomla with jQuery 1.5.1.

<script type="text/javascript>
jQuery(document).ready(function(){
    jQuery(".tmbimage a").click(function(){
        var largePath = jQuery(this).attr("href");
        var largeAlt = jQuery(this).attr("title");
        jQuery("#mainimg").attr({ src: largePath, alt: largeAlt });
    }); 
});
</script>

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

Utilizing Bootstrap 3: Mastering the Implementation of .col Classes with .form-control Classes

In my form, I have utilized the .form-control classes for the form elements. To arrange two inputs side by side, I am applying .col-md-6 on each of these inputs. However, the width generated by .col-md-6 is being overridden by the .form-control class. Is ...

How can I simplify the CSS properties for this border?

I created a div named "commentbox" and I want to apply a border with the color #ccc. However, I only want the left, top, and bottom sides of the div to be bordered, leaving the right side untouched. Thank you! ...

Avoid having any spaces between HTML tags when being displayed from a single <?php?> in PHP

I am intrigued by an issue I am facing and would like to understand why it is happening. Currently, I am working with the Yii2 framework and have created a widget to display a thumbnail using Twitter Bootstrap. I have written the code to allow for the ren ...

"Interact with jQuery by sliding side to side or in a circular motion

I am in need of assistance with sliding images using jQuery back and forth, or making them go round in a loop. Currently, the images slide up to the last element and then swiftly return to the first div, which is not visually appealing at all. I understa ...

How can I keep the size of a Bootstrap button consistent while wrapping text inside it?

Is there a way to wrap or resize text inside a Bootstrap button without changing the button size? I have multiple buttons that need to be aligned properly. I tried using this class, and while the text wraps, the button grows in size, causing alignment iss ...

The Gravity Simulation Seems to Be Malfunctioning

My current project involves creating a simulation of gravity and its effects. As I embark on this endeavor, I have come across a puzzling issue that has me stumped. The goal is to have my particle's speed increase exponentially based on the gravitatio ...

Deleting a Session Cookie: Steps to Remove It

Is there a way to dynamically remove a session cookie using jQuery without having to restart the browser? I remember reading that session cookies are stored in browser memory and usually get deleted when the browser is closed. // sessionFooCookie represen ...

What is the best way to assign multiple default values to an HTML select element by utilizing the data stored in a PHP array?

This situation expands upon the solution provided in a previous question: Setting a default value for an HTML select control in PHP. My goal is to populate multiple matching values from an additional array: Below is the code I have so far: <select nam ...

What is the best way to create a code block with a specific width in mind?

I'm currently working on a documentation website and I'm facing an issue with my code blocks. I can't seem to set a specific width for them; instead, the width is determined by the text content they contain. What I really want is for the co ...

"Exploring the Possibilities: Foundation 4 Reveal and WP AJAX for Creating Previous/Next Modal Windows within an Open Modal

I am currently in the process of developing a personalized gallery that integrates WordPress and Zurb Foundation: The layout showcases an organized collection of custom posts on different pages; these are displayed as a grid of linked thumbnails through ...

Displaying a View with a content-type of "application/json" results in the presentation of a plain HTML format on the screen

Previously, my web application was functioning perfectly with the content-type in the Headers set as "text/html" for the rendered html page. Due to a change in requirements, I now need the page to be displayed with a content-type of "application/json". I ...

Ways to stop VoiceOver from selecting the background content when a modal is open

Is there a way to prevent VoiceOver from reading the content behind a modal? I tried using aria-modal=true, but it seems VoiceOver does not support this feature by default like NVDA and JAWS do. I found more information about this on . According to the in ...

Extract information from a JSON document and showcase photographs in a visual collection

As a newcomer to JQuery, I would greatly appreciate any assistance. "I am looking to use the $.getJSON function to retrieve data from the items.json file and showcase the images in a gallery below. The gallery should present each image as a thumbnail with ...

Is there a way to simplify and optimize the code further?

Here is the code snippet from my index.blade.php: HTML @foreach($sesis as $sesi) <td>{{ $sesi->waktu }} <label class="switch switch-text switch-info switch-pill" id="label-switch{{ $sesi->id } ...

Uploading information from a confirmation page to the controller

Within this cshtml file, I have included a model. This specific page serves as a confirmation page where the user is able to review and verify that all entered information is correct before proceeding. There is a link button (not part of the displayed cod ...

"Unable to get 'vertical-align middle' to function properly in a table

Can someone please review my code at http://jsfiddle.net/4Ly4B/? The vertical alignment using 'vertical-align: middle' on a 'table-cell' is not centering as expected. ...

Dealing with interruptions in the sequence of results from a database request

Greetings! I am currently utilizing a customized base on Wordpress to organize and manage dog show results. The current setup displays the resulting array in a monolithic manner, which can be seen in the image here: https://i.sstatic.net/ka1SZ.png Howeve ...

`Modified regions determined by cursor location`

I am working with a split layout featuring two columns, and I need the ability to make each column separately scrollable. Due to using a specialized scroll-to function, I cannot use overflow-y: scroll; or overflow: auto;. I am looking for alternative solut ...

A "Uncaught TypeError" error occurs when trying to execute a function using the dollar sign

After successfully recognizing the hover function, the console displays an error message: Uncaught TypeError: $ is not a function <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <script> $(docume ...

What is the method for causing a patch event using inline HTMX tags when a carousel-item in a Bootstrap 5 carousel is activated?

I am currently working on triggering a HTMX patch request with a specific id whenever an item in a Bootstrap5 carousel becomes active. While I understand that this can be achieved with some additional scripting, I am curious if it can be purely done using ...