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

2023-05-14前端开发问题
10

本文介绍了改变宽度/高度移动旋转的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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;
}

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

The End

相关推荐

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

ExecJS::ProgramError: SyntaxError: 保留字“function"
ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)...
2024-04-20 前端开发问题
13

无限滚动和 will_paginate 多次附加项目的“下一页"
Infinite scroll and will_paginate appending the #39;next page#39; of items multiple times(无限滚动和 will_paginate 多次附加项目的“下一页)...
2024-04-20 前端开发问题
8