javascript - Drag two images together, but limit one's movement to vertical axis -
i'd move 2 images on page.the layout of following:
|1.1|--2.1--| |1.2|--2.2--| |1.3|--2.3--| |1.4|--2.4--|
so images next each other, cells start '1' belong first image, start '2' belong second image.
when drag of images expected behaviour both images move, image 1 on vertical axis. (so remains on left, might move or down as image 2. image used sort of header, , needs visible on left time, needs vertically in sync image 2.), image 2 can move along both axes.
in example means 1.1 part of first image in line 2.1 part of second image.
is there js framework might support this? i've tried using fabric js, when cap coordinates in event handler becomes unbelievably slow.
this code i've tried, doesn't i've described, restricts movement rectangle, theory behind same.
canvas.on("object:moving", function() { var top = movingbox.top; var bottom = top + movingbox.height; var left = movingbox.left; var right = left + movingbox.width; var topbound = boundingbox.top; var bottombound = topbound + boundingbox.height; var leftbound = boundingbox.left; var rightbound = leftbound + boundingbox.width; movingbox.setleft(math.min(math.max(left, leftbound), rightbound - movingbox.width)); movingbox.settop(math.min(math.max(top, topbound), bottombound - movingbox.height)); });
this can achieved using jquery ui, 2 separate draggable elements, 1 constrained move on 'y' axis, , both update each other 'top' value upon dragging.
** html **
<img id="img1" src="http://placehold.it/100x100" /> <img id="img2" src="http://placehold.it/100x100" />
** js **
var img1 = $('#img1'), img2 = $('#img2'); img1.draggable({ axis: 'y', drag: function(event, ui){ img2.css('top', ui.position.top+'px'); } }); img2.draggable({ drag: function(event, ui){ img1.css('top', ui.position.top+'px'); } });
check working in jsfiddle :-)
Comments
Post a Comment