【发布时间】:2024-01-22 13:31:01
【问题描述】:
我有一个简单的悬停动画;我分离了mouseenter 和mouseleave 函数来定义鼠标离开的条件。如果该条件属实;禁用 mouseleave 功能,但我无法让这个条件工作,javascript 忽略我的条件并运行 mouseleave 函数。
jQuery:
$('.bigBox').mouseenter(function() {
$('.trigger').stop().animate({'left':'-50px'},222);
$('.bigBox').stop().animate({'left':'-1px'},222);
});
var holdCondition = $('#hold').hasClass('selected');
//tried to disable mouseleave here but didnt work
if ( !holdCondition ) {//if #hold has not class selected
$('.bigBox').mouseleave(function() {
$('.trigger').stop().animate({'left':'-1px'},222);
$('.bigBox').stop().animate({'left':'-111px'},222);
});
}
$('.option').click(function(){
$('.option').removeClass('selected');
$(this).addClass('selected');
});
html:
<div class="bigBox">
<span class="trigger">X</span>
<span class="option selected">A</span>
<span class="option">B</span>
<span id="hold" class="option">Hold</span>
</div>
css:
.bigBox {
position:fixed; top:10%; width:100px; height: 20px;
left:-111px; border:1px solid #000; padding-left:5px;
}
.trigger {
position:fixed; top:10%; width:20px; height: 20px;
left:-1px; border:1px solid #000; text-align:right; padding-right:5px;
}
.option { margin:0 5px; cursor:pointer; }
.selected { color:#f00; }
【问题讨论】:
-
我已经编辑了你的代码检查这个jsfiddle.net/Bvr9B/14
标签: javascript jquery hover jquery-animate conditional-statements