简体   繁体   English

使用CSS将div从左上角移动到右下角

[英]Moving an div from the top left corner to bottom right using css

I am trying to move a div from top left corner to bottom right corner using only CSS (this is required for my assignment). 我正在尝试仅使用CSS将div从左上角移动到右下角(这是我的分配要求)。 Also, I need to see the transition happening (the div sliding to the bottom). 另外,我需要查看过渡的发生(div滑到底部)。 I tried using transform:translate() but i can't get my div to go to the bottom corner! 我试过使用transform:translate()但我无法将div移至底角! Here is what I've done until now: http://codepen.io/ascii-hero/pen/JXEXVB 这是我到目前为止所做的: http : //codepen.io/ascii-hero/pen/JXEXVB

Tldr; Tldr;

Solution 1: Using Left/Right/Top/Bottom positioning 解决方案1:使用左/右/上/下定位

In order for your div to move, you will need to set a parent element to relative , and the div to absolute positioning. 为了使div移动,您需要将一个父元素设置为relative ,并将div设置为absolute定位。 Note, since the html element is the 'top most' element of the html tree, it is automatically assumed this relative position. 请注意,由于html元素是html树的“最高”元素,因此会自动采用此relative位置。

 div { background: red; height: 50px; width: 50px; position: absolute; top: 0; left: 0; } html:hover div { top: auto; left: auto; bottom: 0; right: 0; } 
 <div></div> 


Solution 2: Transforms 解决方案2:转换

Using transforms is great, as you can also add transitions for a smooth effect . 使用转换非常棒,因为您还可以添加转换以获得平滑效果 Just note you'll need to add just a slight alteration to solution 1. 请注意,您只需要对解决方案1进行一点改动即可。

 div { background: red; height: 50px; width: 50px; position: absolute; top: 0; left: 0; transition: all 0.4s; } html:hover div { top: 100%; left: 100%; transform: translate(-100%, -100%); } 
 <div></div> 

Explanation of Solution 2 解决方案2的说明

To allow for transforms, the DOM needs to know the start point, the end point, and the duration explicitly . 为了进行转换,DOM需要明确知道起点,终点和持续时间。 So hence, the start is set to 因此,开始设置为

top:0; left:0;

To represent the top and left vales. 代表顶部和左侧。

The 'duration' can be set using the transition property. 可以使用transition属性设置“持续时间”。 Here I have set this to 0.4s(econds), but this can be altered to any suitable value. 在这里,我将其设置为0.4s(秒),但是可以将其更改为任何合适的值。

Lastly, and most crucially, you need to set a definitive end point to your transform. 最后,也是最关键的一点,您需要为转换设置一个明确的终点。 Here you will notice the 在这里,您会注意到

top:100%;left:100%; 

However, as I am sure you are aware in doing that it will put the very top left corner at this position, (100%,100%) so to speak. 但是,正如我确定您知道的那样,可以将它的左上角放在该位置(100%,100%)。 It is hence the reason for the inclusion for the translate to 'move the box back onto the screen'. 因此, translate的原因是“将盒子移回屏幕”。 The translate property takes two values, the X and Y disposition. 平移属性采用两个值,即X和Y处置。 By using a % as the unit, it will move a % of the size of either the width or height of the box, depending on the axis you are moving it. 通过使用%作为单位,它会移动框的宽度或高度的大小的%,具体取决于您移动框的轴。 In other words, using 换句话说,使用

transform:translate(-100%, -100%);

will move the box 100% (of itself) to the left, and 100% (of its height) up, hence it can be seen in the bottom right of the page. 会将框(本身)向左移动100%,并将框(高度向左)移动100%,因此可以在页面的右下角看到它。

try this 尝试这个

#block:hover {
  left: 100%;
  top: 100%;
    -webkit-transition-property: left, top, background, -webkit-transform;
    -webkit-transition-duration: 2s, 2s, 1s, 1s;
    transform:translate(-100%, -100%);
  }

I'm on mobile so I hav t tested it but it /should/ work 我在移动设备上,因此我尚未对其进行测试,但它应该/可以工作

So you want to move the div to bottom right corner. 因此,您要将div移到右下角。 In your code you are doing 在您的代码中,您正在做

#block {
 background: cornflowerblue;
 width: 100px;
 height: 100px;
 left: 0;
 top: 0;
}

so instead of top to be 0, you want bottom to be 0, and instead of left you want right. 因此,您希望bottom为0,而不是top为0,而您希望right而不是left。

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

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