简体   繁体   English

从100%过渡到自动

[英]Transition from 100% to auto

I have the following: http://jsfiddle.net/yHPTv/2491/ 我有以下内容: http : //jsfiddle.net/yHPTv/2491/

I was wondering why the transition isn't working? 我想知道为什么过渡无效? What it's supposed to do is slide in the hidden element (which can be of variable width) to the right edge of the .block element, however, it just pops in. 它应该做的是将隐藏元素(可以是可变宽度)滑动到.block元素的右边缘,但是,它只是弹出。

 .block { position: relative; width: 500px; height: 300px; overflow: hidden; background: lightgrey; } .block .hidden { background: red; padding: 3px 10px; position: absolute; bottom: 0; left: 100%; transition: 1s; } .block:hover .hidden { transition: 1s; left: auto; right: 0; } 
 <div class="block"> <div class="hidden">ABCDEFGHIJKL</div> </div> 

I think it has something to do with left: auto because if I change it left: 50% , it works, but not in the way I need it to. 我认为这与left: auto因为如果我将其改为left: 50% ,它有效,但不是我需要的方式。

Thanks. 谢谢。

As you say you can't animate from % to auto . 正如你所说,你无法从%动画到auto But to get the desire effect you can also use transform property. 但是要获得欲望效果,您还可以使用transform属性。 Try this: 尝试这个:

.block .hidden {
    background: red;
    padding: 3px 10px;
    position: absolute;
    bottom: 0;
    right: 0;
    transform:translateX(100%);
    transition: 1s;
}

.block:hover .hidden {
    transition: 1s;
    transform:translateX(0)
}

Check the Demo Fiddle 检查演示小提琴

Consider transitioning on right , from -100% to 0 : 考虑从right转换,从-100%0

 .block { position: relative; width: 500px; height: 150px; /* shortened to fit in the "Run" window */ overflow: hidden; background: lightgrey; } .block .hidden { background: red; padding: 3px 10px; position: absolute; bottom: 0; right: -100%; transition: 1s; } .block:hover .hidden { right: 0; transition: 1s; } 
 <div class="block"> <div class="hidden">ABCDEFGHIJKL</div> </div> 

For transition to work, you have to define the property you wish to change in both element states. 要转换为工作,您必须在两个元素状态中定义要更改的属性。 In your example it doesn't work because there is no common property between '.hidden' and ':hover' (you set the ' left ' property in '.hidden', and ' right ' property in ':hover') 在你的例子中它不起作用,因为'。hidden'和':hover'之间没有共同属性(你在'.hidden'中设置' left '属性,在':hover'中设置' right '属性)

If you instead use something like: 如果您改为使用以下内容:

.block {
    position: relative;
    width: 500px;
    height: 300px;
    overflow: hidden;
    background: lightgrey;
}

.block .hidden {
    background: red;
    padding: 3px 10px;
    position: absolute;
    bottom: 0;
    right: -100%;
    transition: 1s;
}

.block:hover .hidden {
    transition: 1s;
    right: 0%;
}

It will work because we defined the ' right ' property in both states ('.hidden' and ':hover') 之所以会起作用,是因为我们在两个州(“ .hidden”和“:hover”)都定义了“ right ”属性

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

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