简体   繁体   English

请解释这段代码的含义

[英]Please explain what this code means

I'm going through the tutorial at the address: http://www.stanford.edu/~ouster/cgi-bin/cs142-spring12/lecture.php?topic=event . 我正在浏览地址: http : //www.stanford.edu/~ouster/cgi-bin/cs142-spring12/lecture.php?topic=event

And I don't understand about code at the lines that I have marked with asterisks. 我不明白我用星号标记的代码。

function Dragger(id) {
    this.isMouseDown = false;
    this.element = document.getElementById(id);
    var obj = this;
    this.element.onmousedown = function(event) {
        obj.mouseDown(event);
    }
}

Dragger.prototype.mouseDown = function(event) {
    var obj = this;
    this.oldMoveHandler = document.body.onmousemove;  /******/
    document.body.onmousemove = function(event) {     /******/
        obj.mouseMove(event);
    }
    this.oldUpHandler = document.body.onmouseup;      /******/
    document.body.onmouseup = function(event) {       /******/
        obj.mouseUp(event);
    }
    this.oldX = event.clientX;
    this.oldY = event.clientY;
    this.isMouseDown = true;
}

Dragger.prototype.mouseMove = function(event) {
    if (!this.isMouseDown) {
        return;
    }
    this.element.style.left = (this.element.offsetLeft
            + (event.clientX - this.oldX)) + "px";
    this.element.style.top = (this.element.offsetTop
            + (event.clientY - this.oldY)) + "px";
    this.oldX = event.clientX;
    this.oldY = event.clientY;
}

Dragger.prototype.mouseUp = function(event) {
    this.isMouseDown = false;
    document.body.onmousemove = this.oldMoveHandler;   /******/
    document.body.onmouseup = this.oldUpHandler;       /******/
}

The purpose of the this.oldMoveHandler references are to store whatever event handlers a previous developer of the page may have added to "document.body.onmousemove", with the goal of not interrupting whatever behavior that developer no doubt spend painful hours to build. this.oldMoveHandler引用的目的是存储页面的前一个开发人员可能已添加到“document.body.onmousemove”的任何事件处理程序,其目的是不中断开发人员无疑花费痛苦时间构建的任何行为。 It goes like this: 它是这样的:

  1. Press down with the mouse, store the old handler, add our fancy dragging handler. 用鼠标按下,存储旧的处理程序,添加我们的花式拖动处理程序。

  2. Move the mouse, lovely dragging behavior occurs. 移动鼠标,发生可爱的拖动行为。

  3. Release the mouse, dragging behavior stops, restore old handler (even if it's null). 释放鼠标,拖动行为停止,恢复旧处理程序(即使它为null)。

This is a way to stay out of the way of previous code, although it's a bad solution. 这是一种避免使用以前代码的方法,尽管这是一个糟糕的解决方案。 The much preferred way is to use addEventListener/removeEventListener or attachEvent/detachEvent for barbaric IE browsers. 最喜欢的方法是对野蛮的IE浏览器使用addEventListener / removeEventListener或attachEvent / detachEvent。 These functions are designed so that multiple handlers can subscribe to the same event without stepping on each other. 这些函数的设计使得多个处理程序可以订阅相同的事件而不会相互踩踏。 Here's some good reading: 这里有一些很好的阅读:

http://www.quirksmode.org/js/introevents.html http://www.quirksmode.org/js/introevents.html

Setting document.body.onmousemove is one (ugly) way to listen for mousemove events on the document.body element. 设置document.body.onmousemove是一种(丑陋)方式来监听document.body元素上的mousemove事件。

Therefore, this.oldMoveHandler = document.body.onmousemove; 因此, this.oldMoveHandler = document.body.onmousemove; is simply storing a reference to the event handler function, if any. 只是存储对事件处理函数的引用(如果有的话)。

Please note that using element.addEventListener is preferred for attaching event handlers. 请注意,首选使用element.addEventListener来附加事件处理程序。

As noted in the comments the use of intrusive event handling is very old fashioned and not recommended now. 正如评论中所指出的,侵入式事件处理的使用非常古老,现在不推荐使用。

However to answer your question the code is implementing drag and drop, when the mousedown event is triggered by a mouse press the current event handlers for mouseup and mouseover (first 4 lines of marked code) are "saved" and replaced by the event handlers that will perform the drag and drop. 但是回答代码实现拖放,当鼠标按下事件是由按下鼠标的mouseup鼠标悬停 (第4行的标记代码)当前的事件处理程序“拯救”,取而代之的是事件处理程序触发了您的疑问,将执行拖放操作。

When the dragged element is "dropped" ie the mouseup event fires, the mousemove and mouseup event handlers are replaced with the original event handlers that were saved (last 2 lines of marked code) 当拖动的元素被“删除”,即mouseup事件触发时, mousemovemouseup事件处理程序将被保存的原始事件处理程序(最后2行标记代码)替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM