最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery slide div left to right on hover - Stack Overflow

programmeradmin2浏览0评论

I'd like to fix this jquery left to right div on hover:

HTML:

<div class="holdingbox">
<span class="rightbox">
    <span class="content">Kenyér</span>
</span>
<span class="leftbox">></span>

jQuery:

$('.holdingbox').hover(function(){
    $('.rightbox').animate({width: '90px'}, 1000)
}, function(){
    $('.rightbox').animate({width: '-0'}, 1000)
});

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: / -> slider previous next arrow on hover (I need that!!)

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?

P.S.: I guess this problem can be solved by .stop() function. How?

I'd like to fix this jquery left to right div on hover: http://jsfiddle/PXLJG

HTML:

<div class="holdingbox">
<span class="rightbox">
    <span class="content">Kenyér</span>
</span>
<span class="leftbox">></span>

jQuery:

$('.holdingbox').hover(function(){
    $('.rightbox').animate({width: '90px'}, 1000)
}, function(){
    $('.rightbox').animate({width: '-0'}, 1000)
});

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./ -> slider previous next arrow on hover (I need that!!)

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?

P.S.: I guess this problem can be solved by .stop() function. How?

Share Improve this question edited Mar 10, 2014 at 14:51 Liam 29.8k28 gold badges138 silver badges202 bronze badges asked Mar 10, 2014 at 14:47 GBeniGBeni 531 gold badge2 silver badges7 bronze badges 1
  • 2 Soooo, you've formatted the fiddle as code to get around the links to jsfiddle should include code. Did you not think that error was there for a reason? I.e. links to jsfiddle should also include the code? – Liam Commented Mar 10, 2014 at 14:50
Add a ment  | 

1 Answer 1

Reset to default 4

You can use .stop() to stop the current animation: http://jsfiddle/PXLJG/2/

$('.rightbox').stop().animate({width: '-0'}, 1000)
发布评论

评论列表(0)

  1. 暂无评论