-
Notifications
You must be signed in to change notification settings - Fork 786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option for snap and restrict to be relative to the element's position and dimentions #72
Conversation
interact('*').snap({ elementOrigin: { x: 0, y: 0 } }); elementOrigin.x/y are scalars specifying the position on the element to which snapping should be relative. {x: 0, y: 0} is the top left {x: 0.5, y: 0.5} is the center {x: 1, y: 1} is the bottom right interact('*').restrict({ elementRect: { top: 0, left: 0, bottom: 1, right: 1 } }); For the left and right properties, 0 means the very left of the element and 1 means the very right. For top and bottom, 0 means the top of the element and 1 means the bottom. { top: 0.25, left: 0.25, bottom: 0.75, right: 0.75 } would result in a quarter of the element being allowed to hang over the restriction edges.
CC @jordandh (#32) @dexusnl (#47) @robertCSN (#59) Implementing this was tricky and I expect that there are a few bugs. Any help with testing would be fantastic. If this fix is good then you should be able to remove the workarounds that I suggested for snapping and restricting and use your original settings with the addition of http://rawgit.com/taye/interact.js/snap-restrict-element-rect/interact.js Cheers |
- make snap.elementOrigin `null` by default - fix a bug when restricting without elementRect
... while elementRect is set
@taye I'm trying this branch out but I am having trouble with the settings. I am not really sure how to use them for my slideshow example. Can you explain the settings further or help me nail them down? For say a 3 slide slideshow? |
I tried using your demo at http://codepen.io/jordandh/pen/xlhgG and the snapping seems to work if I remove the code in dragstart that recalculates the snap anchors and instead use normal anchors for snapping. .snap({
mode: 'anchor',
anchors: [
{ x: -slideWidth * 2, y: 0 },
{ x: -slideWidth, y: 0 },
{ x: 0, y: 0 }
],
elementOrigin: {x: 0, y: 0},
endOnly: true
})
.on('dragstart', function (event) {
var $target = $(event.target);
$target.removeClass('slotting');
$target.closest('.slideshow').find('.slideshow-image').removeClass('slotting');
}) |
I updated my demo to use the changes. It works when the body has zero padding but if you give the body padding (40px in the codepen example) then the snap points appear to be off by 40px. I tried changing the elementOrigin to You can view the problem with 40px body padding here: http://codepen.io/jordandh/pen/sFDmo |
The x and y values of the elementOrigin are multiplied by the width and height of the element and added to the offset -
You can instead change the interactable's
|
Setting the It also does not have a smooth end transition like in pull request #69 but I assume you are going to merge these branches if both prove fruitful. |
The problem might be the
Yep. But here's what it would look like if I merged all the pending fixes now: http://taye.me/etc/int-merged.js |
Well with those issues being fixed in other branches I would say that the changes in this branch work for my use case. |
Excellent. Thanks for your help! |
Snap elementOrigin
elementOrigin.x/y are scalars specifying the position on the element to
which snapping should be relative.
Restrict elementRect
For the left and right properties, 0 means the very left of the element
and 1 means the very right. For top and bottom, 0 means the top of the
element and 1 means the bottom.
{ top: 0.25, left: 0.25, bottom: 0.75, right: 0.75 }
would result in aquarter of the element being allowed to hang over the restriction edges.
Closes #47
Closes #59