Issues with the transparency of a basic tab image gallery (CSS + jQuery)

My tab image gallery project is nearly complete, but I'm facing some challenges with the styling. The default thumbnail doesn't load with maximum opacity as intended and my jQuery code seems to be interfering with the hover effect of my CSS. I did experiment with using focus/active CSS to set opacity on click instead of jQuery, but unfortunately, it didn't work.

Achieving the desired effect

Upon loading the page, image 1 should be displayed in the main frame with its thumbnail at full opacity while the rest have an opacity of 0.8. Hovering over an unselected thumbnail should change its opacity to 1. Clicking on a thumbnail should switch the main image and set the clicked thumbnail's opacity to 1 (while restoring image 1's opacity to 80%).

Ongoing issues

The 'default' thumbnail initially loads at 80% opacity instead of 1.

If I add the following lines to my jQuery code snippet below to manage opacity on click events, the hover effect stops working after a selection has been made.

jQuery(document).ready(function() {
  $(".thumbnail").click(function() {
    $("#mainimage").attr("src", $(this).attr("src"));
    $(".thumbnail").css("opacity", "0.8");
    $(this).css("opacity", "1");
  });
})
#thumbnails img {
  display: inline-block;
  width: 20%;
  cursor: pointer;
  overflow: hidden;
  opacity: 0.8;
}

#thumbnails img:hover {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div id='gallery'>
  <div id='panel'>
    <img id='mainimage' src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/large-breed-dogs-1581096488.jpg' width=80%  />
  </div>

  <div id='thumbnails'>
    <center> <img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/large-breed-dogs-1581096488.jpg' class='thumbnail' style='opacity: 1;'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/golden-retriever-puppy-lying-down-on-grass-royalty-free-image-1587052215.jpg' class='thumbnail'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gettyimages-589656325-1-1586896598.jpg' class='thumbnail'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/chihuahua-dog-running-across-grass-royalty-free-image-1580743445.jpg' class='thumbnail'></center>
  </div>
</div>

Edit: By setting the default image's opacity to 1 using <... style=opacity:1;>, I managed to achieve the desired behavior on page load. I also experimented with another jQuery solution for changing opacity on hover, but this led to the unintended consequence of setting the opacity of the last hovered thumbnail to 1 rather than the last clicked one. Currently, the primary issue lies in the fact that my hover CSS isn't functioning correctly alongside the jQuery provided above.

Answer №1

When utilizing jQuery to define a CSS attribute, it will be applied as an inline style. Inline styles take precedence over regular CSS styles. To override this behavior, you can add an !important declaration after the style you wish to prioritize over the inline one, particularly in cases like hover effects.

The hierarchy of CSS follows a specific order of importance and rendering priority. For instance:

  1. CSS Style with !Important
  2. Inline style
  3. ID
  4. Specific Class
  5. Less specific class

Refer to the code snippet below for a practical example:

jQuery(document).ready(function() {
  $(".thumbnail").click(function() {
    $("#mainimage").attr("src", $(this).attr("src"));
    $(".thumbnail").css("opacity", "0.8");
    $(this).css("opacity", "1");
  });
})
#thumbnails img {
  display: inline-block;
  width: 20%;
  cursor: pointer;
  overflow: hidden;
  opacity: 0.8;
}

#thumbnails img:hover {
  opacity: 1!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div id='gallery'>
  <div id='panel'>
    <img id='mainimage' src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/large-breed-dogs-1581096488.jpg' width=80%  />
  </div>

  <div id='thumbnails'>
    <center> <img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/large-breed-dogs-1581096488.jpg' class='thumbnail' style='opacity: 1;'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/golden-retriever-puppy-lying-down-on-grass-royalty-free-image-1587052215.jpg' class='thumbnail'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/gettyimages-589656325-1-1586896598.jpg' class='thumbnail'><img src='https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/chihuahua-dog-running-across-grass-royalty-free-image-1580743445.jpg' class='thumbnail'></center>
  </div>
</div>

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 verification of Wordpress AJAX nonce is failing

On my WordPress site, I am attempting to execute a PHP function using AJAX upon clicking a button, ensuring the process is secure by utilizing a WordPress nonce. Everything seems to be functioning properly except for the nonce verification, which is faili ...

Looking to transfer JSON data between JavaScript and Java code

I am dealing with a JSON string within my JavaScript code and I want to pass this information to a Java class. Can I simply use a regular Java class for this task or is it necessary to implement a servlet? Furthermore, I am curious about the process of pa ...

Tips for altering the color of an image using CSS attributes

