<i id='wgaMu'><tr id='wgaMu'><dt id='wgaMu'><q id='wgaMu'><span id='wgaMu'><b id='wgaMu'><form id='wgaMu'><ins id='wgaMu'></ins><ul id='wgaMu'></ul><sub id='wgaMu'></sub></form><legend id='wgaMu'></legend><bdo id='wgaMu'><pre id='wgaMu'><center id='wgaMu'></center></pre></bdo></b><th id='wgaMu'></th></span></q></dt></tr></i><div id='wgaMu'><tfoot id='wgaMu'></tfoot><dl id='wgaMu'><fieldset id='wgaMu'></fieldset></dl></div>
      <bdo id='wgaMu'></bdo><ul id='wgaMu'></ul>
  1. <tfoot id='wgaMu'></tfoot>

    <small id='wgaMu'></small><noframes id='wgaMu'>

    1. <legend id='wgaMu'><style id='wgaMu'><dir id='wgaMu'><q id='wgaMu'></q></dir></style></legend>

    2. 改变宽度/高度移动旋转的元素

      时间:2023-10-13
      <tfoot id='x1xYY'></tfoot>

      1. <small id='x1xYY'></small><noframes id='x1xYY'>

            <tbody id='x1xYY'></tbody>
            <bdo id='x1xYY'></bdo><ul id='x1xYY'></ul>

              <legend id='x1xYY'><style id='x1xYY'><dir id='x1xYY'><q id='x1xYY'></q></dir></style></legend>
                <i id='x1xYY'><tr id='x1xYY'><dt id='x1xYY'><q id='x1xYY'><span id='x1xYY'><b id='x1xYY'><form id='x1xYY'><ins id='x1xYY'></ins><ul id='x1xYY'></ul><sub id='x1xYY'></sub></form><legend id='x1xYY'></legend><bdo id='x1xYY'><pre id='x1xYY'><center id='x1xYY'></center></pre></bdo></b><th id='x1xYY'></th></span></q></dt></tr></i><div id='x1xYY'><tfoot id='x1xYY'></tfoot><dl id='x1xYY'><fieldset id='x1xYY'></fieldset></dl></div>
              • 本文介绍了改变宽度/高度移动旋转的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当改变旋转元素的宽度/高度时,元素会移动!

                When changing the width/height of a rotated elements, the element moves!

                这是一个例子
                JSFiddle

                Here's an example
                JSFiddle

                例如,当我更改宽度时,对象会失去其原始位置,这会影响 jQueryUI Resizable 并使其无法使用.

                When I change the width for example, the object loses its original position, this is effecting jQueryUI Resizable and making it unusable.

                CSS:

                .test{
                    position: absolute;
                    top: 200px;
                    left; 200px;
                
                    width: 400px;
                    height: 200px;
                    background: red;
                
                    transform: rotate(-90deg);
                }
                

                JSFiddle
                是否有一个库或函数可以通过反转此效果来纠正此问题.

                JSFiddle
                Is there a library or a function that corrects this issue by reversing this effect.

                我制作了一个 jQuery 函数,它在调整旋转元素大小时纠正左上角的位置,作为答案添加.这也是 jqueryui 可调整大小的补丁

                I made a jQuery function that corrects the top-left position upon resizing a rotated element, added as answer. Also this is a patch for jqueryui resizable

                推荐答案

                这个jQuery函数会改变元素的大小并进行必要的修正以保留左上角的位置

                This jQuery function will change the size of the element and make necessary correction to preserve the top-left corner position

                JSFiddle

                /**
                * Resizes rotated set of elements and preserves top-left corner position
                * @param {Number} new_width
                * @param {Number} new_height
                * @param {Number} angle in degrees
                * @returns {object} the current jQuery set
                */
                $.fn.rotSize = function(new_width, new_height, angle){
                
                    //Convert angle from degrees to radians
                    var angle = angle * Math.PI / 180
                
                    $(this).each(function(i, elem){
                        var $elem = $(elem);
                        //initial CSS position.
                        var pos = {left: parseInt($elem.css('left')), top: parseInt($elem.css('top'))};
                        var init_w = $elem.width();
                        var init_h = $elem.height();
                        //Get position after rotation with original size
                        var x = -init_w/2;
                        var y = init_h/2;
                        var new_x = y * Math.sin(angle) + x * Math.cos(angle);
                        var new_y = y * Math.cos(angle) - x * Math.sin(angle);
                        var diff1 = {left: new_x - x, top: new_y - y};
                
                        //Get position after rotation with new size
                        var x = -new_width/2;
                        var y = new_height/2;
                        var new_x = y * Math.sin(angle) + x * Math.cos(angle);
                        var new_y = y * Math.cos(angle) - x * Math.sin(angle);
                        var diff2 = {left: new_x - x, top: new_y - y};
                
                        //Get the difference between the two positions
                        var offset = {left: diff2.left - diff1.left, top: diff2.top - diff1.top};
                        //Calculate the correction
                        var new_pos = {left: pos.left - offset.left, top: pos.top + offset.top};
                
                        //Apply
                        $elem.css(new_pos);
                        $elem.css({width: new_width, height: new_height});
                    });
                }
                

                另一个有用的功能:

                /**
                * Calculate the size correction for resized rotated element
                * @param {Number} init_w
                * @param {Number} init_h
                * @param {Number} delta_w
                * @param {Number} delta_h
                * @param {Number} angle in degrees
                * @returns {object} correction css object {left, top}
                */
                $.getCorrection = function(init_w, init_h, delta_w, delta_h, angle){
                    //Convert angle from degrees to radians
                    var angle = angle * Math.PI / 180
                
                    //Get position after rotation with original size
                    var x = -init_w/2;
                    var y = init_h/2;
                    var new_x = y * Math.sin(angle) + x * Math.cos(angle);
                    var new_y = y * Math.cos(angle) - x * Math.sin(angle);
                    var diff1 = {left: new_x - x, top: new_y - y};
                
                    var new_width = init_w + delta_w;
                    var new_height = init_h + delta_h;
                
                    //Get position after rotation with new size
                    var x = -new_width/2;
                    var y = new_height/2;
                    var new_x = y * Math.sin(angle) + x * Math.cos(angle);
                    var new_y = y * Math.cos(angle) - x * Math.sin(angle);
                    var diff2 = {left: new_x - x, top: new_y - y};
                
                    //Get the difference between the two positions
                    var offset = {left: diff2.left - diff1.left, top: diff2.top - diff1.top};
                    return offset;
                }
                

                这篇关于改变宽度/高度移动旋转的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 CSS3 旋转背景图像 下一篇:将 css3 旋转到 Chrome 中的 DIV 然后背景附件:修复创

                相关文章

                最新文章

                  <bdo id='jPcWU'></bdo><ul id='jPcWU'></ul>

                <small id='jPcWU'></small><noframes id='jPcWU'>

              • <tfoot id='jPcWU'></tfoot>
              • <legend id='jPcWU'><style id='jPcWU'><dir id='jPcWU'><q id='jPcWU'></q></dir></style></legend>

                  <i id='jPcWU'><tr id='jPcWU'><dt id='jPcWU'><q id='jPcWU'><span id='jPcWU'><b id='jPcWU'><form id='jPcWU'><ins id='jPcWU'></ins><ul id='jPcWU'></ul><sub id='jPcWU'></sub></form><legend id='jPcWU'></legend><bdo id='jPcWU'><pre id='jPcWU'><center id='jPcWU'></center></pre></bdo></b><th id='jPcWU'></th></span></q></dt></tr></i><div id='jPcWU'><tfoot id='jPcWU'></tfoot><dl id='jPcWU'><fieldset id='jPcWU'></fieldset></dl></div>