javascript jQuery在悬停时从左到右滑动div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22303411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery slide div left to right on hover
提问by Benejj
I'd like to fix this jquery left to right div on hover: http://jsfiddle.net/PXLJG
我想在悬停时从左到右 div 修复这个 jquery:http: //jsfiddle.net/PXLJG
HTML:
HTML:
<div class="holdingbox">
<span class="rightbox">
<span class="content">Kenyér</span>
</span>
<span class="leftbox">></span>
jQuery:
jQuery:
$('.holdingbox').hover(function(){
$('.rightbox').animate({width: '90px'}, 1000)
}, function(){
$('.rightbox').animate({width: '-0'}, 1000)
});
CSS:
CSS:
div {
display : inline-block;
}
.holdingbox {
position: relative;
top: 0;
margin-left: 100px;
}
.leftbox {
position: relative;
top: 0;
left: 0;
display: inline-block;
font-size: 24px;
background-color: #ac193d;
color: #FFF;
font-weight: bold;
padding: 1px;
}
.rightbox {
position: relative;
display: inline-block;
overflow: hidden;
width: 0;
height: 30px;
vertical-align: top;
margin-right: 0;
}
.content{
width: 100px;
position: absolute;
background-color: #ac193d;
height: 29px;
left: 0;
top: 0;
right: 0;
color: #FFF;
padding-left: 5px;
}
For example: http://www.jamieoliver.com/-> slider previous next arrow on hover (I need that!!)
例如:http: //www.jamieoliver.com/-> 悬停时滑块上一个下一个箭头(我需要那个!!)
My problem is when I trying to hover on the ">" (arrow) it is working sliding left to right. But when I hovering again and again and again while animate sliding to 'width:0' and I leave my mouse from the div the animate don't stop and still hovering left to right for X times I hovered on the div. Can anyone solve this how can I fix this problem?
我的问题是当我尝试将鼠标悬停在“>”(箭头)上时,它正在从左向右滑动。但是,当我在动画滑动到“宽度:0”时一次又一次地悬停并且我将鼠标从 div 上移开时,动画不会停止并且仍然从左到右悬停 X 次,我在 div 上悬停。任何人都可以解决这个问题我该如何解决这个问题?
P.S.: I guess this problem can be solved by .stop() function. How?
PS:我猜这个问题可以通过 .stop() 函数解决。如何?
回答by Jonas Grumann
You can use .stop() to stop the current animation: http://jsfiddle.net/PXLJG/2/
您可以使用 .stop() 停止当前动画:http: //jsfiddle.net/PXLJG/2/
$('.rightbox').stop().animate({width: '-0'}, 1000)