I am looking to create a male Head component in this design draft and modify the CSS according to the skin prop. I have attempted to use filter and mix-blend-mode properties, but have not found a solution yet. Any guidance on how to achieve this would be ...

Exploring how to utilize AJAX for data filtering within the Laravel framework has piqued

The current controller is returning a value even when the fields are empty. I need to display a message if all the fields are empty. Controller public function filterSearch(Request $request){ $obj = (new VaccinationInfo)->newQuery(); if($req ...

The background image is failing to display

I am currently working on a subpage for my website that includes a navigation bar and footer. I am looking to add a background image behind the navigation bar, similar to this design. https://i.stack.imgur.com/j6fDZ.jpg After trying various methods, I at ...

Is there a way in Bower to automatically select only CSS or JS files from an open source project, rather than both, if that is my preference?

Although I suspect the answer, imagine I desire to incorporate the CSS from Twitter Bootstrap without the Javascript. I've set up a gulp script to extract everything from my bower.json file, minimize the JS, compress the CSS, and transfer it to my de ...

Implementing MySQL in JavaScript code leads to various possibilities

I am working on a web application that requires retrieving values from a MySQL database. The process involves: PHP code generates an HTML page successfully. Clicking a button updates a cookie, which also works. Using the cookie in a MySQL query, which is ...

Replace the default disc icon with a more detailed icon for UL lists

I am working with an unordered list and I want to use <details> to show a sublist for one of the items: <ul> <li><details> <summary>details bullet</summary> <ul> <li>detail 1</li> ...

Tips for aligning the navigation bar, main content, and footer in the center

Exploring the realm of web coding for the first time, I find myself in need of some guidance. My current project involves creating a webpage for the local sports team to replace their outdated one on Wordpress. Finding the platform not very user-friendly, ...

Incorrect data retrieval from JSON object with JQUERY

I am working on a function that extracts data from a database on the server-side and assigns it as the value of a textbox on the client-side. The function is functioning correctly on the server-side, but on the client-side, when I use console.log to view t ...

Unpredictable jQuery modal form activation occurring twice

I have integrated Dropzone.js into my website for uploading images, and everything works smoothly during the upload process. However, I encounter a strange issue when attempting to delete the images. Occasionally, when clicking on the "trash" icon to delet ...

Issue with ellipsis not functioning correctly on dynamic placeholders when they are in focus

When I focus on my dynamic placeholder input, the text-overflow: ellipsis property is lost for some reason. However, it is regained when blurred. Can anyone explain why this is happening? If you want to experiment with it, I have set up a stackblitz: htt ...

I am facing issues with my Bootstrap and CSS conflicting with each other in React JS

My styles are mysteriously changing when I add the bootstrap link between the <head></head> tags. Here is the header without the bootstrap link: https://i.sstatic.net/ly2nf.png And here is the header with the bootstrap link: https://i.sstatic ...

What are the implications of dynamically setting or changing event handlers in web development?

Looking to streamline the process of replacing standard confirm() boxes with Bootstrap modals for saving and deleting on a page. Each operation has its own button (referred to as the action button) which triggers the corresponding modal. Upon clicking the ...

Tips for Passing On Parent tabindex Value to Children

Is there a way to propagate a parent's tabindex value to all of its children without individually setting tabindex for each child? Here is an example code snippet: <div id="app"> <div tabindex="2" class="parent-sec ...

What is the best way to retrieve the chosen ID from a list box and pass it to a getJSON function within an ASP.Net MVC application?

I'm facing a challenge with my list box setup... @Html.ListBoxFor(model => model.SelectedCalendarId, Model.PeriodList) After setting up a jQuery call to my controller with a hardcoded value... var calId = 12; $.getJSON("/Publishing/GetLockState1 ...

In-depth preg_match_all analysis

I'm encountering an issue with my detailed preg_match_all not working as expected. Instead of retrieving the desired data, I am getting a blank Array. Below is the code snippet I'm struggling with: <?php $remote_search = file_get_content ...

Designing an interactive region using HTML, CSS, and JavaScript

I recently created this code with some assistance: When I tried to format the code, it all ended up on one line and looked unreadable. To view the code properly, please visit this link: http://jsfiddle.net/QFF4x/ <div style="border:1px solid black;" ...

The functionality of the ajax hash navigation feature seems to be malfunctioning

I've been trying to use hashes for my navigation, but everytime the page loads, my script resets the initial hash to #home. It doesn't matter what hash I add in the URL: Here is the script that runs to check if the hash exists and what content t ...

Mapping with Star Components

As a newcomer to ReactJs, my task is to create a 5-star review component with the ability to display half-star ratings. I previously implemented this in Angular5. Within the map method, I need to loop through the following elements: <!-- full star -- ...