From the mouse to your fingers – traditional drag and drop in the iPad

I have done some UI components before, supporting some drag and drop. I recently got an ipad, and the old components can’t be dragged away or moved on these handheld devices. Later, I thought of a way to simulate the operation of the mouse with the touch event, which is to use the touch response event to trigger the mouse event. Not much to say, upload the code, please take a picture

Js code Collection code

  1. functionisTouchDevice(){
  2. return‘ontouchstart’in window;
  3. }
  4. < li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220) ; background-color:rgb(250,250,250); line-height:18px">

  5. functionfitTouchDevice() {
  6. if (!isTouchDevice()) {
  7. return;
  8. li>
  9. function simMouseEvt(type, target, pos) {
  10. varevt= document. createEvent(‘MouseEvents’);
  11. evt.initMouseEvent(type, true, true, document,
  12. 0, pos.screenX, pos.screenY, pos.clientX, pos.clientY ,
  13. false, false , false, false,
  14. 0 , Target);
  15. return target.dispatchEvent(evt);
  16. document. addEventListener(“touchstart”, function(e){
  17. if (!simMouseEvt(“mousedown”, e.target, e.changedTouches[0])) {
  18. e.preventDefault();
  19. setTimeout(function(){ /span>
  20. span class=”keyword” style=”color:rgb(127,0,85); font-weight:bold”>if (!e.target.getAttribute(“movement”)) {
  21. E.target.removeAttribute(“movement”);
  22. simMouseEvt(“click”, e.target, e.changedTouches[ 0])
  23. , 100);
  24. ));
  25. document.addEventListener(“touchmove”,function(e){
  26. e.target.setAttribute(“movement”,“move”);
  27. simMouseEvt(“mousemove”, e.target, e .changedTouches[0])|| e.preventDefault();
  28. });
  29. document.addEventListener(“touchend”, function( e) {
  30. e.target.removeAttribute (“movement”);
  31. simMouseEvt(“mouseup”, e.target, e.changedTouches[0])||e .preventDefault();
  32. li>
  33. }

currently only tested on ipad and android phones , The idea is workable.

Js code collection code

  1. function isTouchDevice(){
  2. return ‘ontouchstart’ in window;
  3. }
  4. /span>
  5. function fitTouchDevice(){
  6. if(!isTouchDevice()) {
  7. return;
  8. }
  9. function simMouseEvt(type, target, pos){
  10. var evt = document.createEvent(‘MouseEvents’);
  11. evt.initMouseEvent(type, true, true, document,
  12. 0, pos.screenX, pos.screenY, pos. clientX, pos.clientY,
  13. false,false, false, false,
  14. 0, target);
  15. return target.dispatchEvent(evt);
  16. }
  17. document.addEventListener(“touchstart”, function(e){
  18. if(!simMouseEvt (“mousedown”, e.target, e.changedTouches[0])) {
  19. e.preventDefault();
  20. setTimeout(function(){
  21. if(!e.target.getAttribute(“movement”)) {
  22. e.target.removeAttribute(“movement”);
  23. simMouseEvt(“click”, e.target, e.changedTouches[0])
  24. }
  25. 100);
  26. });
  27. document.addEventListener(“touchmove”, function(e){
  28. e.target.setAttribute(“movement”, “move”);
  29. simMouseEvt(“mousemove” span>, e.target, e.changedTouches[0]) || e.preventDefault();
  30. ));
  31. document.addEventListener(“touchend”, function(e){
  32. e.target.removeAttribute(“movement”);
  33. simMouseEvt(“mouseup”, e.target, e. changedTouches[0])|| e.preventDefault();
  34. ));
  35. }

Js code Collection code

Js code Collection Code

Leave a Comment

Your email address will not be published.