admin管理员组文章数量:1426652
I am trying to achieve this effect with jQuery UI - very much like the way you crop an image on Facebook:
.html
Here is a very simple test case in HTML (an img
within a div
):
<div>
<img src="fat_cat.jpg">
</div>
and here is the logic that seems fit for the purpose - however it is unfinished:
$("img").draggable({ drag: dragHandler });
function dragHandler(event, ui) {
var x = event.target.x - event.target.parentNode.offsetLeft;
var y = event.target.offsetTop;
if(x > 0) {
// How to constrain the movement here?
}
if(x < -(event.target.offsetWidth -
event.target.parentNode.offsetWidth)) {
}
if(y > 0) {
}
if(y < -(event.target.offsetHeight -
event.target.parentNode.offsetHeight)) {
}
$("p").text(x + ", " + y);
}
My first attempts were to modify the offsetLeft
& offsetTop
variables, in all their multiple access points, but nothing seems to be working for me.
Here is a jsFiddle with what is explained above: /
I am trying to achieve this effect with jQuery UI - very much like the way you crop an image on Facebook:
http://blog.creonfx./examples/javascript/facebook-cropping-mootools.html
Here is a very simple test case in HTML (an img
within a div
):
<div>
<img src="fat_cat.jpg">
</div>
and here is the logic that seems fit for the purpose - however it is unfinished:
$("img").draggable({ drag: dragHandler });
function dragHandler(event, ui) {
var x = event.target.x - event.target.parentNode.offsetLeft;
var y = event.target.offsetTop;
if(x > 0) {
// How to constrain the movement here?
}
if(x < -(event.target.offsetWidth -
event.target.parentNode.offsetWidth)) {
}
if(y > 0) {
}
if(y < -(event.target.offsetHeight -
event.target.parentNode.offsetHeight)) {
}
$("p").text(x + ", " + y);
}
My first attempts were to modify the offsetLeft
& offsetTop
variables, in all their multiple access points, but nothing seems to be working for me.
Here is a jsFiddle with what is explained above: http://jsfiddle/g105b/FdkBK/
Share Improve this question asked Mar 28, 2011 at 19:50 GregGreg 21.9k17 gold badges88 silver badges109 bronze badges1 Answer
Reset to default 6You can actually do this with an inner container whose width/height is calculated to only allow the image to travel a certain distance. Of course you also have to position the inner container appropriately.
Here is my go at it:
Markup:
<div id="outer"> <!-- position: relative -->
<div id="inner"> <!-- position: absolute -->
<img src="">
</div>
</div>
Script:
var img = $("img").draggable({ containment: '#inner'}),
h = img.height(),
w = img.width(),
outer = $('#outer'),
oH = outer.height(),
oW = outer.width(),
iH = h + (h - oH),
iW = w + (w - oW),
iT = '-' + ((iH - oH)/2) + 'px',
iL = '-' + ((iW - oW)/2) + 'px';
$('#inner').css({ width: iW, height: iH, top: iT, left: iL });
本文标签:
版权声明:本文标题:javascript - jQuery UI draggable - constrain inner element within parent when inner element is larger than parent - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745387155a2656425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论