What could be causing the hover effect to not work on other elements within the div?


I am working on creating a card that displays only the image when not hovered, but expands and reveals additional information in a div to the right of the image when hovered. Unfortunately, when I hover over the image and then move towards the adjacent div, everything starts shaking, indicating an issue with the hover effect. I've attempted to adjust the hover effect for both the image and the parent div containing them, but the problem persists. Any suggestions on how to resolve this?

$(document).ready(function(){
    document.querySelector(`.personCard-0 .imgCard img`).addEventListener("mouseover", () => hoverAnimation(0), false);
    document.querySelector(`.personCard-0 .imgCard img`).addEventListener("mouseout", () => disableHoverAnimation(0), false);
    })
    
    const hoverAnimation = (index) => {
    console.log('inside add animation');
    $(`.personCard-${index}`).toggleClass('active');
    $(`.personCard-${index} .personalInfoCard`).toggleClass('active');
}

const disableHoverAnimation = (index) => {
    console.log('inside remove animation');
    $(`.personCard-${index}`).toggleClass('active');
    $(`.personCard-${index} .personalInfoCard`).toggleClass('active');
}
*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 50vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
.cards{
    display: flex;
}
.personCard{
    display: flex;
    margin: 10px;
    transition: all 0.4s ease-in-out;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.63);
    border-radius: 10px;
    overflow: hidden;
}
.personCard.active{
    transform: scale(1.5);
}
.imgCard{
    height: 200px;
    width: 130px;
    overflow: hidden;
    transition: all 0.4s ease-in-out;
}

.imgCard img{
    height: 200px;
    width: 130px;
}
.personalInfoCard{
    background-color: palevioletred;
    display: flex;
    height: 200px;
    width: 0px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: -1;
    font-size: 14px;
    transition: all 0.4s ease-in-out;
}
.personalInfoCard.active{
    width: 200px;
    display: flex;
    z-index: 1;
    height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
    integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous">
    </script>
    <title>Document</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="cards">
        <div class="personCard-0 personCard" >
            <div class="imgCard">
                <img src="https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="">
            </div>
            <div class="personalInfoCard">
                <p>Name: Rand name</p>
                <p>Age: Rand age</p>
                <p>Job: Rand job</p>
                <p>Study: Rand proffesion</p>
            </div>
        </div>
    </div>
</body>
<script src="script.js"></script>
</html>

Answer №1

Reconfigure your target to focus on the card container so that even as it expands, you will still be positioned over it. The current setup targets the image, causing a mouseout trigger when it moves left.

$(document).ready(function() {
  document.querySelector(`.personCard-0`).addEventListener("mouseover", () => hoverAnimation(0), false);
  document.querySelector(`.personCard-0`).addEventListener("mouseout", () => disableHoverAnimation(0), false);
})

$(document).ready(function() {
  document.querySelector(`.personCard-0`).addEventListener("mouseover", () => hoverAnimation(0), false);
  document.querySelector(`.personCard-0`).addEventListener("mouseout", () => disableHoverAnimation(0), false);
})

const hoverAnimation = (index) => {
  console.log('inside add animation');
  $(`.personCard-${index}`).toggleClass('active');
  $(`.personCard-${index} .personalInfoCard`).toggleClass('active');
}

const disableHoverAnimation = (index) => {
  console.log('inside remove animation');
  $(`.personCard-${index}`).toggleClass('active');
  $(`.personCard-${index} .personalInfoCard`).toggleClass('active');
}
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  height: 50vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cards {
  display: flex;
}

.personCard {
  display: flex;
  margin: 10px;
  transition: all 0.4s ease-in-out;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.63);
  border-radius: 10px;
  overflow: hidden;
}

.personCard.active {
  transform: scale(1.5);
}

.imgCard {
  height: 200px;
  width: 130px;
  overflow: hidden;
  transition: all 0.4s ease-in-out;
}

.imgCard img {
  height: 200px;
  width: 130px;
}

.personalInfoCard {
  background-color: palevioletred;
  display: flex;
  height: 200px;
  width: 0px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: -1;
  font-size: 14px;
  transition: all 0.4s ease-in-out;
}

.personalInfoCard.active {
  width: 200px;
  display: flex;
  z-index: 1;
  height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous">
  </script>
  <title>Document</title>

  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="cards">
    <div class="personCard-0 personCard">
      <div class="imgCard">
        <img src="https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="">
      </div>
      <div class="personalInfoCard">
        <p>Name: Rand name</p>
        <p>Age: Rand age</p>
        <p>Job: Rand job</p>
        <p>Study: Rand proffesion</p>
      </div>
    </div>
  </div>
</body>
<script src="script.js"></script>

</html>

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 Vuex to Access a Component's Property in Vue

Utilizing Vuex in my app is integral for executing asynchronous tasks, such as logging a user into the application. Upon successful login and execution of axios.then(), I aim to notify the component from which I invoked this.$store.dispatch('login&apo ...

What is the best way to target outer elements only using CSS selectors?

Having trouble with a CSS selector: $("td.cv-table-panel-element") This selector returns a list of elements, shown in the screenshot linked below: https://i.stack.imgur.com/RoVMj.png Each element represents a column in a table with unique content. The ...

Aligning elements vertically in Bootstrap 4 with the power of flexbox

Seeking assistance with a frustrating issue in Bootstrap 4: I tried to implement the align-self-start flexbox alignment using the code from this page . However, the example provided on that page does not seem to work. Is there a simpler way to achieve the ...

JQuery: Creating a visually striking screen flash

How can I make the window or page flash/blink using JQuery or plain JavaScript? I also want to get the tab to flash at the same time. Currently, the code I have is: var flash = true; var alert = setInterval(function(){ if (flash) //doFlash ...

Fade in and fade out elements with jQuery using opacity in Internet Explorer

I have encountered an unusual issue in Internet Explorer involving a CSS Overlay used for a lightbox. The problem arises when I apply the fadein and fadeout effects using jQuery - everything seems to work smoothly except in IE. Instead of displaying a smo ...

Encountering an issue when running npm build on server

I attempted to deploy my application on the hosting server and encountered the following error: Failed to compile. ./node_modules/next/dist/pages/_app.js Error: failed to process The global thread pool has not been initialized.: ThreadPoolBuildError { kin ...

Having trouble with the functionality of expanding rows in Kendo grid

I am facing an issue with my Kendo grid that is populated from a SQL database. The expand feature works perfectly and displays a different Kendo grid in the expanded row when the program is first launched. However, if I perform a new search and get differe ...

What distinguishes submitting a form from within the form versus outside of it?

When the code below, you will see that when you click btnInner, it will alert 'submit', but clicking btnOuter does not trigger an alert. However, if you then click btnInner again, it will alert twice. Now, if you refresh the page: If you first ...

Issue encountered: Unable to add MongoDB object to database

Recently, I have encountered an issue with a script that looks like this: use first_db; advertisers = db.agencies.find( 'my query which returns things correctly ); close first_db; use second_db; db.advertisers.insert(advertisers); This error mess ...

View the Outcome of the Latest Form Submission on a Fresh Page

I have created a form that stores input values in a database table. Now, I want to display these results on another page once the form is submitted. However, I am struggling with how to retrieve and display all the values from the database record on the ne ...

Interactive Jquery block

Hello there, I am currently working on a code snippet that involves extracting the href and adding it to a parent block... $(".new-dev").hover(function(){ $(this).css('cursor','pointer'); $('this').$('a').c ...

Error: Improper hook call detected with no hooks being used (material-ui v5)

I've created the following basic application: import Grid from '@material-ui/core/Grid'; function App() { return ( <div className="App"> <Grid container spacing={2}> <Grid item xs={8}> ...

Notification feature experiencing technical difficulties

I have encountered an issue with a simple message form that I created. While the message is sent successfully and a thank you page is displayed, there are two errors that occur. The first error is: Notice: Undefined index: name in public_www/n..../contact- ...

An illustration of HTML frames

Is this method effective? <html> <body> <center> <frameset rows="50%,50%"> <frame src="images.html"> <frameset> <form action="abc.html"> <input type="submit" name="abc page" value="abc page"> </form> & ...

Tips for prefixing all URLs with "#!" for use in AJAX requests

I am working on an Ajax site and I need to add "#!" before all internal URLs from my site when a visitor clicks on them (external URLs should be unaffected). For example, clicking on a URL will change it to . I have tried writing a jQuery script for this ...

Express does not provide a 304 response code for static json files

I have a situation where I am utilizing express.static to serve a large static json file. It seems that express.static is always returning a 200 status code for the static json file, even when it remains unchanged. Given the file's size and the speci ...

The img-fluid class is not properly adjusting the height of the image

I'm currently working with Bootstrap 4 and facing a challenge with creating a grid of 4 images - 2 on top and 2 at the bottom. I've applied the img-fluid class, but the image resizing is based solely on width, leading to the height being too larg ...

What could be causing my THREE.js Documentation Box to malfunction?

I am a newcomer trying to get the hang of THREE.js. I delved into the THREE.js Documentation and attempted to implement the code, but when I loaded my HTML page, it appeared blank. I am at a loss for what to do next. I utilized Visual Studio Code for codin ...

Defining data types for an array of objects in a useState hook

I'm having trouble understanding the issue with my code. interface dataHistory { data: string, before: string | number, after: string | number, } I have an interface defined outside of the Functional Component and inside I specify its struct ...

I am experiencing an issue with my jQuery ajax where my return function is not working as expected

I'm currently working on the following code snippet: $("#upvote").click(function(){ var up = parseInt(document.getElementById('voteScore').innerHTML); up++; document.getElementById('voteScore').innerHTML = up; $.aj ...