2

Possible Duplicate:
How do we prevent default actions in JavaScript?

In the code below I try to create a zoom feature for Charts. I tested it on IE and it worked but when I try it on Firefox, it seems that the browser doesn't cancel the default action and tries to drag the whole image. I cannot figure out why it does not work!

JsFiddle

<img onmousedown="startDrag" onmousemove="drag" onmouseup="stopDrag" src="ChartImageSource">
MouseDownPosition = null;

startDrag = function (s, e) {
    if (!this.MouseDownPosition && e.which == 1) {
        this.MouseDownPosition = {
            x: e.clientX,
            y: e.clientY
        };
        this.Box = document.createElement("div");
        $(this.Box).attr('id', 'zoomBox').css({ 'border-style': 'solid', 'border-color': 'black', 'border-width': '1px', 'position': 'absolute', 'z-index': '1000', 'left': e.clientX + 'px', 'top': e.clientY + 'px', 'width': '0px', 'height': '0px' });
        $(s).parent().append(this.Box);
    }
};
drag = function (s, e) {
    if (this.MouseDownPosition) {
        e.preventDefault();
        var initX = this.MouseDownPosition.x;
        var initY = this.MouseDownPosition.y;
        var curX = e.clientX;
        var curY = e.clientY;
        var left;
        var top;
        var width;
        var height;
        if (curX - initX >= 0) {
            width = curX - initX + 'px';
            left = initX + 'px';
        } else {
            width = -curX + initX + 'px';
            left = curX + 'px';
        }
        if (curY - initY >= 0) {
            height = curY - initY + 'px';
            top = initY + 'px';
        } else {
            height = -curY + initY + 'px';
            top = curY + 'px';
        }
        $(this.Box).css({ 'width': width, 'height': height, 'left': left, 'top': top });
    }
};
stopDrag = function (s, e) {
    if (this.MouseDownPosition) {
        var initX = this.MouseDownPosition.x;
        var initY = this.MouseDownPosition.y;
        var curX = e.clientX;
        var curY = e.clientY;
        var curOffX = e.offsetX;
        var curOffY = e.offsetY;
        var initOffX = ((curX - initX) ? initX - curX : -(initX - curX)) + curOffX;
        var initOffY = ((curY - initY) ? initY - curY : -(initY - curY)) + curOffY;
        var MinPixX = initOffX <= curOffX ? initOffX : curOffX;
        var MaxPixX = initOffX >= curOffX ? initOffX : curOffX;
        var MinPixY = initOffY <= curOffY ? initOffY : curOffY;
        var MaxPixY = initOffY >= curOffY ? initOffY : curOffY;
        s.parentNode.removeChild(this.Box);
        this.MouseDownPosition = null;
    }
};
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
daniatic
  • 502
  • 4
  • 18

2 Answers2

1

You generally need to return false; from the event handlers to bypass the default action.

Alternatively, jQuery provides preventDefault() and stopPropagation() functions. You'll probably need to call both of them, but IMO that's very verbose and the alternative (return false;) also works when you're not using jQuery.

Further discussion on the alternatives

Community
  • 1
  • 1
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
1

e.preventDefault(); is not enough. You'll need to call e.stopPropagation();, too.

See: http://www.quirksmode.org/js/events_order.html

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79