CSS过渡是否适用于定位元素?

时间:2015-01-08 23:52:12

标签: css css3 css-transitions

这一次,我将包含代码(正如我之前被问到的那样)。 问题是,即使这个代码完成了我所需要的(CSS模态/灯箱窗口),我也无法让它为top和translate属性设置动画。 我认为这是一个定位问题,因为原始的例子没有使用容器的位置,而是使用了:before伪元素(我不喜欢它)。 这段代码出了什么问题。

<!DOCTYPE html>
<html>
<head>
    <meta charset="iso-8859-1" />
    <title>Pure CSS Modal Window</title>
    <style>
        *
            {
            margin: 0px;
            padding: 0px;
            }
        .modal-container
            {
            position: fixed;
            display: none;
            top: 0px;
            left: 0px;
            right: 0px;
            bottom: 0px;
            background-color: rgba(0, 0, 0, 0.75);
            }
        .modal-content
            {
            position: absolute;
            top: -100%;
            left: 50%;
            width: 70%;
            max-width: 400px;
            max-height: 400px;
            transform: translateX(-50%);
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            overflow-y: auto;
            transition: all 5s;
            }
        .modal-container:target
            {
            display: block;
            }
        .modal-container:target .modal-content
            {
            top: 50%;
            transform: translate(-50%, -50%);
            }
    </style>
</head>
<body>
    <a href="#modal1">Modal 1</a>
    <a href="#modal2">Modal 2</a>
    <div class="modal-container" id="modal1">
        <div class="modal-content">
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <p>This is the content of Modal 1</p>
            <a href="#modal-close">Close</a>
        </div>
    </div>
    <div class="modal-container" id="modal2">
        <div class="modal-content">
            <p>This is the content of Modal 2</p>
            <a href="#modal-close">Close</a>
        </div>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

这是因为display状态从display: none更改为display: block,这意味着之前没有布局可以进行转换。

一种选择是将display: none替换为visibility: hidden,将display: block替换为visibility: visible

工作示例:

*
    {
    margin: 0px;
    padding: 0px;
    }
.modal-container
    {
    position: fixed;
    visibility: hidden;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    background-color: rgba(0, 0, 0, 0.75);
    }
.modal-content
    {
    position: absolute;
    top: -100%;
    left: 50%;
    width: 70%;
    max-width: 400px;
    max-height: 400px;
    transform: translateX(-50%);
    background-color: #fff;
    padding: 20px;
    border-radius: 10px;
    overflow-y: auto;
    transition: all 5s;
    }
.modal-container:target
    {
    visibility: visible;
    }
.modal-container:target .modal-content
    {
    top: 50%;
    transform: translate(-50%, -50%);
    }
<a href="#modal1">Modal 1</a>
<a href="#modal2">Modal 2</a>
<div class="modal-container" id="modal1">
    <div class="modal-content">
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <p>This is the content of Modal 1</p>
        <a href="#modal-close">Close</a>
    </div>
</div>
<div class="modal-container" id="modal2">
    <div class="modal-content">
        <p>This is the content of Modal 2</p>
        <a href="#modal-close">Close</a>
    </div>
</div>

答案 1 :(得分:0)

Alexander O&#;; Mara提供的解决方案效果很好(并且还教了我一点点),但我找到了另一个答案。 我解决了它只是分开了DIV&s。我的意思是,而不是:

<div class="container">
<div class="content">
    <p>Content here</p>
</div>
</div>

我用过:

<div class="container">
</div>
<div class="content">
    <p>Content here</p>
</div>

对于我使用的CSS:

.container:target + .content {}

而不是

.container:target .content {}

这一切都没有改变&#34;显示:无&#34;对于&#34;可见性:隐藏&#34;,正如亚历山大建议的那样,&amp;它工作得很好。 谢谢你的帮助。很高兴看到世界各地的人们担心其他问题和其他问题。试着解决它们。

干杯。 马塞洛。