【问题标题】:E.ClientY in Draggable HTML Elements可拖动 HTML 元素中的 E.ClientY
【发布时间】:2018-08-05 17:08:25
【问题描述】:

我一直在从事一个项目,但遇到了一个问题。 我正在尝试制作多个在拖动时沿 y 轴移动的上下滑块。 我从一个可拖动元素 (https://www.w3schools.com/howto/howto_js_draggable.asp) 的 W3Schools 代码基础开始,然后从那里构建它。 但是,e.clientY 的某些东西不起作用。在测试时,我发现它没有改变它的值。 如果有人有任何想法,他们将不胜感激。谢谢! 附:对不起所有的测试代码,我还没有清理它。一些 cmets 和函数与程序并不真正相关。

HTML: 一个

</div>
</div>
<button id="Button">
Click Here
</button>
<div id="Error">
TEXT
</div>

JS:

    var b = document.getElementById("Button");
var area = document.getElementById("AddGraphsHere");
var etwo;
var count = 0;
var error = document.getElementById("Error");
var point = 1;
var interval;
var elmnt;
var canExecute;

b.onclick = function addDiv() {
area.innerHTML += "<div id='Graph'><div onmouseover='dragElement(this, event)' id='Point'></div></div>";
point = document.getElementById('Point');
}

function falert() {alert("Hello!"); this.onmousedown = dragMouseDown;}

//Start with a dragElement(Graph);

function dragElement(elmnt, e) {
  var pos1 = 0, pos2 = 0;
    /* if present, the header is where you move the DIV from:*/
    //error.innerHTML = elmnt;
    document.addEventListener("mousedown", function () {dragMouseDown();});

  function dragMouseDown(e) {
    //error.innerHTML = "dragMouseDown Activated!";
    e = e || window.event;
    //alert(e);
    // get the mouse cursor position at startup:
    pos2 = e.clientY;
    canExecute = true;
        error.innerHTML = "a";
    // call a function whenever the cursor moves:
    document.addEventListener("mousemove", function () {elementDrag(e);});
    //document.onmouseup = closeDragElement();
  }

  function elementDrag(e) {
    if (canExecute === true) {
      e = e || window.event;

            count = count + 1;
      // calculate the new cursor position:
      pos2 = e.clientY;
      pos1 = pos2 - e.clientY;
      pos2 = e.clientY;
      // set the element's new position:
      elmnt.style.position = "absolute";

      elmnt.style.top = (elmnt.offsetTop - pos1) + "px";
      //elmnt.top = (elmnt.offsetTop - 
      //pos1) + "px";
      error.innerHTML = pos1 + ", " + count;
      console.log(count + ", " + pos2);
      //error.innerHTML = elmnt.offsetTop + " - " + pos1;

      document.addEventListener("mouseup", function() {closeDragElement();});
      }
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
    error.innerHTML = "Cleared!";
    canExecute = false;
  }
  }

CSS:

#Border {
  border: #000000 5px solid;
  width: 90%;
  height: 200px;
}
#Graph {
  width: 18%;
  height: 190px;
  float: left;
  border: 1px solid black;
  padding: 5px;
  font-family: sans-serif;
  font-size: 12px;
}
#Point {
  width: 8px;
  height: 8px;
  border-radius: 100%;
  border: 3px solid #0000FF;
  background-color: #AAAAFF;
  top: 100px;

  position: absolute;
  cursor: move;
}

(抱歉没有把它作为代码sn-p,但是我测试过它并没有像以前那样工作。)

