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

Error: The value of 'v4' property is not defined and cannot be read

I am encountering an issue with Vue.js: vue.esm.js?efeb:628 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'v4' of undefined" found in ---> <AddTodo> at src/components/AddTodo.vue <App> at src/Ap ...

The crosshair functionality in Zing Chart is causing a CPU leak

After enabling the crosshair feature on my chart, I noticed a significant issue when using Chrome 57 (and even with versions 58 and ZingChart 2.6.0). The CPU usage spikes above 25% when hovering over the chart to activate the crosshair. With two charts, th ...

Unable to retrieve this object because of a intricate JavaScript function in Vue.js

For my VueJs project, I am utilizing the popular vue-select component. I wanted to customize a keyDownEvent and consulted the documentation for guidance. However, I found the example provided using a mix of modern JS techniques to be quite cryptic. <tem ...

Tips for concealing a field within a form using jQuery

I've been working on a script to hide an input field that I want to load dynamically with other form fields. To assist me in this process, I created a small module. /** * Implements hook_form_alter(). */ function editorhide_form_alter(&$form,$f ...

Interactive sidebar scrolling feature linked with the main content area

I am working with a layout that uses flexboxes for structure. Both the fixed sidebar and main container have scroll functionality. However, I have encountered an issue where scrolling in the sidebar causes the scroll in the main container to activate whe ...

The input field is failing to capture the final character entered

Is there a way to store all input files in a single object and use the information in a graph? Currently, when I enter the first character, it creates an empty object, so the last character I enter is not captured in the object. Any suggestions on how to ...

Issues with utilizing Fetch API and JSON Data

I'm encountering some difficulties while trying to interact with my json file. I am using the fetch API to retrieve my json file but, unfortunately, when I log the response to the console, I don't see any data returned. Instead, what appears is a ...

Showing data from a Node.js Express application in a Jade template file

I am encountering an issue with my simple jade page where not all variables passed from the javascript route are displaying correctly. I have attempted to implement the solution described in this answer, but it seems to be ineffective in my case. My goal i ...

"Employing AJAX along with jQuery in .NET often leads to the error message stating, 'The requested resource was not

Could use some guidance on setting up a simple Ajax call in .NET Any suggestions? [WebMethod] public string Greetings() { return "Hello World"; } When I try to access the webmethod through my browser using this URL: I encounter the error: "The r ...

What is the best way to add new input fields dynamically using the push() function in AngularJS?

I'm currently working on implementing an add more field feature in angularjs. Here is the code that I am using: Javascript <script> function FruitsController($scope){ var div = 'Apple'; $scope.fruits= ...

List of JQuery Elements

Being able to reference my JQuery selectors from an array instead of individually would be much more convenient. I thought that by using the following logic, I could achieve this: var reference = $('.class'); reference[0].<function>; As ...

Issue: NG04002 encountered post migration from Angular to Angular Universal

Having recently created a new Angular app and converted it to Angular Universal, I encountered an issue when running the project using npm run dev:ssr. The error displayed in the terminal is as follows: ERROR Error: Uncaught (in promise): Error: NG04002 Er ...

Sass fails to import the theme for the angular material checkbox

I'm facing an issue where, despite specifying imports in my SCSS file and setting up a custom theme, the checkbox styles are not visible except for those related to typography. This is what my SCSS file looks like: @import "~bootstrap/scss/bootstrap ...

Unlocking the power of accessing nested data in JSON files dynamically

Building a new feature that allows users to input a word, choose the language, and receive the definition along with an example using the API service. To retrieve the desired data at position 0 of "exclamation" in the "meaning" section. This ensures that ...

Translating from a higher-level programming language to a lower-level programming language

Is compilation effectively the transformation of high-level programming languages (HLL) into machine code or low-level language? If so, why is TypeScript (a HLL) compiled to JavaScript (also a HLL) instead of being compiled to a low-level language? ...

How can a div tag and its class be nested using CSS in Angular?

Can someone help me with nesting a div tag with the "error" class in CSS? <div [class]="{error: condition}">{{ message }}</div> I want to know how to have the .error selector inside the div selector in the CSS file. Note: The error ...

The system has detected an invalid NSStringEncoding value of 0x8000100 while converting NSAttributedString to HTML

I encountered a warning in the debugger while using XCode: It detected an incorrect NSStringEncoding value of 0x8000100 when converting an NSAttributedString to html data. I am unsure of what is causing this issue or how to resolve it. Below is the code sn ...

The code is malfunctioning when it comes to scrolling, it is not behaving as I

I am currently utilizing jQuery to implement an Event Scroll that will assign a value to my div as the window is scrolling. Below is the code I have written, please let me know if there are any errors and how I can make it functional: $(window).scroll(fun ...

Could the absence of a tree view in the div be due to an error in the positioning of the DOM

I'm currently working on displaying a tree structure with specific child elements inside a div using JavaScript and HTML. Despite creating all the necessary objects and ensuring that the data is correctly read from the JSON file (refer to the "data" i ...

How can I utilize ng-repeat in AngularJS to iterate through an array of objects containing nested arrays within a field?

Here is the structure of an array I am working with: 0: {ID: null, name: "test", city: "Austin", UserColors: [{color: "blue"},{hobby:"beach"} ... ]} }... I am currently attempting to ng-repeat over this initial array in my code. However, when I tr ...