Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
How I can move div to empty space ONLY when they are visually neighbors in flex display using js or jquery?

I`m working on a simple number ordering game with Js
I used display:flex to place all inner DIVs inside the container DIV
And I want when I click on specific DIV it moves to the empty space ONLY when that DIV and the empty space are visually neighbors
I mean they share any border either above or beneath or left or right
as shown in this Image:
Image

In this case (in the image above) I want when someone click the div number 6 it moves down to the empty space and left empty space in its original place where I can move further divs if they are visually neighbors
as this image :
second image after 1 move
I try this

$('.tile').click(function (event) {
$(this).animate({top:'100'});
});

but that code moves the div only DOWN

I want to get some code that I can handle all directions after each move

How can I achieve this with js or jquery?

my full code

$(document).ready(function(){

$('.tile').click(function (event) {

$(this).animate({top:'100'});

//var div = $(event.target).closest('.tile');
//div.next('.tile').after(div).animate({top:'100'});

});

});



.flexDiv{
display:flex;
flex-wrap: wrap;
width:310px;
border:1px solid black;
}

.tile{
position:relative;
background:#08088A;
text-align:center;
width:29%;
margin:1px;
font-size:75px;
font-weight:bold;
color:#fff;
padding:5px;
}
.white{
background:#fff;
}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div class="flexDiv">
<div class="tile">1</div>
<div class="tile">2</div>
<div class="tile">3</div>
<div class="tile">4</div>

<div class="tile">5</div>
<div class="tile">6</div>
<div class="tile">7</div>
<div class="tile">8</div>
</div>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!