tags:

views:

48

answers:

1

I have the following JS:

$(document).ready(function () {
    $(".LeftColumn").hide();
    $(".SidebarToggle").toggle(function () {
        $(this).addClass("active");
        $(this).text("Hide Sidebar");
        $(this).attr("title", "Hide Sidebar");
        //$(".LeftColumn").fadeIn("fast");
        $(".LeftColumn").show("slide", { direction: "left" }, 100);
        return false;
    },
    function () {
        $(this).removeClass("active");
        $(this).text("Show Sidebar");
        $(this).attr("title", "Show Sidebar");
        //$(".LeftColumn").fadeOut("fast");
        $(".LeftColumn").hide("slide", { direction: "left" }, 100);
        return false;
    });

Which basically shows and hides a div with a class of LeftColumn. The problem I have is that LeftColumn floats left and I have another div called Middle Column that floats left next to it. But when I do the animation the LeftColumn does a nice easing slide but the MiddleColumn will snap in and out to fill the void. How can I get the MiddleColumn to ease back and forth in relation to the LeftColumn (baring in mind that the MiddleColumn has no defined width).

Thanks

A: 

Do you know the width of the left column, the one that shows/hides? If yes, you could set position:absolute on the middle column and then animate its "left" value together with sliding the left column. Do I make sense?

EDIT: I believe the problem is due to float:left on the middle column. If the left column wouldn't be there, the middle column would be displayed on the most left. So, while the animation lasts, it thinks of the left column as if it's there. Once the animation is done, the left column is hidden, elements get realigned and you get the snapping effect.

So, assuming your mark up is something like this:

<div style="position:relative;">
  <div class="LeftColumn" style="float:left;"></div>
  <div class="MiddleColumn" style="position:absolute;left:200px;"></div>
</div>

Now in your javascript for showing:

$(".LeftColumn").show("slide", { direction: "left" }, 100);
$(".MiddleColumn").animate({'left': '200'}, 100);

and for hiding:

$(".LeftColumn").hide("slide", { direction: "left" }, 100);
$(".MiddleColumn").animate({'left': '0'}, 100);

So while animating the hide of the left column, you're also animating the position of your middle column. (I'm confused on why you're animating direction: "left" in both hide and show. Shouldn't on show be animating direction: "right"?)

Dimskiy
The left column is 200px wide. Can you share some example code? Thanks.
Cameron
The code works great. Only problem is that absolute breaks the layout, and the width of the MiddleColumn remains the same even though it has now taken up more screen space when it slides back into place.
Cameron
@Cameron - It's hard to give a good solution without seeing the whole markup. You could animate the width of the middle column, too. Although, I think you said you don't have the exact value of it. Which again, you might grab the width of the middle column while left column is expanded. Then on hiding the left column, animate the width of the middle column to [stored value] + 200
Dimskiy