简体   繁体   中英

how to detect drag left to right with movement constrain?

I want to detect direction of drag event on the static(no movement) element. From what I have searched, they use jquery-ui draggable that the element will move.

here is my try ( I use containment to stop its movement but then I cannot detect direction of drag)

 var prevX = -1; $('div').draggable({ containment: "parent", drag: function(e) { //console.log(e.pageX); if (prevX == -1) { prevX = e.pageX; return false; } // dragged left if (prevX > e.pageX) { alert('dragged left'); } else if (prevX < e.pageX) { // dragged right alert('dragged right'); } prevX = e.pageX; } }); 
 .wrapper { width: 100px; height: 100px; } .inside { width: 100px; height: 100px; background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <div class="wrapper"> <div class="inside"></div> </div> 

使用dragstart保存开始位置XY,使用end dragend比较

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