Do not activate hover on the children of parents using triggers

Check out my demonstration here: http://jsfiddle.net/x01heLm2/

I am trying to achieve two goals with this code. Goal number one is to have the mini thumbnail still appear when hovering over the .box element. However, I do not want the hover event to be triggered if the user hovers over the click area (goal number two). Currently, I have bound the hover event to the parent element, which is .box, and this has achieved goal number one but I am stuck on achieving goal number two.

Can anyone provide guidance on how to prevent triggering the hover event when the user hovers over the click area first?

    $(document).ready(function() {
      $('.box').not('.box .box-lower').hover(function() {
        $(this).find('.productsThumbWrap').stop(true, true).animate({
          "margin-top": "+=108px"
        }, "normal");
        $(this).find('.productsThumbWrap img').stop(true, true).css({
          opacity: 1,
          'transition': 'opacity 0.3s ease 0.35s'
        });
      }, function() {
        $(this).find('.productsThumbWrap').stop(true, true).css({
          'margin-top': '92px'
        });
        $(this).find('.productsThumbWrap img').stop(true, true).css({
          opacity: 0,
          'transition': 'none'
        });
      });
    });
.box {
  width: 100%;
  height: auto;
  min-height: 290px;
  margin-bottom: 30px;
  border: 5px solid #000
}
.orange {
  background: orange
}
.box-lower {
  background: red;
  height: 80px;
}
.box-upper {
  position: absolute;
}
.productsThumbWrap {
  text-align: center;
  margin-top: 92px;
}
.productsThumbWrap img {
  padding: 14px 6px;
  display: inline;
  opacity: 0;
}
.click {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="col-lg-6">
  <div class="box">
    <div class="box-upper">
      <img src="http://placehold.it/398x200" />
    </div>
    <div class="productsThumbWrap">
      <img src="http://placehold.it/80x80" />
      <img src="http://placehold.it/80x80" />
      <img src="http://placehold.it/80x80" />
      <img src="http://placehold.it/80x80" />
    </div>
    <div class="box-lower">
      <button class="click">Click</button>
    </div>
  </div>
</div>

Answer №1

To simplify the process, consider adding a wrapper around the "hover to expand" section.
(View the demo: http://jsfiddle.net/ncbrr6py/ )

<div class="hoverExpand">
</div>

This wrapped segment will look like this:

<div class="hoverExpand">
    <div class="box-upper">
        <img src="http://placehold.it/398x200" />
    </div>
    <div class="productsThumbWrap">
        <img src="http://placehold.it/80x80" />
        <img src="http://placehold.it/80x80" />
        <img src="http://placehold.it/80x80" />
        <img src="http://placehold.it/80x80" />
    </div>
</div>

You can then adjust your javascript code as follows:

$(document).ready(function () {
    $('.hoverExpand').mouseenter(function () {
        $(this).find('.productsThumbWrap').stop(true, true).animate({
            "margin-top": "+=108px"
        }, "normal");
        $(this).find('.productsThumbWrap img').stop(true, true).css({
            opacity: 1,
            'transition': 'opacity 0.3s ease 0.35s'
        });
    });
    $('.box').mouseleave(function () {
        $(this).find('.productsThumbWrap').stop(true, true).css({
            'margin-top': '92px'
        });
        $(this).find('.productsThumbWrap img').stop(true, true).css({
            opacity: 0,
            'transition': 'none'
        });
    });
});

Note that mouseenter and mouseleave are now used instead of hover. Additionally, I have included some CSS:

.hoverExpand{
    overflow:hidden;
}

This CSS helps resolve display issues and avoids the need for a border around the control.

If you want the expander to contract when hovering over it, consider binding mouseleave to .hoverExpander as well. However, this may make clicking the button awkward as it shrinks away from the cursor.

An alternative approach is to utilize event.stopPropagation() within your event handlers to prevent events from bubbling up the DOM tree. You would bind an event to stop the bubbling:

$('.box-lower').hover(function(event){
    event.stopPropagation();
});

It's worth noting that running the old code may still trigger expansion when hovering near the border or padding below the button, highlighting the benefit of using a container.

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

Having difficulty in selecting the element

My current challenge involves using Appium to automate a framework I've created. After inspecting an element with Appium inspector, I'm attempting to click on the element within the DOM, even though it's not visible on the device screen. I t ...

CanJS utilizes mustache templates to escape HTML tags

I have a requirement to present a series of choices to the user. I am using a mustache template along with the CanJS JavaScript framework to showcase these options. The problem arises when attempting to display an option such as: Potato Rs. 12 The mustac ...

Utilizing the Page Method to Handle Warnings in $.ajax({}) Calls

I created a custom method in the .cs file of my Default.aspx page: [WebMethod] public static string ConvertToJSON(object data) { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); string json = jsSerializer.Serialize(data); retu ...

Adjust the height of the outer div automatically

Currently, I am attempting to dynamically set the height of div#container so that it adjusts based on its relative positioning and the absolute positioning of inner divs. Essentially, I want the inner divs to be centered within the container. <div id=" ...

Instructions on how to automatically close a Bootstrap 5 alert without using jQuery

With the removal of jQuery as a dependency in Bootstrap 5, I have been exploring ways to automatically dismiss an Alert after a set duration using raw Javascript. Below is a snippet of my current approach. I believe there is room for optimization or a bett ...

Using Javascript to dynamically add an element to an array with a unique index

Given let inputArray = []; $('some_selector').each(function() { let outer, inner; outer=$(this).parent().attr('some_property'); inner=$(this).attr('a_property'); if (!inputArray[outer]) inputArray[outer] = ...

What is the process for saving selected values from a drop-down list into a database?

Hey there, I'm a newcomer to PHP and ajax. When choosing an option from the drop-down list, a separate div function should appear where I need to insert both the selected option and input data in different divs. Sorry for my poor English. Any help fro ...

Using JavaScript to modify the -webkit-animation-play-state

Hello, I'm currently facing a challenge in changing my css3 -webkit-animation-play-state property from paused to running upon clicking on another div. Does anyone have any suggestions on how I can achieve this? I believe JavaScript might be the way to ...

Troubleshooting Ajax contact form's failure to refresh (PHP and Bootstrap 4)

I have successfully implemented this code on one website, but it is not working as expected on another website. It sends the email but refreshes the page and redirects to a contact.php page with a message. Despite double-checking everything, including cha ...

Use jQuery to drag an element and display controls in a designated area

I am trying to develop a tool that allows me to drag an image from one section to another, and upon dropping it, the following actions should take place: 1 - The dragged image should revert back to its original position 2 - A set of controls should be add ...

Navigating JSON/API data within a Vue.js component template: step-by-step guide

I've successfully implemented a code snippet that renders a list of songs here: https://jsfiddle.net/jeremypbeasley/guraav4r/8/ var apiURL = "https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=thisisheroic&period=7day&limit= ...

Looking for a list of events in Express.js?

I've been searching through the official documentation, but I couldn't find a list of events for express. Does anyone know if there's something like 'route-matched' so that I can use app.on('route-matched', () => {})? ...

The Art of Revealing an Element

Is it possible to manipulate a parent div element in JavaScript without affecting its child nodes? For example, transforming this structure: <div class="parent"> <div class="child"> <div class="grandchil ...

Service for Posting in Angular

I am looking to enhance my HTTP POST request by using a service that can access data from my PHP API. One challenge I am facing is figuring out how to incorporate user input data into the services' functionality. Take a look at the following code snip ...

Issue with Jquery AJAX success function specifically in Firefox browser, while other functions in the script are functioning correctly

I have 4 scripts using ajax, but one of them isn't functioning properly in Firefox. Even the alert in success doesn't trigger anything. There are no error messages, just nothing happening. However, it works perfectly fine in IE and Chrome. Belo ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: ht ...

Ensuring IconButton is perfectly aligned with Text in one straight line

I want to display IconButtons alongside plain text in a single block of text, but the result didn't turn out as I had hoped. Here is what I attempted: JS(ReactJS): <div> <span> <h3>Title</h3> <IconButton className ...

Encountered a snag while executing Powershell with Selenium: Error message - unable to interact with

Looking to update a textarea with a value? The script below triggers an error stating "element not interactable". This occurs because the textarea is set to "display:none". However, manually removing the "NONE" word allows the script to successfully set th ...

How can you apply margin to an element within a column in Bootstrap 4?

Within a column, I have several span elements: <div class="container-fluid"> <div class="row"> <div class="col"> <span class="myClass">some text</span> <span class="myClass">some text</span> ...

Angular URL changes causing template flickering

Currently, I am in the process of developing a small application using Angular, but I am encountering an issue with template flickering. This problem occurs when one template is displayed briefly and then immediately switches to another. In my base templa ...