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
- functionisTouchDevice(){
- return‘ontouchstart’in window;
- }
- functionfitTouchDevice() {
- if (!isTouchDevice()) {
- return;
- li>
- function simMouseEvt(type, target, pos) {
- varevt= document. createEvent(‘MouseEvents’);
- evt.initMouseEvent(type, true, true, document,
- 0, pos.screenX, pos.screenY, pos.clientX, pos.clientY ,
- false, false , false, false,
- 0 , Target);
- return target.dispatchEvent(evt);
- document. addEventListener(“touchstart”, function(e){
- if (!simMouseEvt(“mousedown”, e.target, e.changedTouches[0])) {
- e.preventDefault();
- setTimeout(function(){ /span>
- span class=”keyword” style=”color:rgb(127,0,85); font-weight:bold”>if (!e.target.getAttribute(“movement”)) {
- E.target.removeAttribute(“movement”);
- simMouseEvt(“click”, e.target, e.changedTouches[ 0])
- , 100);
- ));
- document.addEventListener(“touchmove”,function(e){
- e.target.setAttribute(“movement”,“move”);
- simMouseEvt(“mousemove”, e.target, e .changedTouches[0])|| e.preventDefault();
- });
- document.addEventListener(“touchend”, function( e) {
- e.target.removeAttribute (“movement”);
- simMouseEvt(“mouseup”, e.target, e.changedTouches[0])||e .preventDefault();
- li>
- }
< 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">
currently only tested on ipad and android phones , The idea is workable.
- function isTouchDevice(){
- return ‘ontouchstart’ in window;
- }
- /span>
- function fitTouchDevice(){
- if(!isTouchDevice()) {
- return;
- }
- function simMouseEvt(type, target, pos){
- var evt = document.createEvent(‘MouseEvents’);
- evt.initMouseEvent(type, true, true, document,
- 0, pos.screenX, pos.screenY, pos. clientX, pos.clientY,
- false,false, false, false,
- 0, target);
- return span> target.dispatchEvent(evt);
- }
- document.addEventListener(“touchstart”, function(e){
- if(!simMouseEvt (“mousedown”, e.target, e.changedTouches[0])) {
- e.preventDefault();
- setTimeout(function(){
- if(!e.target.getAttribute(“movement”)) {
- e.target.removeAttribute(“movement”);
- simMouseEvt(“click” span>, e.target, e.changedTouches[0])
- }
- 100);
- });
- document.addEventListener(“touchmove”, function(e){
- e.target.setAttribute(“movement”, “move”);
- simMouseEvt(“mousemove” span>, e.target, e.changedTouches[0]) || e.preventDefault();
- ));
- document.addEventListener(“touchend”, function(e){
- e.target.removeAttribute(“movement”);
- simMouseEvt(“mouseup”, e.target, e. changedTouches[0])|| e.preventDefault();
- ));
- }
Js code