开发者

Javascript 'evt is undefined' but works in Chrome

开发者 https://www.devze.com 2023-01-29 07:02 出处:网络
function ganttChart(gCon开发者_运维技巧tainerID) { this.gContainer = document.getElementById(gContainerID);
function ganttChart(gCon开发者_运维技巧tainerID) {

    this.gContainer = document.getElementById(gContainerID); 
    this.gCurrentMouseX;
    this.gCurrentMouseY;

    this.gAttatchMove = function(self) {

        self.gContainer.onmousemove = function() {


            if (self.gIsDraggingBar) {

                self.gUpdateMousePos();
                self.gBarBeingDragged.style.left = (self.gBarStartX - (self.gMouseStartX - self.gCurrentMouseX)) + "px";
            }

        };

        self.gContainer.onmouseup = function() {
            self.gIsDraggingBar = false;
        };

    }
    var self = this;
    this.gAttatchMove(self);

   // Update mouse coords
    this.gUpdateMousePos = function(evt) {
        if (window.event) {
            this.gCurrentMouseX = event.x;
            this.gCurrentMouseY = event.y;
        }
        else {
            this.gCurrentMouseX = evt.x;
            this.gCurrentMouseY = evt.y;
        }
    }

This works fine in Chrome, but FF throws the error:

evt is undefined


You need to pass the mousemove event into this.gUpdateMousePos():

self.gContainer.onmousemove = function(evt) {
    if (self.gIsDraggingBar) {
        self.gUpdateMousePos(evt);
        // And the rest follows here
    }
};

IE only supplies the current event via window.event. Firefox only supplies it via a parameter passed into the event handler function (which you were not passing on, hence the error). Chrome and Safari do both (hence no error in Chrome).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号