简体   繁体   中英

Disable mobile browser swipe to go back behavior?

I'm building a feature that let's users drag different items around. However, when they drag an item that's near the edge of the screen of their phone, the browser goes back to the last page. Does anyone know if it's able to disable this in a single <div> or is it impossible?

You could try the below code to prevent this.

If the touch element is the targeted div then prevent the action otherwise allow.

HTML:

<div id="tragetDiv"> </div>
 window.addEventListener("touchmove", function(event) {
       let target = event.target;
       if (target.id === "tragetDiv") {
           event.preventDefault();
       }
 });

you can set the draggable item's position inside the div coordinates. If it goes beyond target div simply set the bounds value to that.

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