首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使其在用户尝试调整图像大小时图像不会移动

使其在用户尝试调整图像大小时图像不会移动
EN

Stack Overflow用户
提问于 2020-09-01 23:40:41
回答 1查看 61关注 0票数 1

正如标题所述,我希望用户能够调整图像的大小,而不需要用户拖动图像。

我有它,所以用户可以拖动图像,使他们可以相互重叠,但我也想添加的功能,用户可以调整任何图像的大小。我正在通过CSS/JS来做这件事。

我关于如何拖动的资源来自这里:https://www.w3schools.com/howto/howto_js_draggable.asp

我关于如何调整图像大小的源码来自这里:https://jqueryui.com/resizable/

目前,当用户尝试调整img的大小时,img也会被拖来拖去。

下面是我的问题示例:https://jsfiddle.net/3ybsd6rk/1/

代码语言:javascript
运行
复制
var drag = true;

// makes any image resizable on the page
$(function() {
  $("img").resizable();
});

//  Make the DIV element draggable
// Simple loop that goes through all element ids that start with 'dragId'
// (This needs to be the naming convention or this won't work)
var dragPrefix = 'dragId';
var el;
for (i = 0; el = document.getElementById(dragPrefix + i); i++) {
  dragElement(el);
}

function dragElement(elmnt) {
  var pos1 = 0,
    pos2 = 0,
    pos3 = 0,
    pos4 = 0;
  if (document.getElementById(elmnt.id)) {
    // if present, the header is where you move the DIV from:
    document.getElementById(elmnt.id).onmousedown = dragMouseDown;
  } else {
    // otherwise, move the DIV from anywhere inside the DIV:
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
    getToTop();
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    elmnt.style.opacity = "0.5"
    getToTop();
  }

  function closeDragElement() {
    // stop moving when mouse button is released:
    document.onmouseup = null;
    document.onmousemove = null;
    elmnt.style.opacity = "1.0";
    getToTop();
  }

  // Simple function that makes all other drag elements 1 and the one in 'focus' 5 which will make it
  // hover over the other elements
  function getToTop() {
    for (i = 0; el = document.getElementById(dragPrefix + i); i++) {
      el.style.zIndex = "1";
    }
    elmnt.style.zIndex = "5"
  }
}
代码语言:javascript
运行
复制
.drag_img {
  position: absolute;
  display: inline-block;
}
#mydiv {
  position: absolute;
  z-index: 9;
  background-color: #f1f1f1;
  border: 1px solid #d3d3d3;
  text-align: center;
}

#mydivheader {
  padding: 10px;
  cursor: move;
  z-index: 10;
  background-color: #2196F3;
  color: #fff;
}
代码语言:javascript
运行
复制
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-02 03:15:54

现在,调整大小和拖动都是同时执行的,导致了冲突。我认为你在旗帜上的方向是正确的,但是调整大小是有一个阈值的,所以与拖动相比,事件的开始和停止将会延迟,因此拖动仍然会优先发生。最好的选择是绑定到大小调整手柄,这样当用户操作大小调整程序时,拖动将被禁用。

每当用户的鼠标在处理程序上按下时创建一个标志:

代码语言:javascript
运行
复制
$("#mydivheader").resizable({
  create: function(e, ui) {
    $('.ui-resizable-handle')
        .on('mousedown', function() {
        resizing = true;
      })
        .on('mouseup', function() {
        resizing = false;
      });
      
  }
});

https://jsfiddle.net/jo9m2phb/1/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63691014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档