views:

23

answers:

3

Hey all,

I was working on a slider using an unordered list. Standard structure, example (not the same code I'm using):

<div style="overflow: hidden;width: 500px;padding: 20px;position: relative">
    <ul style="width: 1000px;position: relative;padding: 0;margin: 0;list-style: none;">
    <li style="width: 500px;padding: 0;margin: 0;float: left;"></li>
    <li style="width: 500px;padding: 0;margin: 0;float: left;"></li>
    </ul>
</div>

using an li width of 500px and a div padding of 20px plus the width of 500px for example. And some jQuery like:

$(div ul).animate({left: position.left-500+"px"});

I noticed by accident through my mistake that the jQuery when sliding the ul left only would slide it 460px in this case. It seems that it is minusing the padding off automatically? And therefore the slided <ul> is off every time.

Any explanation as to why this is happening?

A: 

I guess sliding doesn't appreciate the padding of the parent. Note that thing you're sliding, the ul, doesn't have any padding at all.

Try setting the padding on the ul, not the container.

EMMERICH
Yeh, I just thought it was strange how jQuery got this a bit mixed up! if the ul had padding put on it, it wouldn't be very universal across the li's I think the best in that case would be to add padding to each li and then slide the total value of both width and padding, presuming jQuery didnt do the same here for each. Thanks anyway!
Stefan
A: 

i tried this and works next:

either change ul position to absolute

or animate like $('ul').animate({left: '-=520'+"px"}

20px added only in opera, in ie, ff '-=500' works as expected with relative position

this is my test code

    <div style="overflow: hidden;width: 500px;padding: 20px;position: relative;background-color: black;">
<ul style="width: 1500px;position: relative;padding: 0;  margin: 0;list-style: none; ">
<li style="width: 500px;padding: 0;margin: 0;float: left; background-color: blue;">-</li>
<li style="width: 500px;padding: 0;margin: 0;float: left;background-color: red;">-</li>
<li style="width: 500px;padding: 0;margin: 0;float: left;background-color: green;">-</li> 
</ul>
</div>  

 <script  type="text/javascript">
 $(document).ready(function(){
// $('ul').animate({left: '-=20'+"px"});       
 $('ul').animate({left: '-=500'+"px"});
 $('ul').animate({left: '-=500'+"px"});    

 })
</script>
samrockon
I thought that the padding was added onto the width so it would be 540px?
Stefan
if div width is 500px and padding 20px all child elements will have 460px width. so you see only first 460px of every li element.
samrockon
you should change div width to 540px or remove padding or set li width to 460px
samrockon
Sorry I feel like i'm being stupid, i've been making websites for 12 years now and I believe padding is added on as according to the box model spec after strict DOCTYPE was introduced. The content area of the parent div is 500px and it's got an additional padding of 20px that makes 540px and the child li's are 500px width and 0 padding which fits nicely in the parent divs content 500px, I don't understand where your coming from, is there any other way you can explain, thanks for your time :)
Stefan
yep, you are absolutely right))how can you get you position.left value? througt .offset().left ?
samrockon
it looks like if you change ul position to absolute or add 20px to ul left value when animating it will work
samrockon
Thats very strange as just because it's relative shouldn't cause jQuery to minus the parent padding off the distance you specify... very odd. Ah well now I know for next time and how to think about it/work around it! Thanks for your time!
Stefan
A: 

There are 20px left + 20px right = 40px padding in your first div when you subtract from width,that's 460px.

Padding unites with width of element.If you remove padding or increase width,that should do it.
Best Regards

Myra