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 });
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387155a4625489.html
评论列表(0条)