JQuery / CSS - Moving a relative DIV using left / top

I have a DIV with the attribute "Position: relative;". Now there are three of these divs. They all get a unique identifier, etc.

Now, if I press DIV, I want it to plunge into a certain position in the document. Although I can not determine the left / top values, because if I use the "top" and "left", it will relate the position on the left to the parents.

Maybe a little unclear, but here's what I got.

CSS clickable div that will move.

div#left_hldr .switch_project {
    z-index: 1001;

    position: relative;

    margin: 10px 0px 0px 0px;

    cursor: pointer;    
}

Now, the jQuery code that I have right now.

// Open project.
$(".switch_project").live('click', function () {

    // Get the ID value.
    var More_Id = $(this).attr("id");

    // Explode the Id.
    var Id_Split = More_Id.split("_");

    // Get the project ID.
    var Project_Id = Id_Split[2];

    /*
        Replacement of the switch project div.

        1 ) Define the current position.

        2 ) Define the new position.

        3 ) Replace the DIV to the new position.

        4 ) Fade the new page in.

        5 ) Put the DIV back to its original position.
    */

    // Current Position.
    var Ori_Left = $(this).offset().left;

    var Ori_Top = $(this).offset().top;

    // New Position.    [ Based on the content_hldr container ]
    var New_Top = $("div#content_hldr").offset().top;

    var New_Left = $("div#content_hldr").offset().left;

    // Define the current div.
    var Div_Rep = $(this);

    // Hide the More Info tab.
    $(Div_Rep).children(".more_info").fadeOut("fast");

    // Fade content out.
    $("div#content_hldr").fadeOut("fast");

    // Replace the div.
    $(Div_Rep).animate({
        top: New_Top,
        left: New_Left
    }, "slow", function () {

        // Load Home page in.
        $("div#content_hldr").load("content/content.projects.php?id=" + Project_Id, function () {

            // Re-define Cufon.
            Cufon.replace('h2');

        });

        // Fade in the content.
        $("div#content_hldr").fadeIn("fast", function () {

            // Hide the replaced div.
            $(Div_Rep).hide();

            // Replace it back to its position.
            $(Div_Rep).css({
                top: Ori_Top,
                left: Ori_Left
            });

            // Show the More Info tab again.
            $(Div_Rep).children(".more_info").show();

            // Fade back in.
            $(Div_Rep).fadeIn("medium");

        });

    });

});
+3
source share
1 answer

... he will relate the position on the left to the parents.

, . left top position: relative, , , (, ), . , ( ).

, ( offset) left top, , , () [100,50], [-100, -50] ... [0,0].

:

$("selector_for_your_divs").click(function() {
    var pos, $this;
    $this = $(this);
    pos = $this.offset();
    $this.animate({
        left: "-" + pos.left + "px",
        top:  "-" + pos.top  + "px"
    });
});

, , — :

$("selector_for_your_divs").click(function() {
    var mypos, otherpos, $this;

    // Move to the target element
    $this = $(this);
    pos = $this.offset();
    otherpos = $('selector_for_other_element').offset();
    pos.left = otherpos.left - pos.left;
    pos.top  = otherpos.top  - pos.top;
    $this.animate({
        left: pos.left + "px",
        top:  pos.top  + "px"
    });
});

+5

All Articles