I have been dabling with a javascript image panner to which I have hacked and shredded together from code on here… I have now abandoned this for a simpler method but need advice on how to do a couple of things.
I have this code for left and right buttons.
<div id="wrap">
<div class="entrance-hall pan-image">
</div>
</div>
<input type="button" value="Move Left" class="nav-left" onclick="pan_animate('-=20%')" />
<input type="button" value="Move Right" class="nav-right" onclick="pan_animate('+=20%')" />
and this for the javascript.
function pan_animate(px) {
$('.pan-image').animate({
'marginLeft' : px
});
}
It pans an image left or right 20% within the wrapping div however I want to…
- make it scroll smoothly rather than in increments of a percentage
- stop going past the edge of the left and right container
- start at the center of the image.
Not asking for much eh? Hope this makes sense and someone can help!
Cheers.
css added
#wrap {
margin-bottom:60px;
border:4px solid white;
overflow:hidden;
}
.pan-image {
position:relative;
width:2106px;
height:395px;
left:50%;
margin-left:-1053px;
}
/* -- ===entrance hall=== -- */
.entrance-hall {
background:url(/staging/kennawayhouse.org.uk/images/1-entrance-hall.jpg);
}
One way to achieve this is simply using
easingfunctions. From the jQuery animate api:Your code could be then, changed to:
And some smooth will be perceived.
This one will need some
csshack, butposition:absolute;left:5x;top:5pxmay solve your problem about the inner element. If you want an answer more accurate than may, you can post your css code.EDIT
Based on your css code, I’ve achieved a function that alert if your margin has trespassed the limit of it’s parent when moving:
You can check it a fiddle: http://jsfiddle.net/brunovieira/3wHn3/7/
In order to center your
$(".pan-image")elements you can use two approaches, the css one:position:relative;margin-left:auto;margin-right:auto, or, in case position relative is not an option, you can center your element using jQuery like:Again, depending of your coding, the conditions above answered may not work.