I have this html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrap {
            position: relative;
            overflow: auto;
            height: 600px;
            width: 600px;
            padding: 10px;
        }

        .parent {
            width: 200px;
            height: 200px;
            background-color: chartreuse;
            position: absolute;
            top: 177px;
            left: 177px;
        }

        .child {
            width: 50px;
            height: 50px;
            background-color: darkorchid;
            position: absolute;
            top: 70px;
            left: 70px;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>

<body>
    <div id="wrap">
        <div class="parent">
            <div class="child"></div>
        </div>
    </div>
    <button id="moveChild">Move Child</button>



</body>

</html> 

How can I select the child, remove it from the parent element and place it inside the wrap element, while keeping it’s visual position the same? Note that scrollbars might be present.

Solution:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrap {
            position: relative;
            overflow: auto;
            height: 600px;
            width: 600px;
            padding: 10px;
        }

        .parent {
            width: 200px;
            height: 200px;
            background-color: chartreuse;
            position: absolute;
            top: 177px;
            left: 177px;
        }

        .child {
            width: 50px;
            height: 50px;
            background-color: darkorchid;
            position: absolute;
            top: 70px;
            left: 70px;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>

<body>
    <div id="wrap">
        <div class="parent">
            <div class="child"></div>
        </div>
    </div>
    <button id="moveChild">Move Child</button>

   <script>

      document.getElementById('moveChild').addEventListener('click', function () {
            // Select elements
            var childElement = document.querySelector('.child');
            var wrapElement = document.querySelector('#wrap');

            // Get the child's current position relative to the viewport
            var rectBefore = childElement.getBoundingClientRect();

            // Append the child to the wrap
            wrapElement.appendChild(childElement);

            // Get the child's new position relative to the viewport
            var rectAfter = childElement.getBoundingClientRect();

            // Calculate the change in the child's position
            var changeInTop = rectAfter.top - rectBefore.top;
            var changeInLeft = rectAfter.left - rectBefore.left;

            // Adjust the child's position to cancel out the change
            childElement.style.position = 'absolute';
            childElement.style.top = (childElement.offsetTop - changeInTop) + 'px';
            childElement.style.left = (childElement.offsetLeft - changeInLeft) + 'px';
        });

</script>



</body>

</html>

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *