user1009762 user1009762 - 1 year ago 57
jQuery Question

Jquery slider working good, but why does it break when try to make it centered?

I'm using a jquery slider plugin and I have it working (new to jquery/css/html), but when I try to center the slider it breaks. The red dot that moves around the circle does not center along with the pictures? I'd like to understand why this is and how can it be fixed? Thank you very much.


<div id="rotatescroll" class="centered">
<div class="viewport">
<ul class="overview">
<li><a href=""><img src="" /></a>
<li><a href=""><img src="" /></a>
<li><a href=""><img src="" /></a>
<li><a href=""><img src="" /></a>
<li><a href=""><img src="" /></a>
<div class="dot"></div>
<div class="overlay"></div>
<div class="thumb"></div>


img { border: 0; }
.centered {
margin: 0 auto;

#rotatescroll { height:300px; position:relative; width:300px; }
#rotatescroll .viewport{ height:290px; position: relative; margin:0 auto; overflow:hidden; width:290px; border-radius: 50%; }
#rotatescroll .overview { position: absolute; width: 798px; list-style: none; margin: 0; padding: 0; left: 0; top: 0; }
#rotatescroll .overview li { height:300px; width:300px; float: left; position: relative; }
#rotatescroll .thumb { background:url('') no-repeat 50% 50%; position: absolute; top: -3px; cursor: pointer; left: 137px; width: 100px; z-index: 200; height: 100px; }
#rotatescroll .dot { background:url('') no-repeat 0 0; display: none; height: 12px; width: 12px; position: absolute; left: 155px; top: 3px; z-index: 100; }
#rotatescroll .dot span { display: none; }
#rotatescroll .overlay {background:url('') no-repeat 0 0; pointer-events: none; position: absolute; left: 0; top: -2px; height:300px; width:300px; }

.highlight, .indicator{background-color:#FC0;}


interval: true,
dotsSnap: true,
intervalTime: 1000

Answer Source

When calculating the size of the container, margins are included into calculation and later used to determine the position of the red dot. A possible solution is to exclude margins when calculating container width by passing false or omitting the parameter:

containerSize =
    width  : $container.outerWidth()
,   height : $container.outerHeight()

If you don't feel like editing the script, you can try centering your div without using margins, for example:

.centered {    
    left: calc( 50% - 150px );

Or by using a wrapper div, like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download