2013-04-29 133 views
0

我正在尝试旋转和调整元素(我的roomplanner中的'家具')。 每个元素(图像)都有一个div包装。这个div是可拖动的,图像可以旋转。有一个第二个div包裹在我用来调整大小的图像上。位置旋转div

<div class="element furniture" height="53" width="146"> 
    <div class="imageholder"> 
     <img width="146" height="53" src="/images/furniture/bankstel_3zits.png" /> 
    </div> 
</div> 

旋转之后,我将div的高度设置为图像的宽度,将div的宽度设置为图像的高度。但是,当我旋转,div的是在另一个位置,然后内部的图像..我尝试使用边距,但我想这不是正确的解决方案?

我设置一个例子: http://jsfiddle.net/Hqcnh/6/

+0

默认情况下此小提琴,你的形象是围绕其旋转中心点。你为什么不旋转整个div而不是内部的图像? – ahren 2013-04-29 10:40:27

+0

那么,首先我旋转整个div,但然后我有一个调整大小的问题..(手柄不会旋转) – 2013-04-29 11:13:12

回答

1

旋转父格,其他的一切都会旋转

$(document).ready(function() { 
    $('.rotate').click(function() { 
     Rotate(); 

    }); 
    function Rotate(){ 
     var clickedElement = $('.element.furniture'); 
     var imageHolder = clickedElement.find('.imageholder'); 
     var clickedElementToRotate = $('.element.furniture'); 

     var degrees = 90; 
     if (typeof (clickedElementToRotate.attr('rotate')) !== 'undefined') { 
      degrees = parseFloat(clickedElementToRotate.attr('rotate')) + 90; 
     } 

     clickedElementToRotate.attr('rotate', degrees); 
     clickedElementToRotate.cssrotate(degrees); 

     if (degrees == 90 || degrees == 270) { 
      clickedElementToRotate.attr('data-width', clickedElementToRotate.height()).attr('data-height', clickedElementToRotate.width()); 
     } else { 
      clickedElementToRotate.attr('data-width', clickedElementToRotate.width()).attr('data-height', clickedElementToRotate.height()); 
     } 



    } 
    $("div.element.furniture").each(function() { 
     var div = $(this); 
     var img = $(this).find('img'); 

     div.draggable(); 
     div.css('border', '1px solid red'); 
     div.width(img.width()); 
     div.height(img.height()); 

     img.wrap('<div class="imageholder" />'); 
     div.find('.imageholder').css('border', '1px solid blue').css('width', div.width()); 
     div.find('.imageholder').css('height', div.height()); 

     div.find('.imageholder').resizable({ 
      handles: "n, e, s, w", 
      aspectRatio: true, 
      resize: function (event, ui) { 

       if (img.attr('rotate') == 90 || img.attr('rotate') == 270) { 
        img.css('width', ui.size.height); 
        img.css('height', ui.size.width); 
       } else { 
        img.css('width', ui.size.width); 
        img.css('height', ui.size.height); 
       } 

       div.css('width', ui.size.width); 
       div.css('height', ui.size.height); 
      } 
     }); 
    }); 
}); 


(function ($) { 
    var _e = document.createElement("canvas").width; 
    $.fn.cssrotate = function (d) { 
     return this.css({ 
      '-moz-transform': 'rotate(' + d + 'deg)', 
       '-webkit-transform': 'rotate(' + d + 'deg)', 
       '-o-transform': 'rotate(' + d + 'deg)', 
       '-ms-transform': 'rotate(' + d + 'deg)' 
     }).prop("rotate", _e ? d : null); 
    }; 
    var $_fx_step_default = $.fx.step._default; 
    $.fx.step._default = function (fx) { 
     if (fx.prop != "rotate") return $_fx_step_default(fx); 
     if (typeof fx.elem.rotate == "undefined") fx.start = fx.elem.rotate = 0; 
     $(fx.elem).cssrotate(fx.now); 
    }; 
})(jQuery); 

看到这里FIDDLE

+0

Thnx,但现在我有一个调整大小的问题..(手柄不旋转),这就是为什么我只旋转图像。 – 2013-04-29 11:13:43