添加标记和拖拽弹窗自定义指令

import Vue from "vue";
var index;
var index1;
var index3;
Vue.directive("addMarks", {
  bind(el, binding, vnode, oldVnode) {},
  inserted(el, binding, vnode, oldVnode) {
    console.log(el);
    var wId = "w";
    var index = 0;
    var startX = 0,
      startY = 0;
    var flag = false;
    var retcLeft = "0px",
      retcTop = "0px",
      retcHeight = "0px",
      retcWidth = "0px";
    el.onmousedown = e => {
      console.log(e.button);
      el.style.cursor = "crosshair";
      flag = true;
      try {
        var evt = window.event || e;
        var scrollTop =
          document.body.scrollTop || document.documentElement.scrollTop;
        var scrollLeft =
          document.body.scrollLeft || document.documentElement.scrollLeft;
        startX = evt.clientX + scrollLeft;
        startY = evt.clientY + scrollTop;
        index++;
        var div = document.createElement("div");
        div.id = wId + index;
        div.className = "div";
        div.style.marginLeft = startX + "px";
        div.style.marginTop = startY + "px";
        document.body.appendChild(div);
      } catch (e) {
        //alert(e);
      }
      document.onmousemove = function(e) {
        if (flag) {
          try {
            var evt = window.event || e;
            var scrollTop =
              document.body.scrollTop || document.documentElement.scrollTop;
            var scrollLeft =
              document.body.scrollLeft || document.documentElement.scrollLeft;
            retcLeft =
              (startX - evt.clientX - scrollLeft > 0
                ? evt.clientX + scrollLeft
                : startX) + "px";
            retcTop =
              (startY - evt.clientY - scrollTop > 0
                ? evt.clientY + scrollTop
                : startY) + "px";
            retcHeight = Math.abs(startY - evt.clientY - scrollTop) + "px";
            retcWidth = Math.abs(startX - evt.clientX - scrollLeft) + "px";
            document.getElementById(wId + index).style.marginLeft = retcLeft;
            document.getElementById(wId + index).style.marginTop = retcTop;
            document.getElementById(wId + index).style.width = retcWidth;
            document.getElementById(wId + index).style.height = retcHeight;
          } catch (e) {
            //alert(e);
          }
        }
      };
      document.onmousemove = function(e) {
        if (flag) {
          try {
            var evt = window.event || e;
            var scrollTop =
              document.body.scrollTop || document.documentElement.scrollTop;
            var scrollLeft =
              document.body.scrollLeft || document.documentElement.scrollLeft;
            retcLeft =
              (startX - evt.clientX - scrollLeft > 0
                ? evt.clientX + scrollLeft
                : startX) + "px";
            retcTop =
              (startY - evt.clientY - scrollTop > 0
                ? evt.clientY + scrollTop
                : startY) + "px";
            retcHeight = Math.abs(startY - evt.clientY - scrollTop) + "px";
            retcWidth = Math.abs(startX - evt.clientX - scrollLeft) + "px";
            document.getElementById(wId + index).style.marginLeft = retcLeft;
            document.getElementById(wId + index).style.marginTop = retcTop;
            document.getElementById(wId + index).style.width = retcWidth;
            document.getElementById(wId + index).style.height = retcHeight;
          } catch (e) {
            //alert(e);
          }
        }
      };
      document.ondragstart = e => {
        el.style.cursor = "crosshair";
        console.log(e);
      };
      document.ondrag = e => {
        console.log(e);
      };
    };
  }
});
// v-dialogDrag: 弹窗拖拽属性
Vue.directive("dialogDrag", {
  bind(el, binding, vnode, oldVnode) {
    const dragDom = el.querySelector(".el-dialog");
  },
  inserted(el, binding, vnode, oldVnode) {
    const dialogFooterEl = el.querySelector(".el-dialog__footer");
    const dialogHeaderEl = el.querySelector(".el-dialog__header");
    const dragDom = el.querySelector(".el-dialog");
    // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = (function() {
      if (window.document.currentStyle) {
        return (dom, attr) => dom.currentStyle[attr];
      } else {
        return (dom, attr) => getComputedStyle(dom, false)[attr];
      }
    })();
    let positionX = "";
    dragDom.onmousedown = e => {
      positionX = e.screenX;
      if (e.target.nodeName == "div") {
        dragDom.style.cursor = "move";
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX - dialogHeaderEl.offsetLeft;
        const disY = e.clientY - dialogHeaderEl.offsetTop;
        const screenWidth = document.body.clientWidth; // body当前宽度
        const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)

        const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
        const dragDomheight = dragDom.offsetHeight; // 对话框高度

        const minDragDomLeft = dragDom.offsetLeft;
        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;

        const minDragDomTop = dragDom.offsetTop;
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
        // 获取到的值带px 正则匹配替换
        let styL = sty(dragDom, "left");
        let styT = sty(dragDom, "top");

        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (styL.includes("%")) {
          styL = +document.body.clientWidth * (+styL.replace(/%/g, "") / 100);
          styT = +document.body.clientHeight * (+styT.replace(/%/g, "") / 100);
        } else {
          styL = +styL.replace(/px/g, "");
          styT = +styT.replace(/px/g, "");
        }

        document.onmousemove = function(e) {
          // 通过事件委托,计算移动的距离
          let left = e.clientX - disX;
          let top = e.clientY - disY;

          // 移动当前元素
          dragDom.style.cssText += `;left:${left + styL}px;top:${top +
            styT}px;`;
        };

        document.onmouseup = function(e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      } else {
        dragDom.style.cursor = "default";
        document.onmousemove = null;
        document.onmouseup = null;
      }
    };
    el.onmousedown = function(e) {
      let arr = document.getElementsByClassName("bigView");
      let arr1 =
        document.getElementsByClassName("addForm") ||
        document.getElementsByClassName("originalForm");
      if (arr && arr.length) {
        if (!positionX || positionX != e.screenX) {
          arr[0].style.zIndex = Number(arr[0].style.zIndex) + 100;
        } else {
          // console.log(1111111111)
        }
      }
    };
    //将此时的位置传出去
    // 		binding.value({
    // 			x: e.pageX,
    // 			y: e.pageY
    // 		})
    // 		if (dialogHeaderEl) {
    // 			dialogHeaderEl.style.cursor = 'move';
    // 			dialogHeaderEl.onmousedown = (e) => {
    // 				// 鼠标按下,计算当前元素距离可视区的距离
    // 				const disX = e.clientX - dialogHeaderEl.offsetLeft;
    // 				const disY = e.clientY - dialogHeaderEl.offsetTop;
    // 				const screenWidth = document.body.clientWidth; // body当前宽度
    // 				const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)
    //
    // 				const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
    // 				const dragDomheight = dragDom.offsetHeight; // 对话框高度
    //
    // 				const minDragDomLeft = dragDom.offsetLeft;
    // 				const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
    //
    // 				const minDragDomTop = dragDom.offsetTop;
    // 				const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
    //
    //
    // 				// 获取到的值带px 正则匹配替换
    // 				let styL = sty(dragDom, 'left');
    // 				let styT = sty(dragDom, 'top');
    //
    // 				// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    // 				if (styL.includes('%')) {
    // 					styL = +document.body.clientWidth * (+styL.replace(/%/g, '') / 100);
    // 					styT = +document.body.clientHeight * (+styT.replace(/%/g, '') / 100);
    // 				} else {
    // 					styL = +styL.replace(/px/g, '');
    // 					styT = +styT.replace(/px/g, '');
    // 				};
    //
    // 				document.onmousemove = function(e) {
    // 					// 通过事件委托,计算移动的距离
    // 					let left = e.clientX - disX;
    // 					let top = e.clientY - disY;
    //
    // 					// 移动当前元素
    // 					dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
    // 				};
    //
    // 				document.onmouseup = function(e) {
    // 					document.onmousemove = null;
    // 					document.onmouseup = null;
    // 				}
    // 			};
    // 		}
    //
    // 		if (dialogFooterEl) {
    // 			dialogFooterEl.style.cursor = 'move';
    // 			dialogFooterEl.onmousedown = (e) => {
    // 				// 鼠标按下,计算当前元素距离可视区的距离
    // 				const disX = e.clientX - dialogHeaderEl.offsetLeft;
    // 				const disY = e.clientY - dialogHeaderEl.offsetTop;
    // 				const screenWidth = document.body.clientWidth; // body当前宽度
    // 				const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)
    //
    // 				const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
    // 				const dragDomheight = dragDom.offsetHeight; // 对话框高度
    //
    // 				const minDragDomLeft = dragDom.offsetLeft;
    // 				const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
    //
    // 				const minDragDomTop = dragDom.offsetTop;
    // 				const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
    //
    //
    // 				// 获取到的值带px 正则匹配替换
    // 				let styL = sty(dragDom, 'left');
    // 				let styT = sty(dragDom, 'top');
    //
    // 				// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    // 				if (styL.includes('%')) {
    // 					styL = +document.body.clientWidth * (+styL.replace(/%/g, '') / 100);
    // 					styT = +document.body.clientHeight * (+styT.replace(/%/g, '') / 100);
    // 				} else {
    // 					styL = +styL.replace(/px/g, '');
    // 					styT = +styT.replace(/px/g, '');
    // 				};
    //
    // 				document.onmousemove = function(e) {
    // 					// 通过事件委托,计算移动的距离
    // 					let left = e.clientX - disX;
    // 					let top = e.clientY - disY;
    //
    // 					// 移动当前元素
    // 					dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
    //
    // 					//将此时的位置传出去
    // 					// 					binding.value({
    // 					// 						x: e.pageX,
    // 					// 						y: e.pageY
    // 					// 					})
    // 				};
    //
    // 				document.onmouseup = function(e) {
    // 					document.onmousemove = null;
    // 					document.onmouseup = null;
    // 				}
    // 			};
    // 		}
    // 		dragDom.onmousewheel = function(e) {
    // 			if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件
    // 				if (e.wheelDelta > 0) { //当滑轮向上滚动时
    // 					dragDom.style.transform = dragDom.style.transform + ' scale(0.9)';
    // 				}
    // 				if (e.wheelDelta < 0) { //当滑轮向下滚动时
    // 					dragDom.style.transform = dragDom.style.transform + ' scale(1.1)';
    // 				}
    // 			} else if (e.detail) { //Firefox滑轮事件
    // 				if (e.detail > 0) { //当滑轮向上滚动时
    // 					dragDom.style.transform = dragDom.style.transform + ' scale(0.9)';
    // 				}
    // 				if (e.detail < 0) { //当滑轮向下滚动时
    // 					dragDom.style.transform = dragDom.style.transform + ' scale(1.1)';
    // 				}
    // 			}
    // 		};
  }
});

