1

I'm coding a slider and I have problems with the stylying of the container.

I have 3 div:

  1. A div that sets the width and height of the slider
  2. A container div with all the content divs (and the scroll for the slider)
  3. Many divthat show different contents each

What I want to do is apply a negative margin on the second div to slide the content.

LIVE example: http://jsbin.com/efuyix/7/edit

JS:

  function animate(element) {
  var start = new Date();
  var id = setInterval(function() {
    var timePassed = new Date() - start;
    var progress = timePassed / 600;
    if (progress > 1) progress = 1;
    element.style.marginLeft = -50 * Math.pow(progress, 5)+"px";
    if (progress == 1) {
      clearInterval(id);
    }
  }, 10);
}

CSS

  .example_path {
    overflow: hidden;
    width: 50px;
    height: 50px;
  }
  .example_block {
    min-width: 100px;
    height: 50px;
    float:left;
   }
  .example_in_block {
    width: 50px;
    height: 50px;
    float:left;
   }

HTML

<div class="example_path">
  <div class="example_block" onclick="animate(this)">
    <div class="example_in_block" style="background-color:blue;"></div>
    <div class="example_in_block" style="background-color:pink;"></div>
    <div style="clear:both;"></div>
  </div>
</div>

The problem: The width of .example_block has to be exactly the same or more than (amount of content divs .example_block * 50 [width size of content div] ) to work.

For example, if I set the width size of the .example_block to 90, the pink div will be below the blue div and not beside it.

I want the container div to be dynamic so I don't have to set the specific width size.

How can I do this?

2
  • Please briefly tell ehat you want to do with the div's (as in how you want it to be dynamic - are you gonna change the width by javascript of both div's?) Commented Nov 16, 2011 at 10:16
  • @tunetosuraj you can see how it's dont in the live example i gave in the top of the topic , i want to change the container margin to negative margin Commented Nov 16, 2011 at 10:19

2 Answers 2

1

Simply remove the float:left in the .example_block.

See http://jsbin.com/efuyix/9/edit

Sign up to request clarification or add additional context in comments.

Comments

0

Not with negative margins. You can probably set padding on one of the outer DIVs.

Also, min-width isn't going to be backwards compatible with older versions of IE.

Check this example: http://jsfiddle.net/5xBYN/6/

If the initial positioning is good, you can then use negative values on your container DIV (the third DIV) for top, left, right or bottom to achieve sliding.

Update:

Maybe this is closer to what you want. http://jsfiddle.net/5xBYN/7/

I'm still not sure what you are trying to do. Maybe edit the fiddle I posted and update your question with what I'm getting wrong if there is anything.

1 Comment

the second div is the container (example_block class)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.