Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Three column layout with user controlled frame width.

I'm trying to create a three column layout with columns that a user can adjust on their on. I'm using jQuery to control the resizing of the columns and I'm almost there...

The two columns on the left adjust perfect, but the two columns on the right adjust opposite of the direction you drag your mouse. I can't seem to figure out how to get them to adjust the appropriate way.

Any thoughts or suggestions for improving this would be really appreciated! Here is a link to the jsFiddle: (http://jsfiddle.net/cleverdirt/hsaL9/)

1 Answer

I'm not the best with Javascript but this will get you close. The last thing you have to do is to make the width of the div with a class of "right" adjust properly, and that part I couldn't figure out.

    var offset = $(this).offset().left;
    var offset2 = $(".middle").offset().left;

    if (mousedownFirst) {
        $(".left").css("width", e.pageX - offset);
        $(".middle").css("left", e.pageX - offset);
    }
    if (mousedownSecond) {
        $(".middle").css("width", e.pageX - offset - offset2);
        $(".right").css("left", e.pageX);
        $(".right").css("width", e.pageX);
    }

Thank you for the inspiration!

I think I was able to get it going with the following, factoring in width's of the current elements in relation to the page.

    var offset = $(this).offset().left;
    var middleOffset = $(".middle").offset().left;

    var leftWidth = $(".left").width();
    var middleWidth = $(".middle").width();
    var pageWidth = $(document).width();

    if (mousedownFirst) {
        $(".left").css("width", e.pageX - offset);
        $(".middle").css("left", e.pageX - offset);
        $(".right").css("left", e.pageX - offset + middleWidth);
        $(".right").css("width", pageWidth - leftWidth - middleWidth);
    }

    if (mousedownSecond) {      
        $(".middle").css("width", e.pageX - offset - middleOffset);
        $(".right").css("left", e.pageX);
        $(".right").css("width", pageWidth - leftWidth - middleWidth);
    }

Here is the jsFiddle: http://jsfiddle.net/cleverdirt/hsaL9/6/

Nice work Chad!