Zooming an draggable image keeping the center fix
I have an script that will resize an image with a jquery slider. The image is inside an overflow hidden container. But I am getting problem in keeping the draggable center of the image fix while zooming out or in. Its working fine while the image is not dragged. I know that can be done by plus minus the left and top value while zooming in out. But can't able to find a way to do it. You can View my script-
- var globalElement_width;
- var globalElement_height;
- $(document).ready(function () {
- $img = $('.canvas img');
- globalElement_width = $img.width();
- globalElement_height = $img.height();
- $(".slider").slider({
- step: 1,
- min: 0,
- max: 100,
- value: 0,
- slide: function (event, ui) {
- resize_img(ui.value);
- }
- });
- $('.canvas img').draggable();
- });
- function resize_img(val) {
- var width = globalElement_width;
- var zoom_percen = width * 0.5; // Maximum zoom 50%
- var ASPECT = zoom_percen / 50;
- var zoom_value = val / 2;
- var size = width + ASPECT * zoom_value;
- var d = ASPECT * zoom_value / 2;
- $img = $('.canvas img');
- $img.stop(true).animate({
- top: -d,
- left: -d,
- width: size
- }, 250);
- }
Any help? Thanks in advance.