简体   繁体   English

Javascript firefox无法找到所有其他浏览器所做的值

[英]Javascript firefox can't find values all other browsers do

function ganttChart(gContainerID) {

    this.gContainer = document.getElementById(gContainerID);    // The container the chart          this.gCurrentDragBar = -1;
    this.gIsDraggingBar = false;
    this.gMouseStartX = 0;
    this.gMouseStartY = 0;
    this.gBarStartX = 0;
    this.gBarStartY = 0;
    this.gBarBeingDragged;
    this.gCurrentMouseX;
    this.gCurrentMouseY;

    // On mouse move
    this.gAttatchMove = function(self) {

        self.gContainer.onmousemove = function(evt) {
            if (self.gIsDraggingBar) {

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

                //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                // PRINTS MESSAGE HERE
                document.getElementById("db").innerHTML = "Bar start: " + self.gBarStartX + "<br />Mouse start:" + self.gMouseStartX + "<br />Mouse current:" + self.gCurrentMouseX;
                //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            }
        }; 
        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;
        }
    }

    // Sets up the bar mouse events
    gSetupMouseEvents = function(gBar, i, self) {

        gBar.onmouseover = function() {
            gBarHighlight(gBar, self.gData[i][1], self.gData[i][2]);
        };
        gBar.onmouseout = function() {
            gBarUnHighlight(gBar, self.gData[i][1], self.gData[i][2]);
        };
        gBar.onmousedown = function(evt) {

            // Initialise
            self.gCurrentDragBar = i;
            self.gBarBeingDragged = document.getElementById("gBar" + i);
            self.gIsDraggingBar = true;

            // Set initial positions
            self.gUpdateMousePos(evt);
            self.gMouseStartX = self.gCurrentMouseX;
            self.gMouseStartY = self.gCurrentMouseY;
            self.gBarStartX = self.gBarBeingDragged.offsetLeft;
            self.gBatStartY = self.gBarBeingDragged.offsetTop;
        };


    }

}

The values printed out are: 打印出的值是:

Firefox: 火狐:

Bar start: 0
Mouse start:undefined
Mouse current:undefined

Chrome

Bar start: 0
Mouse start:163
Mouse current:165

IE IE

Bar start: 1
Mouse start:3
Mouse current:19

I'm not worried about varying values between IE/Chrome I can debug that (just the way mouse position is interpreted) but firefox not picking any values is confusing me! 我并不担心IE / Chrome之间的变化值我可以调试它(只是解释鼠标位置的方式)但firefox没有选择任何值让我感到困惑!

鼠标位置在事件对象的“clientX”和“clientY”属性中给出,而不是“x”和“y”。

    this.gCurrentMouseX = evt.x;

You don't want to use .x . 你不想使用.x It doesn't exist on a standard DOM MouseEvent object (which is why Firefox doesn't have it) and even in IE, it's usually not what you want. 它不存在于标准的DOM MouseEvent对象上(这就是为什么Firefox没有它),甚至在IE中,它通常不是你想要的。 It's relative to the nearest positioned ancestor, where the meaning of 'positioned' may vary. 它与最近的祖先相对,其中“定位”的含义可能会有所不同。 It's generally better to get page-relative co-ordinates. 获得与页面相关的坐标通常更好。

Unfortunately, the pageX property that gives you this is also not a standard Event property, so although it is supported by IE9, Firefox, WebKit etc, you can't guarantee it's there. 不幸的是,为您提供此功能的pageX属性也不是标准的Event属性,因此尽管IE9,Firefox,WebKit等支持它,但您无法保证它存在。 You can instead use clientX , which is standard, but is relative to the viewport, so to correct it in case the viewport has scrolled, you have to add the scrollLeft of the documentElement (unless you're in IE Quirks Mode, in which case it's the body instead... don't be in Quirks Mode!). 您可以使用clientX ,它是标准的,但是相对于视口,所以要在视口滚动的情况下更正它,您必须添加documentElementscrollLeft (除非您处于IE Quirks模式,在这种情况下)它是body而不是......不要处于Quirks模式!)。

(The other standard positioning property on MouseEvent is screenX , but that's largely useless except for positioning popups. There are also the non-standard properties offsetX and layerX which are even more useless.) (MouseEvent上的另一个标准定位属性是screenX ,但除了定位弹出窗口之外,这几乎没用。还有非标准属性offsetXlayerX ,它们更加无用。)

    if (window.event) {

Better to test for the standard first ( evt not being undefined ), and only fall back to window.event for IE<9. 最好先测试标准( evt not undefined ),然后只回到window.event ,IE <9。 Otherwise you may not get what you expect on browsers that support both, or where there's something else (eg a variable or element) defined called event . 否则,您可能无法在支持两者的浏览器上获得您期望的内容,或者在其他地方(例如变量或元素)定义了名为event

// Update mouse coords
this.gUpdateMousePos = function(evt) {
    if (evt===undefined) evt= window.event;

    if ('pageX' in evt) {
        this.gCurrentMouseX = evt.pageX;
        this.gCurrentMouseY = evt.pageY;
    } else {
        this.gCurrentMouseX = evt.clientX+document.documentElement.scrollLeft;
        this.gCurrentMouseY = evt.clientY+document.documentElement.scrollTop;
    }
}

This gives you page-relative co-ordinates. 这为您提供了页面相对坐标。 If you want to calculate position relative to a particular element you then have to subtract the page-relative co-ordinates of that element, typically using an offsetLeft / offsetParent loop. 如果要计算相对于特定元素的位置,则必须减去该元素的页面相对坐标,通常使用offsetLeft / offsetParent循环。 Though there are lots more potential browser problems there too. 虽然还有更多潜在的浏览器问题。 Sigh. 叹。 Frameworks can help here. 框架可以在这里提供帮助。 Although to be honest most of them still trip up on some of the uglier corner cases. 虽然说实话,他们中的大多数人仍然会遇到一些丑陋的角落案件。

(It would be jolly nice if some helpful organisation would specify MouseEvent pageX / pageY as being standard, and add element/view pageLeft / pageTop properties to match... I can dream...) (如果一些有用的组织将MouseEvent pageX / pageY指定为标准,并且添加元素/视图pageLeft / pageTop属性以匹配......那将是非常好的...我可以梦想...)

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

相关问题 简单的Javascript在FireFox中不起作用(但在所有其他浏览器中都有效) - Simple Javascript doesn't work in FireFox (but does in ALL other Browsers) javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作? - How can javascript work perfectly in FireFox, but not work at all in other browsers? Firefox无法识别Java代码,但可以在所有其他浏览器中使用 - Javascript code not understood by Firefox But works in all other browsers JavaScript适用于Firefox,但不适用于其他浏览器 - JavaScript working in Firefox but not other browsers Firefox 无法调度然后刷新页面,但其他浏览器可以 - Firefox can't dispatch and then refresh page but other browsers can Firefox不能正确渲染SVG,其他浏览器也是如此 - Firefox doesn't render SVG properly, other browsers do 为什么这个 JavaScript 在 Firefox 而不是其他浏览器中工作? - Why is this JavaScript working in Firefox but not other browsers? 如何在Firefox以外的所有其他浏览器中实现此功能 - How to make this work in all other browsers other than Firefox JavaScript无法在IE上运行,但在所有其他浏览器中都可以 - JavaScript not working on IE, but OK in all other browsers Firefox和其他浏览器的事件监听器是否有所不同? - Do Firefox and other browsers do event listeners differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM