本文主要是介绍React 实现 iphone 上的小圆点的拖拽功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在移动端或者PC端我们来模拟拖拽功能的实现, 通过模拟iphone上的Touch-Sensitive Home Button,此处简单实现,仅作开发参考。
效果演示
主要代码演示
/* 拖动功能 */// 初始化宽高
let oW,oH;
let oLeft = 0;
let oTop = 0;
let htmlWidth = document.documentElement.clientWidth;
let htmlHeight = document.documentElement.clientHeight;
let bWidth = 0;
let bHeight = 0;
let block = null;export default {// 初始化init(_block) {// 更新dom元素及其宽度和高度block = _block;bWidth = block.offsetWidth;bHeight = block.offsetHeight;// 监听this.listen(block);},// 拖动功能documentMove(e) {// 区分移动端和PC端(e.touches) && (e = e.touches[0]);oLeft = e.clientX - oW;oTop = e.clientY - oH;// 控制可拖动范围if(oLeft < 0) {oLeft = 0;} else if (oLeft > htmlWidth - bWidth) {oLeft = (htmlWidth - bWidth);}if(oTop < 0) {oTop = 0;} else if (oTop > htmlHeight - bHeight) {oTop = (htmlHeight - bHeight);}block.style.left = oLeft + "px";block.style.top = oTop + "px";},// 监听功能listen(block) {// 支持 移动端block.addEventListener("touchstart", (e)=>{e = e.touches[0];oW = e.clientX - block.offsetLeft;oH = e.clientY - block.offsetTop;// 监听document touchmovedocument.addEventListener("touchmove", this.documentMove ,false);// 监听document touchenddocument.addEventListener("touchend", (e) => {document.removeEventListener("touchmove", this.documentMove, false);},false);},false);// 支持PC端block.addEventListener("mousedown", (e)=>{oW = e.clientX - block.offsetLeft;oH = e.clientY - block.offsetTop;// 监听document mousemovedocument.addEventListener("mousemove", this.documentMove ,false);// 监听document mouseupdocument.addEventListener("mouseup", (e)=>{document.removeEventListener("mousemove", this.documentMove, false);},false);// 监听document mouseleavedocument.addEventListener("mouseleave", ()=>{document.removeEventListener("mousemove", this.documentMove, false);}, false);},false);}
}
支持平台
- 移动端
- PC端
Github地址
- https://github.com/johnnynode/iphone-drag-button
这篇关于React 实现 iphone 上的小圆点的拖拽功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!