// v-dialogDrag: 弹窗拖拽属性
Vue.directive("drag", {
  componentUpdated(el, binding, vnode, oldVnode) {
    index3 = el.style.zIndex;
  },
  inserted(el, binding, vnode, oldVnode) {
    let positionX = "";
    const dragDom = el.querySelector(".el-dialog");
    const dialogHeaderEl = el.querySelector(".el-dialog__header");
    let oDiv = el; //当前元素
    // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = (function() {
      if (window.document.currentStyle) {
        return (dom, attr) => dom.currentStyle[attr];
      } else {
        return (dom, attr) => getComputedStyle(dom, false)[attr];
      }
    })();
    dragDom.onmousedown = function(e) {
      positionX = e.screenX;
      console.log(e.target.nodeName);
      if (e.target.nodeName == "DIV") {
        dragDom.style.cursor = "move";
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX - dialogHeaderEl.offsetLeft;
        const disY = e.clientY - dialogHeaderEl.offsetTop;
        const screenWidth = document.body.clientWidth; // body当前宽度
        const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)

        const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
        const dragDomheight = dragDom.offsetHeight; // 对话框高度

        const minDragDomLeft = dragDom.offsetLeft;
        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;

        const minDragDomTop = dragDom.offsetTop;
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
        // 获取到的值带px 正则匹配替换
        let styL = sty(dragDom, "left");
        let styT = sty(dragDom, "top");

        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (styL.includes("%")) {
          styL = +document.body.clientWidth * (+styL.replace(/%/g, "") / 100);
          styT = +document.body.clientHeight * (+styT.replace(/%/g, "") / 100);
        } else {
          styL = +styL.replace(/px/g, "");
          styT = +styT.replace(/px/g, "");
        }

        document.onmousemove = function(e) {
          // 通过事件委托,计算移动的距离
          let left = e.clientX - disX;
          let top = e.clientY - disY;

          // 移动当前元素
          dragDom.style.cssText += `;left:${left + styL}px;top:${top +
            styT}px;`;
        };

        document.onmouseup = function(e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
        // dragDom.onmousewheel = function(e) {
        //   if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件
        //     if (e.wheelDelta > 0) { //当滑轮向上滚动时
        //       dragDom.style.transform = dragDom.style.transform + ' scale(0.9)';
        //     }
        //     if (e.wheelDelta < 0) { //当滑轮向下滚动时
        //       dragDom.style.transform = dragDom.style.transform + ' scale(1.1)';
        //     }
        //   } else if (e.detail) { //Firefox滑轮事件
        //     if (e.detail > 0) { //当滑轮向上滚动时
        //       dragDom.style.transform = dragDom.style.transform + ' scale(0.9)';
        //     }
        //     if (e.detail < 0) { //当滑轮向下滚动时
        //       dragDom.style.transform = dragDom.style.transform + ' scale(1.1)';
        //     }
        //   }
        // };
      } else {
        dragDom.style.cursor = "default";
        document.onmousemove = null;
        document.onmouseup = null;
      }
    };
    el.onmousedown = function(e) {
      let arr = document.getElementsByClassName("bigView");
      let arr1 =
        document.getElementsByClassName("addForm") ||
        document.getElementsByClassName("originalForm");
      if (!positionX || positionX != e.screenX) {
        arr[0].style.zIndex = Number(arr[0].style.zIndex) - 100;
      }
    };
  }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值