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.