Hovering over one element in Jquery triggers actions on multiple elements simultaneously

I'm working on a project where I have multiple cards, and I've implemented a hover effect using jQuery to make the images inside the cards bigger. However, I'm facing an issue where hovering over one card triggers the effect on all the other cards as well. Is there a solution to this problem without having to create separate classes for each card in CSS?

$(document).ready(function() {
  $('.cardjs-1,.cardjs-2').mouseover(function() {
    $('.cardimage').addClass('card-js-hover');
    $('.cards').mouseout(function() {
      $('.cardimage').removeClass('card-js-hover');
    });
  });

});
.cards-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  align-content: center;
  justify-content: center;
  padding: 50px;
}

.cards {
  display: grid;
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  text-align: center;
  width: 200px;
  height: auto;
  background: #f2f2f2;
  border-radius: 15px;
  box-shadow: 10px 10px 80px 10px #a29d9d;
  overflow: hidden;
}

.cardimage {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100px;
  height: 100px;
  border-radius: 100px;
  transition: border-radius .1s, width .1s, height .1s;
}

.card-js-hover {
  border-radius: 0;
  width: 100%;
  height: 100%;
}

.btn-book {
  padding: 15px;
  background: #F25F5C;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
  <div class="cards cardjs-1">

    <img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
    <h4>PARIS</h4>
    <p>$500/4 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
  <div class="cards cardjs-2">

    <img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
    <h4>BERLIN</h4>
    <p>$200/3 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
</div>

Answer №1

The issue arises because you are selecting all elements with the class .cardimage. To fix this, target only the relevant one based on the hovered .cards element using the this keyword and jQuery's find() method.

Additionally, consider utilizing a single selector for .cards along with hover() and toggleClass() as shown below to streamline the code logic.

$(document).ready(function() {
  $('.cards').hover(function() {
    $(this).find('.cardimage').toggleClass('card-js-hover');
  });
});
.cards-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  align-content: center;
  justify-content: center;
  padding: 50px;
}

.cards {
  display: grid;
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  text-align: center;
  width: 200px;
  height: auto;
  background: #f2f2f2;
  border-radius: 15px;
  box-shadow: 10px 10px 80px 10px #a29d9d;
  overflow: hidden;
}

.cardimage {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100px;
  height: 100px;
  border-radius: 100px;
  transition: border-radius .1s, width .1s, height .1s;
}

.card-js-hover {
  border-radius: 0;
  width: 100%;
  height: 100%;
}

.btn-book {
  padding: 15px;
  background: #F25F5C;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
  <div class="cards cardjs-1">
    <img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
    <h4>PARIS</h4>
    <p>$500/4 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
  <div class="cards cardjs-2">
    <img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
    <h4>BERLIN</h4>
    <p>$200/3 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
</div>

However, keep in mind that achieving the same effect can be done in CSS alone without the need for JavaScript. This approach offers the advantage of not depending on JS for UI functionality and delivers superior performance since CSS is hardware accelerated while JS isn't.

.cards-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  align-content: center;
  justify-content: center;
  padding: 50px;
}

.cards {
  display: grid;
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  text-align: center;
  width: 200px;
  height: auto;
  background: #f2f2f2;
  border-radius: 15px;
  box-shadow: 10px 10px 80px 10px #a29d9d;
  overflow: hidden;
}

.cardimage {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100px;
  height: 100px;
  border-radius: 100px;
  transition: border-radius .1s, width .1s, height .1s;
}

.cards:hover .cardimage, 
.card-js-hover {
  border-radius: 0;
  width: 100%;
  height: 100%;
}

.btn-book {
  padding: 15px;
  background: #F25F5C;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
  <div class="cards cardjs-1">
    <img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
    <h4>PARIS</h4>
    <p>$500/4 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
  <div class="cards cardjs-2">
    <img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
    <h4>BERLIN</h4>
    <p>$200/3 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
</div>

Answer №2

To achieve this effect, you can simply utilize the CSS selector .cards:hover .cardimage:

.cards-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  align-content: center;
  justify-content: center;
  padding: 50px;
}

.cards {
  display: grid;
  grid-template-rows: 3fr 1fr 1fr 1fr;
  align-items: center;
  text-align: center;
  width: 200px;
  height: auto;
  background: #f2f2f2;
  border-radius: 15px;
  box-shadow: 10px 10px 80px 10px #a29d9d;
  overflow: hidden;
}

.cardimage {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100px;
  height: 100px;
  border-radius: 100px;
  transition: border-radius .1s, width .1s, height .1s;
}

.cards:hover .cardimage{
  border-radius: 0;
  width: 100%;
  height: 100%;
}

.btn-book {
  padding: 15px;
  background: #F25F5C;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cards-container">
  <div class="cards cardjs-1">

    <img src="https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="paris-image" class="cardimage">
    <h4>PARIS</h4>
    <p>$500/4 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </div>
  <div class="cards cardjs-2">

    <img src="https://images.pexels.com/photos/2570063/pexels-photo-2570063.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="cardimage">
    <h4>BERLIN</h4>
    <p>$200/3 days</p>
    <a class="btn-book" href="#">Book Now</a>
  </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

What could be causing the malfunction of the Facebook iframe like button?

Even though it functions offline, there seems to be an issue with its performance online. Here is the code that was used: <iframe src="http://www.facebook.com/plugins/like.php?app_id=125925834166578&amp;href=http%3A%2F%2Fwww.facebook.com%2FBaradei. ...

Tips for identifying whether an ajax response represents an array containing key-value pairs or simply a string response

My ajax function is encountering an issue where some PHP functions send responses as arrays while others send simple strings. For example, a successful response will be received as a key-value pair, but if there's an unauthorized error, an error key w ...

Creating a logo that scales seamlessly across different web browsers while maintaining responsiveness through

I am working on making this logo align perfectly with all screen resolutions and browsers, while ensuring it stays at the exact center (both vertically and horizontally) of the #container. If the resolution drops below 320px, I want the company name to dis ...

Using Javascript to validate passwords and Bootstrap for design enhancements

I've been working on learning Javascript and some basic HTML using bootstrap v5 recently. One of the tasks I'm currently tackling is creating a Sign-in form and implementing validation for the required fields. While following the Bootstrap docu ...

Identifying the accurate folder for file uploads

In my project directory, I have a folder named uploads, along with two files called upload.html and upload.php. The relevant part of my upload.html file looks like this: $(document).ready(function(){ $("#pictureUploadSubmit").submit(function(event) { ...

Leverage Angular variables within HTML tags

Below is the code in my controller.js file: $scope.animatt = 900; and here is the corresponding html code: <div id="properties" data-{{animatt}}="left:100%;top:10%;"> <h2>all numeric properties</h2> </div> I am t ...

FadeOut Television static on Canvas after a period of inactivity

Is there a way to smoothly fade out the TV noise after a specific timeout period? I found this code snippet on Stack Overflow that seems to address a similar issue: var canvas = document.getElementById('canvas'), ctx = canvas.getContext( ...

What causes the q[num] error to occur when stopping a jQuery queue process?

When I use $.manageAjax to create and execute an ajax request queue, I encounter an issue when trying to abort the entire queue using ajaxManager.abort();. An error message pops up saying: q[num] has no properties (jquery.ajaxmanager.js line 75) Here is t ...

What is the best way to execute a Java script using AJAX from a different file?

I have included multiple ajax scripts in the main body of the Django template. However, when I try to run them from a separate JS file, they do not seem to work. This is an example of one of the working scripts within the body template: <!--Add product ...

What is causing my background image to move upward when I include this JavaScript code for FlashGallery?

There's an issue on my website where adding a flash gallery script is causing the background image to shift unexpectedly. You can view the affected page here: The culprit seems to be the "swfobject.js" script. I've identified this by toggling t ...

Creating an HTML Canvas using an AngularJS template

I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...

Achieving bottom alignment for a button in Bootstrap 4

<div class="row"> <div class="col-lg-3 col-md-6 col-11 d-flex flex-column text-center ng-star-inserted"> <div class="card prcard"> <img height="200px" alt="Card image cap" class="card-img-top mx-auto d-block" ...

Displaying data from a PHP form

I'm working on a table that is populated with data from my controller. The issue I am facing is that only the first record, in this case Sephen C. Cox, does not redirect me when I click the "add employee" button. For all other records, the button work ...

preventing elements from moving unexpectedly as the navigation bar becomes fixed at the top of the page

I have a website that features a Bootstrap 3 navbar. This navbar is positioned 280px below a block div and becomes sticky at the top of the page when scrolled to that point. HTML (within < head > tags) <script> $(document).ready(function() ...

Sorry, we couldn't find the page you were looking for. The request method used was GET and the request URL was http://127.0.0.1:

I've previously reported this error without success, so I'm providing more details now. I am new to coding and recently started a Django & Python project, but encountered some issues. Despite three weeks of troubleshooting, the problems persist. ...

Data stored in Ajax has been irretrievably lost

When I send an Ajax request using GET, one of the parameters (to) seems to be lost and cannot be retrieved on the called file using $_GET["to"]. I'm curious as to what might be causing this issue. Interestingly, some files are working fine with this ...

Ways to access dropdown menu without causing header to move using jQuery

Greetings everyone, I am currently working on a dropdown language selection feature for my website. The issue I am facing is that when I click on the language dropdown in the header, it causes the height of the header to shift. $('.language- ...

What is the best way to include a JavaScript variable within CSS styling?

I am currently implementing a dropdown menu system where the image of a parent div changes on mouse hover over a link, and reverts back when the mouse moves away. I have this functionality set up using a variable in my HTML code. Is there a way for me to m ...

How do you retrieve an element using its tag name?

Just delving into the world of API's here. Working on a project involving loading a DIV with a bunch of links that I need to repurpose. Managed to disable the default onclick function, now trying to figure out how to save the "innerHTML" attribute of ...

Using jQuery's each method to implement dynamic fallback with JSON data

Is it possible to set a fallback function dynamically from an AJAX JSONP call? I've been trying, but it doesn't seem to work. I'm not sure if I'm doing it right. Here's what I have: var GetFacebookData = function (data) { ...