JQuery - Slide to a specific position in a div with overflow: hidden

I have many intervals in div s overflow:hidden;. Since the width of the div is fixed, only 5 spans are visible at first. At the touch of a button, how can I slide to a certain gap, what makes it visible?

LIVE JSFIDDLE

JQuery

$("#gobtn").click(function (e)  {
    $("span.selected").removeClass('selected');
    var s = $('#nr').val();
    $("#c" + s).addClass('selected');
  //$("#c" + s).??? 
});

HTML

<div class='container'>
    <div class='circles'>
        <span id='c1'>1</span>  
        <span id='c2'>2</span>
        <span id='c3'>3</span>
        <span id='c4'>4</span> 
        <span id='c5'>5</span>  
        <span id='c6'>6</span>  
        <span id='c7'>7</span>
        <span id='c8'>8</span> 
        <span id='c9'>9</span>  
        <span id='c10'>10</span> 
    </div>
</div>
<br /><br/>
Go to circle nr. <input id='nr' type="text" /> 
<input type='button' id='gobtn'  value='go!'>

CSS

span {
    border: solid 1px Silver;
    height: 14px;
    width: 14px;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    float: left;
    background-color: #fff;
    cursor: pointer;
    font-size:10px;
    text-align:center;
    margin-right:50px;
}
.circles {
    width:9000px;
}
.container {
    width:300px;
    display:block;    
    overflow:hidden;
    border:solid 2px #eee;
    padding:10px;
}
.selected
{
    background-color:Yellow;
}
+5
source share
2 answers

Add position: relativeto class circles. And set the property leftusing the method animate.

I made one. Give it a try. You can improve it however you want.

+5
source

Animate the left margin of a class circlesin a function click:

$("#gobtn").click(function (e)  {
    $("span.selected").removeClass('selected');
    var s = $('#nr').val();
    $("#c" + s).addClass('selected');

    $(".circles").animate({
        marginLeft: '-' + (parseInt(s-1) * 40) + 'px'
    }, 500);
});

http://jsfiddle.net/LdzTH/15/

Still needs fine tuning to get the numbers exactly where you want them, i.e. should always be in the middle.

+1
source

All Articles