首页 > 其他分享 >JS函数封装实现控件拖拽

JS函数封装实现控件拖拽

时间:2022-08-24 17:47:33浏览次数:133  
标签:function 控件 false dragable 50% JS let wrap 拖拽

js脚本

export function dragBox(drag, wrap) {

  //  用于获取父容器的样式属性值
  function getCss(ele, prop) {
    // getComputedStyle返回值是带单位的字符串,所以得parseInt
    return parseInt(window.getComputedStyle(ele)[prop]);
  }

  // 鼠标点击位置的屏幕x,y坐标
  let initX;
  let initY;

  // 父容器的屏幕x,y坐标
  let wrapLeft;
  let wrapRight;

  // 用于判断鼠标按住与松开
  let dragable = false;
  // 给子容器绑定mousedown事件
  drag.addEventListener(
    "mousedown",
    function (e) {
      dragable = true;
      wrapLeft = getCss(wrap, "left");
      wrapRight = getCss(wrap, "top");
      initX = e.clientX;
      initY = e.clientY;
    },
    false
  );

  // 给子容器绑定mouseup事件
  drag.addEventListener(
    "mouseup",
    function (e) {
      dragable = false;
    },
    false
  );

  // 页面绑定mousedown事件
  document.addEventListener("mousemove", function (e) {
    if (dragable === true) {
      let nowX = e.clientX;
      let nowY = e.clientY;
      let disX = nowX - initX;
      let disY = nowY - initY;
      wrap.style.left = wrapLeft + disX + "px";
      wrap.style.top = wrapRight + disY + "px";
    }
  });
}

vue页面使用

<template>
<div id="father">
    <div id="son"/>
    ......
</div>
</template>
<script>
import { dragBox } from '@/utils/controls'
export default {
    .....

    mounted(){
        this.$nextTick(()=>{
            dragBox(document.querySelector('.son'),document.querySelector('#father'))
        })
    },

    ......
}
</script>
<style>
#father{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 15px;

    #son{
        cursor: move;
    }

    ......
}
</style>

注意这里的函数会形成闭包,变量会一直保存在内存

效果展示

标签:function,控件,false,dragable,50%,JS,let,wrap,拖拽
From: https://www.cnblogs.com/echohye/p/16620990.html

相关文章

  • phantomjs实现截图
    准备阶段下载phantomjsPhantomJSAPI参考EChartsConvert浏览器查看base64编码图片方法echarts官网java后端使用freemarker生成echarts图表word流程简要说明1.下载p......
  • WinForm代码添加控件
    1.新建Winform程序1.1拖一个FlowLayoutPanel控件属性:Dcok=Fill1.2拖一个Panel控件属性:Margin=10,10,10,10Visible=False1.3拖一个Label控件属性......
  • mfc调用ocx控件
    项目中,有时候三方公司提供ocx控件调用,下面介绍如何一步步调用。1)在任意目录存放三方ocx并进行注册和反注册测试,先保证三方ocx能注册成功reg.bat:regsvr32/uC:\xxx_......
  • JS控制浏览器打印样式,分页及移除页眉页尾
    很简单,引入以下CSS即可:<style>/*分页符*/.print_gap{page-break-before:always;}.print_gap.gap_line{height:1px;background:#e5e5e5;margin:40pt030pt;......
  • mysql php js 经纬度 转换 查询
    坐标系介绍地球坐标(WGS84)WGS-84:是国际标准,GPS坐标(GoogleEarth使用、或者GPS模块)国际标准,从专业GPS设备中取出的数据的坐标系国际地图提供商使用的坐标系火星坐......
  • jsp
    注意要写成out.println,去掉system   ......
  • curl 返回json 并格式化
    应用curlhttp://url/path|python-mjson.tool例子curl'http://localhost:9090/api/v1/targets?state=active'|python-mjson.tool%Total%Received%X......
  • JS doc 接口文档生成器
    前言项目中使用到需要把js方法生成接口文档,使用到了JSdoc这个工具,使用该工具生成文档,需要在方法里加入注释,根据注释说明生成文档,这里顺便记录一下使用过程,模拟了一些j......
  • mysql增删改查json中的某个字段
    创建表1CREATETABLEt_json(idINTPRIMARYKEY,NAMEVARCHAR(20),infoJSON);插入记录1INSERTINTOt_json(id,sname,info)VALUES(1,'test','{"time":"20......
  • Github+jsDelivr+PicGo搭建图床
    1.GitHub创建仓库,申请TokenSetting→DeveloperSettings→PersonalaccessTokens→PersonalaccessTokens→Generatenewtoken创建如图所示,输入名字和勾选r......