最近做一個功能,一個點擊按鈕,需要支持拖拽。完成后謝在這里做一下筆記。
export default class Record extends Component {
constructor(props) {
super(props);
// 限制最大寬高翰苫,不讓滑塊出去
this.dragLimit = {
maxW: null,
maxH: null,
};
this.timer = null;
}
// 認購記錄拖拽
recordDrag = (e) => {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.timer = setTimeout(() => {
// 執(zhí)行點擊事件
window.location.hash = `#${Routers.Record}`;
}, 500);
const ev = e || window.event;
const touch = ev.targetTouches[0];
const bragBox = document.querySelector('#apply-record');
const containerBox = document.querySelector('#cal-wrap');
if (!bragBox || !containerBox) {
return;
}
if (this.dragLimit.maxW === null || this.dragLimit.maxH === null) {
this.dragLimit = {
maxW: containerBox.clientWidth - bragBox.offsetWidth,
maxH: containerBox.clientHeight - bragBox.offsetHeight,
};
}
// 手指觸摸開始,記錄盒子的初始位置
const oL = touch.clientX - bragBox.offsetLeft;
const oT = touch.clientY - bragBox.offsetTop;
// 觸摸中的这橙,位置記錄
bragBox.addEventListener('touchmove', (e1) => {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
const ev1 = e1 || window.event;
const touch1 = ev1.targetTouches[0];
let oLeft = touch1.clientX - oL;
let oTop = touch1.clientY - oT;
if (oLeft < 0) {
oLeft = 0;
} else if (oLeft >= this.dragLimit.maxW) {
oLeft = this.dragLimit.maxW;
}
if (oTop < 0) {
oTop = 0;
} else if (oTop >= this.dragLimit.maxH) {
oTop = this.dragLimit.maxH;
}
bragBox.style.left = `${oLeft}px`;
bragBox.style.top = `${oTop}px`;
});
// 觸摸結(jié)束時的處理
bragBox.addEventListener('touchend', () => {
document.removeEventListener('touchmove', (evt) => {
// 阻止默認事件
evt.preventDefault();
});
});
}
render() {
return (
<div styleName="wrap" id="cal-wrap">
...
<div id="apply-record" styleName="apply-record" onTouchStart={this.recordDrag} >
{/* <span>認購記錄</span> */}
</div>
...
</div>
);
}
}