简体   繁体   中英

How can I check in which direction I can move an element?

I want to add a small function to move an element up/down/left/right.

How I can check in which direction an element (div span etc) can move at all?

For example, I have this code:

<div id="stop_here_container">
<div></div>
<div>
  <div><div>
  <div></div>
  <div></div>
</div>
<div></div>
</div>

Now I must check in which direction every element could be moved (elements could be unlimited nested!). For example, to know this:

<div id="stop_here_container">
<div>I can move down</div>
<div>
  <div>I can move up/down/right</div>
  <div>I can move left/right/up/down</div>
  <div>I can move up/down/left</div>
</div>
<div>I can move up</div>
</div>

After that, I can add a data.attr or a class to every element, and go on with his.

Thanks for your help and suggestions

You can use data attribute to store your state and using class you can easily find that state like below.

 $(".move").mousedown(function() { console.log($(this).data('myval')); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='move' data-myval='down'>i can move down</div> <div> <div class='move' data-myval='up/down/right'> i can move up/down/right</div> <div class='move' data-myval='left/right/up/down'> i can move left/right/up/down</div> <div class='move' data-myval='up/down/left'> i can move up/down/left</div> </div> <div class='move' data-myval='up'>i can move up</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM