[英]HTML5 Drag and drop. Detecting where element was grabbed
您可以使用計算出的元素的padding + width和MouseEvent的offsetX
屬性來計算所選區域。
yourElement.addEventListener('mousedown', function onDragStart(event){
let width = parseInt(window.getComputedStyle(yourElement).getPropertyValue('width'));
let padding = parseInt(window.getComputedStyle(yourElement).getPropertyValue('padding-left'));
let position = event.offsetX;
let middle = (width / 2) + padding;
if (position <= middle) {
console.log('left');
} else {
console.log('right');
}
});
有關示例,請參見此jsfiddle: https ://jsfiddle.net/c23Lu6gy/28/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.