Martin said:
On my latest drag code,
http://www.martinrinehart.com/examples/js-drag-code.html
the blue slider is constrained. This is the entire display function
(excluding the end-of-drag logic):
y = Math.max( Math.min(400,y), 100 );
blue.me.style.top = y + 'px';
The slider is not bound in Opera 9, PC. The slider is constrained to y
between 100 and 400 in six other browsers I just tested. Is it time to
quit?
Well, lets see.
<body
onselectstart='return false'
onmousedown='return false'
onload=init()>
That makes for a pretty bad example. It is invalid and I had to
disabled scripting to select text on the page. Attach and detach the
mousedown listener on drag and drop respectively. I don't know why
you feel you need the selectstart event as I've never used it for
anything.
dragger = {
MAX: 1000000,
Why is this all caps?
callback: undefined, // function to send x, y
You can lose this.
// wrinkle: The mouse is not likely to be on the
// top/left corner of the draggable.
// object = mouse + delta
deltaX: undefined, deltaY: undefined,
mouseX: undefined, mouseY: undefined,
objectX: undefined, objectY: undefined,
ceiling: undefined, // event trapper
And these.
init: function () {
this.ceiling =
document.createElement( 'div' );
var z = this.ceiling.style;
z.position = 'absolute'; z.top = 0; z.left = 0;
Strings not numbers.
z.zIndex = this.MAX;
this.ceiling.onmousemove = dragger.moved;
this.ceiling.onmouseup = dragger.stopDrag;
I would lose the "ceiling" too.
}, // end of init()
moved: function ( evt ) {
if ( !evt ) { evt = window.event; }
// 'this' is the drag object
dragger.objectX = evt.clientX + dragger.deltaX;
dragger.objectY = evt.clientY + dragger.deltaY;
That won't work reliably cross-browser. Did you look at examples of
mouse position reporting? And it is best to re-position the element
based on the difference between the original and current mouse
position. Then you can lose deltaX/Y.
dragger.callback(
dragger.objectX, dragger.objectY );
},
startDrag: function ( callback, evt, location_ ) {
this.callback = callback;
this.mouseX = evt.clientX;
this.mouseY = evt.clientY;
You can't rely on clientX/Y alone.
this.objectX = location_.left_;
this.objectY = location_.top_;
What is with the underscores?
this.deltaX = this.objectX - this.mouseX;
this.deltaY = this.objectY - this.mouseY;
You don't need this.
if ( !this.ceiling ) { this.init(); }
this.ceiling.style.height =
document.body.clientHeight;
this.ceiling.style.width =
document.body.clientWidth;
This will only work in quirks mode in IE >= 6. And what of the
documentElement?
[snip]
Is it time to quit? Depends on what you want to get out of this.
Certainly it isn't ready for prime time. I tried it on FF3 and it was
unusable. Every time the boxes hit an impediment, I would end up
dragging with the mouse button up. Trying to cancel the drag in this
case seemed to be a problem as well.