简体   繁体   English

在jquery中制作动画时div的不当行为

[英]Misbehavior of div when tring to animate in jquery

I trying to animate a small child div inside a parent div. 我试图在父div中设置一个小子div。 And the animation is to move the child div -60px upwards when mouse enter to parent div and child div will return to its original position when mouse leave the parent div. 当鼠标离开父div时,动画将向上移动子div -60px,当鼠标离开父div时,子div将返回其原始位置。 And its working properly. 它的工作正常。 But When the mouse move directly towards the child div it move up but get down before mouse leaving from the parent div. 但是当鼠标直接向子div移动时,它会向上移动,但在鼠标离开父div之前会下降。 Please help me to solve this issue 请帮我解决这个问题

See this link 看到这个链接

My html code is 我的HTML代码是

<div id="box" align="center">
    <div id="minibox"></div>
</div>

css code is css代码是

#box {
    position:relative;
    top:100px;
    width:200px;
    height:100px;
    border:1px solid #ddd;
}
#minibox {
    position:relative;
    width:50px;
    height:50px;
    border:1px solid #333;
    background-color:green;
}

jquery code is jquery代码是

$("#box").mouseenter(function () {
    ("#minibox").animate({
        top: "-60px"
    }, {
        duration: 180
    });
});
$("#box").mouseout(function () {
    $("#minibox").animate({
        top: "0px"
    }, {
        duration: 180
    });
});

使用mouseleave而不是mouseout。

$("#box").mouseleave(function(){...});

This kind of thing should NOT be done with jQuery. 这种事情不应该用jQuery来完成。

Raw CSS: 原始CSS:

#minibox { /* you can merge this with existing styles */
    top:0;
    transition: top 180ms ease;
    -webkit-transition: top 180ms ease;
}
#box:hover #minibox {
    top:-60px;
}

Demo 演示

The miniboy is a parent of the box, so the mouse-event will be executed by overing the minbox too. 迷你小盒子是盒子的父母,因此鼠标事件也将通过覆盖minbox来执行。 try this: 尝试这个:

if($(e.target).closest('#minibox').length)

or: http://jsfiddle.net/FE3jD/7/ 或者: http//jsfiddle.net/FE3jD/7/

Maybe you must be more specific on what happens in each case. 也许你必须更具体地了解每种情况下会发生什么。 Made this simple example, http://jsfiddle.net/blackjim/FE3jD/13/ 做了这个简单的例子, http://jsfiddle.net/blackjim/FE3jD/13/

var $box = $("#box"),
    $minibox = $("#minibox"),
    animationIsDone = false,
    miniBoxMoved = false,
    onDone;

$minibox.mouseenter(function (e) {
    if (miniBoxMoved) {
        e.stopPropagation();
    }
});

$box.mouseenter(function () {
    animationIsDone = false;
    miniBoxMoved = true;

    $minibox.animate({
        top: "-60px"
    }, {
        duration: 180,
        complete: function () {
            animationIsDone = true; // animation ended
            if (typeof onDone === 'function') {
                onDone();
            }
        }
    });

});

$box.mouseleave(function () {

    if (animationIsDone) {
        whenOutOfBox(); // call directly our callback
        onDone = ''; // not needed now
    } else {
        onDone = whenOutOfBox; //    pass it to the animation ending
    }
});


var whenOutOfBox = function () {
    $minibox.animate({
        top: "0px"
    }, {
        duration: 180,
        complete: function () {
            miniBoxMoved = false;
        }
    });
};

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

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