简体   繁体   English

函数被多次调用

[英]Function gets called many times

I use jquery to mouseenter and mouseleave , the problem is when I pass from one div to another div, it run again, so I just need to run one time one mouse enter and mouse leav. 我使用jquery进行mouseentermouseleave ,问题是当我从一个div传递到另一个div时,它再次运行,因此我只需要运行一次鼠标进入和鼠标离开。

<div class="row">
  <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
  <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
  <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
  <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
</div>


body {
    height:1000px;
    width:100%;
    background-color:#F0F0F0;
}

#header_div {
    width:100%;
    height:100px;
    position:fixed;
    top:0;
    left:0;
}
#header_div img {
    max-height: 100%;
    max-width: 100%;
}

$(document).ready(function () {
   $(".header").mouseleave(function () {
       $(".header").animate({
           height: "50px"
       }, 600);
   });

   $(".header").mouseenter(function () {
       $(".header").animate({
           height: "100px"
       }, 600);
   });
})

There is a Jsfiddle of my problem: https://jsfiddle.net/DTcHh/21266/ 我的问题有一个Jsfiddle: https ://jsfiddle.net/DTcHh/21266/

If you want to use a "master" header... 如果您想使用“主”标题...

If you want to use a "master" header that would apply this to all of your child headers, then you could use a similar approach to your original example and target the mouseover and mouseleave events for your main header and then apply the animation to all of the child elements : 如果要使用将其应用于所有子标题的“主”标题,则可以对原始示例使用类似的方法,并针对主标题指定mouseovermouseleave事件,然后将动画应用于所有子元素:

<div id='main-header' class="row">
        <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
        <!-- More headers omitted for brevity -->
</div>

along with : 随着 :

 // When your main header is hovered, animate all of the sub headers
 $("#main-header").mouseleave(function () {
       $('.header').animate({  height: "50px" }, 600);
 });
 $("#main-header").mouseenter(function () {
       $('.header').animate({ height: "100px" }, 600);
 });

Example (Single Main Header) 示例(单个主标题)

在此处输入图片说明

 $("#main-header").mouseleave(function() { $('.header').animate({ height: "50px" }, 600); }); $("#main-header").mouseenter(function() { $('.header').animate({ height: "100px" }, 600); }); 
 body { height: 1000px; width: 100%; background-color: #F0F0F0; } #header_div { width: 100%; height: 100px; position: fixed; top: 0; left: 0; } #header_div img { max-height: 100%; max-width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='main-header' class="row"> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_02.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_03.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_04.jpg" /> </div> </div> 

If you want to target individual headers... 如果要定位单个标头...

Currently, every one of your elements has the class header, which is going to trigger the event on it's own, which may be what you are going for : 当前,您的每个元素都有一个类头,它将单独触发事件,这可能就是您想要的:

<div class="row">           
        <div class="col-md-4 header" >
            <!-- Image omitted for brevity -->
        </div>
        <div class="col-md-4 header" >
            <!-- Image omitted for brevity -->
        </div>
        <div class="col-md-4 header" >
            <!-- Image omitted for brevity -->
        </div>
        <div class="col-md-4 header" >
            <!-- Image omitted for brevity -->
        </div>
</div>

This effect is magnified because each time a mouseover or mouseleave event occurs, it's animating every header element : 放大效果是因为每次发生mouseovermouseleave事件时,它都会对每个header元素进行动画处理:

// When any header is hovered over
$(".header").mouseleave(function () {
       // Animate every header
       $(".header").animate({  height: "50px" }, 600);
});

If you only want the animation to affect the header being hovered, simply change your inner animate selector to use $(this) instead of $('.header') : 如果只希望动画影响被悬停的标题,只需更改内部动画选择器以使用$(this)而不是$('.header')

$(".header").mouseleave(function () {
    // Only animate the current element
    $(this).animate({height: "50px" }, 600);
});

$(".header").mouseenter(function () {
    // Only animate the current element
    $(this).animate({ height: "100px"}, 600);
});

Example (Individual Child Headers) 示例(单个儿童标题)

在此处输入图片说明

 $(document).ready(function() { $(".header").mouseleave(function() { $(this).animate({ height: "50px" }, 600); }); $(".header").mouseenter(function() { $(this).animate({ height: "100px" }, 600); }); }) 
 body { height: 1000px; width: 100%; background-color: #F0F0F0; } #header_div { width: 100%; height: 100px; position: fixed; top: 0; left: 0; } #header_div img { max-height: 100%; max-width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_02.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_03.jpg" /> </div> <div class="col-md-4 header"> <img src="http://www.mandroid.in/demo/srems/images/news_image_04.jpg" /> </div> </div> 

Add a master-header class at the parent div. 在父div上添加一个master-header类。

 <div class="row master-header">
    <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
    <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
    <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
    <div class="col-md-4 header" ><img src="http://www.mandroid.in/demo/srems/images/news_image_01.jpg" /></div>
</div>

Then do the event on that: 然后执行以下操作:

$(document).ready(function () {
   $(".master-header").mouseleave(function () {
       $(".header").animate({
           height: "50px"
       }, 600);
   });

   $(".master-header").mouseenter(function () {
       $(".header").animate({
           height: "100px"
       }, 600);
   });
})

Add a height (and maybe width too): 添加一个高度(也许还有宽度):

.master-header {
  height: 400px;
}

Also, consider detaching the mouseenter event if it's already animating Reattach once it's done animating. 另外,如果动画已经完成,请考虑分离mouseenter事件。

If I understood your intention correctly, then your problem is not with the event but with the animation: 如果我正确理解了您的意图,那么问题不在于事件,而在于动画:

$(document).ready(function () {
   $(".header img").mouseleave(function () {
       $(this).stop().animate({
           height: "50px"
       }, 600);
   });

   $(".header img").mouseenter(function () {
       $(this).stop().animate({
           height: "100px"
       }, 600);
   });

}) })

Let me know if I got your intention right. 让我知道我是否正确。 Fiddle . 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM