Mouseover and mouseleave trigger every time I move the mouse inside the given element

最后都变了- 提交于 2019-11-30 08:43:29
John Shepard

Try using the .mouseenter event instead of the .mouseover

I think that will do!

You may need to use mouseenter instead of mouseover and mouseleave instead of mouseout

Or you may try and use .hover function as below,

jQuery(document).ready(function(){
    jQuery("#dropdown-menu-create .dropimage").hover(function(){ //mouseenter
        jQuery("#dropdown-menu-create .toggle").animate({height:"154px"},200);
    }, function(){ //mouseleave
        jQuery("#dropdown-menu-create .toggle").animate({height:"0"},200);
    });
});

Since you are trying to implement this on a container, your correct event handler are mouseenter and mouseleave.

Example:

$("#dropdown-menu-create .dropimage").mouseenter(function(){
    $("#dropdown-menu-create .toggle").animate({height:"154px"},200);
});
$("#dropdown-menu-create .dropimage").mouseleave(function(){
    $("#dropdown-menu-create .toggle").animate({height:"0"},200);
});

adding a .stop() before your .animate() will help fix this. by stopping the previous animation this prevents the animation firing multiple times if the user moves their mouse over the element many times quickly

Although this may not be completely relevant, I had a similar issue. Everything I would move my most over the div, the event would fire. To solve it, I realized that when the 'hover' event was triggered, the toggled div had become the one my mouse was over. To fix it, I just added an event on that one to fade it out on mouse leave.

    .profile-about {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 5%;
      left: 0;
      z-index: 2;
      display: none;
    }

.profile-image {
  max-height: 300px;
  max-width: 300px;
  -webkit-box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.75);
} 

    $('.profile-image').hover(function(){
      $(this).next().fadeIn(100);
    });

    $('.profile-about').mouseleave(function(){
      $(this).fadeOut(100);
    });


    <img src="img/somebody.jpg" class="profile-image">
                    <div class="profile-about">
                        <p>
                         Hello I am somebody!
                        </p>
                    </div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!