简体   繁体   English

如何将旋转的元素定位到右边缘

[英]How to position rotated element to the right edge

I am trying to align an absolute element (image in this case) to the right edge of the container. 我正在尝试将绝对元素(在这种情况下为图像)对齐到容器的右边缘。

It works if the element is not rotated, but when a transformation is involved, the left property is not calculated correctly. 如果不旋转元素,则它起作用,但是当涉及变换时,left属性的计算不正确。

Maybe I am missing something, but the solution I am using right now is getBoundingClientRect() to get the width and then subtract it from the container width. 也许我缺少了一些东西,但是我现在使用的解决方案是getBoundingClientRect()以获取宽度,然后从容器宽度中减去它。

Here is a JSFiddle that demonstrate what I am doing. 这是一个演示我在做什么的JSFiddle

getBoundingClientRect is a good approach, the problem is that when you set css left, it positions it without the rotation calculated. getBoundingClientRect是一种很好的方法,问题是当您将CSS设置为左时,它将在不计算旋转度的情况下定位它。 The order in which you set it doesn't change the fact the the rotation is applied in relation to the css, not in relation to the current position of the rotated div. 设置顺序不会改变相对于css而不是相对于旋转后的div的当前位置应用旋转的事实。 So when you calculate dimensions using getBoundingClientRect you're taking into account the rotation, then you use it on a css that doesn't take it into account. 因此,当您使用getBoundingClientRect计算尺寸时,要考虑到旋转,然后在不考虑旋转的CSS上使用它。

One easy way to get proper coordinates, would be to calculate the x difference between before rotation and after and adjust you left accordingly. 一种获得正确坐标的简单方法是计算旋转前后的x差,并相应地调整左移。 You'll have prevDimension.x - dimension.x giving you the difference in x that the rotation is creating, which allows you to adjust newLeft . 您将拥有prevDimension.x - dimension.x ,以提供旋转所创建的x的差异,从而可以调整newLeft

Like this: 像这样:

$('#rotate-align').click(function () {
                var prevDimensions = $('.element')[0].getBoundingClientRect();
        $('.element').css('transform', 'rotate(0.99923rad)');
        var dimensions = $('.element')[0].getBoundingClientRect();
        var newLeft = $('#bounds').width() - dimensions.width - dimensions.x + prevDimensions.x;
        $('.element').css('left', newLeft);
});

http://jsfiddle.net/jgcynwmp/3/ http://jsfiddle.net/jgcynwmp/3/

Another approach would be to calculate the x difference based on the width difference between the non rotated element and the rotated element. 另一种方法是基于未旋转元素和旋转元素之间的宽度差来计算x差。 This can be done using offsetWidth (which doesn't take the rotation into account) and the getBoundingClientRect . 可以使用offsetWidth (不考虑旋转)和getBoundingClientRect来完成 The difference between the 2 will tell you how much width is lost with the rotation. 2之间的差异将告诉您旋转损失了多少宽度。 Note that for this calculation, the transform origin is important. 请注意,对于此计算,变换原点很重要。 For example, with a centered rotation, you'll need to divide by 2 the width difference to get the x difference, but with another origin it would be something else. 例如,在旋转居中的情况下,您需要将宽度差除以2才能获得x差,但是在另一个原点处则需要其他操作。

Like this: 像这样:

$('#rotate-align').click(function () {
        $('.element').css('transform', 'rotate(0.99923rad)');
        var dimensions = $('.element')[0].getBoundingClientRect();
        var newLeft = $('#bounds').width() - $('.element')[0].offsetWidth + (($('.element')[0].offsetWidth - dimensions.width) / 2);
        $('.element').css('left', newLeft);
});

http://jsfiddle.net/jgcynwmp/4/ http://jsfiddle.net/jgcynwmp/4/

There is a JSFiddle here . 有一个的jsfiddle 这里

When the image is rotated, the bounding rectangle remains in the place of the rotation, instead of being to the transformed coordinates. 旋转图像时,边界矩形将保留在旋转位置,而不是转换后的坐标。

I added a 'bcr' <div> element which then is matched to the bounding client rectangle. 我添加了一个'bcr' <div>元素,然后该元素与边界客户端矩形匹配。

After the rotation we can move the image into place (477 is the absolute right of bounds ). 旋转之后,我们可以将图像移到适当位置(477是bounds的绝对右边)。

There appears to be a small problem if you repeatedly click the button, but I guess that's the magic of CSS transforms! 如果您反复单击该按钮,似乎会有一个小问题,但是我想这就是CSS转换的神奇之处!

 $('#align').click(function () { var newLeft = $('#bounds').width() - $('.element').outerWidth(); $('.element').css('left', newLeft); }); $('#rotate-align').click(function () { $('.element').css('transform', 'rotate(0.69923rad)'); var dimensions = $('.element')[0].getBoundingClientRect(); $('.element').css('left',477-dimensions.width-dimensions.left); $('#bcr').css('left',dimensions.left); $('#bcr').css('top',dimensions.top); $('#bcr').css('width',dimensions.width); $('#bcr').css('height',dimensions.height); }); 
 #bounds { width:427px; height:354px; left:50px; top:38px; border: 1px solid red; position: absolute; } #bcr { width:327px; height:254px; left:150px; top:138px; border: 1px solid green; position: absolute; } .element { top: 100px; z-index: 102; line-height: 82px; width: 312px; height: 82px; #transform: rotate(0.99923rad); left: 0; position:absolute; border: 1px solid green; } .element-img { width: 100%!important; height: 100%!important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bounds"> <div class="element"> <img class="element-img" src="https://www.google.com/logos/doodles/2014/2014-winter-olympics-5710368030588928-hp2x.jpg"> </div> </div> <input type="button" id="align" value="Align right" style="width:100%;" /> <input type="button" id="rotate-align" value="Rotate and align right" style="width:100%;" /> <div id="bcr"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM