html 5 拖放在移动屏幕上不起作用
html 5 drag and drop not working on mobile screen
我正在创建一个需要拖放组件的场景,
经过一些变通,我找到了一个,如下所示
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
let target = ev.target;
let btnsHolder = document.getElementById("buttonsHolder")
let currentBtn;
var data = ev.dataTransfer.getData("text");
//check if there's already a button inside
if (target.children.length > 0){
currentBtn = target.children[0];
btnsHolder.appendChild(currentBtn);
}
target.appendChild(document.getElementById(data));
}
#div1 {
width: 350px;
height: 70px;
padding: 10px;
border: 1px solid #aaaaaa;
}
<p>Drag the W3Schools image into the rectangle:</p>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<div id="buttonsHolder">
<button id="test" draggable="true" ondragstart="drag(event)" width="336" height="69">button 1</button>
<button id="test2" draggable="true" ondragstart="drag(event)" width="336" height="69">button 2</button>
<button id="test3" draggable="true" ondragstart="drag(event)" width="336" height="69">button 3</button>
</div>
但是相同代码的问题是,
它在 pc 上工作,但在移动屏幕上不工作,
用于测试,
按下
后
F12
点击切换设备工具栏将启用移动视图,
同样的代码不起作用,
这可能是什么问题?
一些移动设备不监听 drag
事件。要解决这个问题,您必须使用触摸事件 touchstart
、touchend
、touchcancel
、touchleave
、touchmove
TouchEvent - Web APIs
// get The element on which to attach the event
var btn = document.querySelector('.btn');
// attaching each event listener
btn.addEventListener('touchstart', function(){
console.log('btn touched');
})
btn.addEventListener('touchend', function(){
console.log('btn leaved');
})
btn.addEventListener('touchmove', function(){
console.log('btn leaved');
})
btn.addEventListener('touchleave', function(){
console.log('btn moving end');
})
btn.addEventListener('touchcancel', function(){
console.log('btn moving cancel');
})
<button class="btn">test</button>
这可能是由于滚动操作覆盖。将 touch-action: none;
CSS 样式添加到您的 .draggable
class
我正在创建一个需要拖放组件的场景,
经过一些变通,我找到了一个,如下所示
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
let target = ev.target;
let btnsHolder = document.getElementById("buttonsHolder")
let currentBtn;
var data = ev.dataTransfer.getData("text");
//check if there's already a button inside
if (target.children.length > 0){
currentBtn = target.children[0];
btnsHolder.appendChild(currentBtn);
}
target.appendChild(document.getElementById(data));
}
#div1 {
width: 350px;
height: 70px;
padding: 10px;
border: 1px solid #aaaaaa;
}
<p>Drag the W3Schools image into the rectangle:</p>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<div id="buttonsHolder">
<button id="test" draggable="true" ondragstart="drag(event)" width="336" height="69">button 1</button>
<button id="test2" draggable="true" ondragstart="drag(event)" width="336" height="69">button 2</button>
<button id="test3" draggable="true" ondragstart="drag(event)" width="336" height="69">button 3</button>
</div>
但是相同代码的问题是,
它在 pc 上工作,但在移动屏幕上不工作,
用于测试,
按下
后F12
点击切换设备工具栏将启用移动视图,
同样的代码不起作用,
这可能是什么问题?
一些移动设备不监听 drag
事件。要解决这个问题,您必须使用触摸事件 touchstart
、touchend
、touchcancel
、touchleave
、touchmove
TouchEvent - Web APIs
// get The element on which to attach the event
var btn = document.querySelector('.btn');
// attaching each event listener
btn.addEventListener('touchstart', function(){
console.log('btn touched');
})
btn.addEventListener('touchend', function(){
console.log('btn leaved');
})
btn.addEventListener('touchmove', function(){
console.log('btn leaved');
})
btn.addEventListener('touchleave', function(){
console.log('btn moving end');
})
btn.addEventListener('touchcancel', function(){
console.log('btn moving cancel');
})
<button class="btn">test</button>
这可能是由于滚动操作覆盖。将 touch-action: none;
CSS 样式添加到您的 .draggable
class