Reputation: 14927
I am trying to scale an element and drag this element to do a WorkSpace that can work in all screen resolutions.
The problem I have it that I can't do it works fine. If you try the Fiddle Code you can see that the elements that is being resized doesn't going to the final corner. I have read a lot of post like this (jQuery Drag/Resize with CSS Transform Scale) but the problem is when you reference a containment.
The fiddle code with the error example:
Updated: added text too
HTML
<div id="containerDragable">
<img id="img_messi" style="display: inline; position: absolute; z-index: 101; " src="http://www.elconfidencial.com/fotos/noticias_2011/2012090245messi_dentro.jpg" />
<div id="text_example">hi!</div>
CSS
#containerDragable {
width: 80vw;
height: 45vw;
background-color: rgb(227, 227, 227);
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
position:absolute;
}
#img_messi
{
-webkit-transform-origin: 0 0;
-moz-transform-origin: 0 0;
-ms-transform-origin: 0 0;
-o-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scale(0.5);
position:absolute;
}
#text_example
{
font: normal 36px arial,sans-serif;
position:absolute;
-webkit-transform-origin: 0 0;
-moz-transform-origin: 0 0;
-ms-transform-origin: 0 0;
-o-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scale(0.5);
}
JavaScript
$(function () {
$('#img_messi').draggable({
containment: "#containerDragable", cursor: "move", scroll: false
});
});
$(function () {
$('#text_example').draggable({
containment: "#containerDragable", cursor: "move", scroll: false
});
});
Thanks!!
Upvotes: 0
Views: 1034
Reputation: 8199
Don't use CSS
to scale your image. Use jQuery
instead :
$('#img_messi').width($('#img_messi').width()/2);
Edit :
To fit better to your needs, if you have to use the scale
css property, you should use this simple workaround with negatives margins :
$(this).css('margin', '0 -'+($(this).width()/2)+'px -'+($(this).height()/2)+'px 0');
That's quite ugly yet it works...
Edit : with dynamic ratio
Ok this is a tough one... The main trick here is to set the margin to top, right, bottom and left.
See this :
var verticalOffset = ($(this).height() - $(this).height()*ratio)*0.5;
var horizontalOffset = ($(this).width() - $(this).width()*ratio)*0.5;
Here $(this).height()
is the original height of the DOM (same for width). You substract to this itself times the ratio, and then divide the whole by 2 (*0.5
) because the margins are on top and bottom (same for left and right).
Upvotes: 2