[英]JS-hover side effect with element outside a div
I created a simple box with a small CSS-arrow at the upper right border. 我在右上角创建了一个带有小CSS箭头的简单盒子。 This box will be displayed via JS as a hover event over
.item_add
is triggered ( http://jsfiddle.net/357nf0ht/6/ ). 此框将通过JS显示为触发
.item_add
的悬停事件( http://jsfiddle.net/357nf0ht/6/ )。
But here is the problem: As you move the mouse slowly over the .arrow
it gets messed up. 但问题出在这里:当你将鼠标慢慢移动到
.arrow
它会变得混乱。 I think it is because of the fact, that the arrow is outside of the container. 我认为这是因为箭头在容器之外。 So I hope there is a improvement in the HTML/CSS Layout to solve this 'bug'.
所以我希望HTML / CSS布局有一个改进来解决这个'错误'。
<div id="item_add">
<header>X</header>
<div class="body">
Content
</div>
<div class="arrow"></div>
</div>
CSS: CSS:
#item_add {
display: none;
position: absolute;
left: 5.5em;
width: 2em;
border: 1px solid #ddd;
background-color: #f7f7f7;
color: #aaa;
padding: 0px 6px;
}
#item_add .body {
display: none;
}
#item_add .arrow {
width: 0px;
height: 0px;
-webkit-transform:rotate(360deg);
border-style: solid;
border-width: 5px 0 5px 7px;
border-color: transparent transparent transparent #f7f7f7;
top: 5px;
right: -7px;
position: absolute;
}
You could use CSS animations instead of JS/jQuery animations (CSS animations are hardware accelerated, JS animations not). 您可以使用CSS动画而不是JS / jQuery动画(CSS动画是硬件加速,JS动画不是)。 jQuery fires a new mouseenter/mouseleave event if you hover over a child element.
如果将鼠标悬停在子元素上,jQuery将触发一个新的mouseenter / mouseleave事件。 With the CSS
:hover
you don't get this issue. 使用CSS
:hover
你不会遇到这个问题。
Be careful to use the correct transition durations and delays. 小心使用正确的转换持续时间和延迟。 I also changed the arrow to the
:after
pseudo element which saves some code. 我还将箭头更改为
:after
伪元素,保存了一些代码。
Set the max-height
attribute of #item_add:hover .body
to some big value to ensure that everything is visible. 将
#item_add:hover .body
的max-height
属性设置为一个大值,以确保一切都可见。
Check it out in this fiddle: http://jsfiddle.net/7n9jxo9c/ 在这个小提琴中查看: http : //jsfiddle.net/7n9jxo9c/
#item_add {
...
transition: width 200ms ease-in-out 200ms, left 200ms ease-in-out 200ms;
}
#item_add:hover {
width: 7em;
left: .5em;
transition-delay: 0s;
}
#item_add:hover .body {
max-height: 100px;
visibility: visible;
transition-delay: 200ms;
}
#item_add .body {
max-height: 0;
overflow: hidden;
visibility: hidden;
transition: visibility 0s ease-in-out 200ms, max-height 200ms ease-in-out 0s;
}
#item_add:after {
content: '';
...
/* same as #item_add .arrow */
}
I tried fiddling around it and it seems to show correctly if you just check if the box is allready animated. 我试图摆弄它,如果你只是检查盒子是否已经动画,它似乎正确显示。 Heres the change i made to your code:
以下是我对您的代码所做的更改:
$(document).on('mouseenter', '#item_add', function( event ) {
var isAnimating = $(this).is(':animated');
if(isAnimating == false){
$(this).animate({ "width": '7em', "left": '.5em' }, 200, function(){
$(this).find(".body").slideDown(200);
});
}
}).on('mouseleave', '#item_add', function( event ) {
var menue = $(this);
var isAnimating = menue.is(':animated');
if(isAnimating == false){
menue.find(".body").slideUp(200, function() {
menue.animate({ "width": '2em', "left": '5.5em' }, 200);
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.