【问题讨论】:

  • 没有阅读所有内容(或者说实话几乎没有阅读),但document.onmousedown = elementDrag(e); 可能不是您想要的。它将调用elementDrag(e) 的返回值设置为document.onmousedown 处理程序,即undefined,因为您不会从该函数返回任何内容。
  • 没有看足够多的代码来知道是否还有其他问题,所以现在不会关闭,而是“JavaScript - onclick event getting called automatically 的可能重复”。
  • 你说得对,这是我的问题之一。但是,我会礼貌地不同意,因为我的主要问题是鼠标移动时 e.clientY 没有改变。所以这是问题的一部分,但不是我目前要关注的问题。
  • 我们开始,问题已解决。应该更新 JS 代码,如果将其粘贴到文本编辑器中,error.innerHTML 应该根据情况更改:mousedown - "a"; mousedown 和 mousemove - "0, var count"; mouseup - “清除!”;不幸的是,这并不能解决我原来没有更改 e.clientY 的问题。所以如果有人有任何想法,请发表。谢谢! (另外,小错误。只要鼠标在文档上而不是 div 上按下,事件就会激活。不是很关心,只是想让你们知道。)

标签: javascript events draggable


【解决方案1】:

我找到了一个(某种)解决方案来解决我的问题。您可以在 http://jsfiddle.net/0rjt6svy/ 找到指向此的链接。对于有类似问题的任何人的一些更新: 1. 第 35 行有 func(e)。但是,删除 e 每次都会更改鼠标事件,因此 e.clientY 是不同的,因此是不同的变量,从而允许 div 移动。 2. 我删除了在第 44 行被注释掉的一行代码,它被注释掉了。所以它现在确实移动了,只是不像我想象的那样。我将继续更新它,如果有人有任何改进或修复它的建议,请告诉我! (另外,非常感谢 Kaiido 提出的惊人建议,如果没有这些建议我无法解决。)

HTML:
<div id="Border">
<div id="AddGraphsHere">
a
</div>
</div>
<button id="Button">
Click Here
</button>
<div id="Error">
TEXT
</div>

JS:
var b = document.getElementById("Button");
var area = document.getElementById("AddGraphsHere");
var etwo;
var count = 0;
var error = document.getElementById("Error");
var point = 1;
var interval;
var elmnt;
var canExecute;

b.onclick = function addDiv() {
area.innerHTML += "<div id='Graph'><div onmouseover='dragElement(this, event)' id='Point'></div></div>";
point = document.getElementById('Point');
}

function falert() {alert("Hello!"); this.onmousedown = dragMouseDown;}

//Start with a dragElement(Graph);

function dragElement(elmnt, e) {
  var pos1 = 0, pos2 = 0;
    /* if present, the header is where you move the DIV from:*/
    //error.innerHTML = elmnt;
    document.addEventListener("mousedown", function () {dragMouseDown();});

  function dragMouseDown(e) {
    //error.innerHTML = "dragMouseDown Activated!";
    e = e || window.event;
    //alert(e);
    // get the mouse cursor position at startup:
    pos2 = e.clientY;
    canExecute = true;
        error.innerHTML = "a";
    // call a function whenever the cursor moves:
    document.addEventListener("mousemove", function () {elementDrag();});
    //document.onmouseup = closeDragElement();
  }

  function elementDrag(e) {
    if (canExecute === true) {
      e = e || window.event;
            count = count + 1;
      // calculate the new cursor position:
      //pos2 = e.clientY;
      pos1 = pos2 - e.clientY;
      pos2 = e.clientY;
      // set the element's new position:
      elmnt.style.position = "absolute";

      elmnt.style.top = (elmnt.offsetTop - pos1) + "px";
      //elmnt.top = (elmnt.offsetTop - 
      //pos1) + "px";
      error.innerHTML = "(" + pos2 + " ; " + pos1 + "), " + count;
      //error.innerHTML = elmnt.offsetTop + " - " + pos1;

      document.addEventListener("mouseup", function() {closeDragElement();});
      }
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
    error.innerHTML = "Cleared!";
    canExecute = false;
  }
  }

CSS:
#Border {
  border: #000000 5px solid;
  width: 90%;
  height: 200px;
}
#Graph {
  width: 18%;
  height: 190px;
  float: left;
  border: 1px solid black;
  padding: 5px;
  font-family: sans-serif;
  font-size: 12px;
}
#Point {
  width: 8px;
  height: 8px;
  border-radius: 100%;
  border: 3px solid #0000FF;
  background-color: #AAAAFF;
  top: 100px;

  position: absolute;
  cursor: move;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多