I have a grid of 6 blocks. When you click on each of the blocks, the block expands and closes the container containing the blocks.
The first square (top left) looks great, but others cannot imagine the block growing to the width and height of the container because it starts from the top left position.
Ideally, drawers 2 and 5 should expand from their center, and windows 1, 3, 4, and 6 should expand from their far corner. Is it possible and how?
I created a JSFiddle that shows my problem. But the code to play here:
JQuery
$(".service-block").on("click", "a", function (e) { e.preventDefault(); var block = $(this); var blockOffset = block.offset(); var blockBackgroundColor = block.css("backgroundColor"); var container = $(".service-grid"); var containerOffset = container.offset(); // Create the popup and append it to the container var popout = $("<div class='service-block-popup'>Test</div>"); popout.css({ "backgroundColor": blockBackgroundColor, "position": "absolute", "top": blockOffset.top, "left": blockOffset.left }).appendTo(container) // Now animate the properties .animate({ "height": container.height() + "px", "width": container.width() + "px", "top": containerOffset.top, "left": containerOffset.left }, 1500, function() { //alert("done"); }) .on("click", function () { popout.remove(); }); });
Markup
<div class="service-grid"> <div class="row"> <div class="service-block"> <a href="#" class="box1"> <span class="title">Box 1</span> </a> </div> <div class="service-block"> <a href="#" class="box2"> <span class="title">Box 2</span> </a> </div> <div class="service-block"> <a href="#" class="box3"> <span class="title">Box 3</span> </a> </div> </div> <div class="row"> <div class="service-block"> <a href="#" class="box4"> <span class="title">Box 4</span> </a> </div> <div class="service-block"> <a href="#" class="box5"> <span class="title">Box 5</span> </a> </div> <div class="service-block"> <a href="#" class="box6"> <span class="title">Box 6</span> </a> </div> </div> </div>
CSS
*, *::after, *::before { box-sizing: border-box; } .service-grid { width: 600px; } .row { overflow: hidden; } .service-grid .service-block a { display: block; height: 200px; width: 200px; text-align: center; float: left; } .service-grid .service-block a > img { display: block; margin: 0 auto; transition: all 100ms linear 0s; } .service-grid .service-block a > .title { display: block; font-family: Arial,Helvetica,sans-serif; font-size: 2.2rem; font-weight: bold; line-height: 3.2rem; margin-top: 20px; text-transform: uppercase; } .box1 { background: red; } .box2 { background: purple; } .box3 { background: yellow; } .box4 { background: orange; } .box5 { background: green; } .box6 { background: magenta; }