英文:
display a modal from right to left and content inside another div
问题
我想把我的模态框包含在容器中(红色区域)。
当我将位置设置为相对时,模态框从左侧打开,而我希望它从右侧打开
而且模态框在按钮之后,但我希望它覆盖红色区域中的所有内容。
#container{ background-color:red; width: 100%; height: 300px }
.modal {
display: block;
position: absolute;
z-index: 99;
right: 0; top: 0;
width: 100%; height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
animation: slide-in 1s linear 1;
}
@keyframes slide-in { 0% { width: 0; } 25% { width: 25%; } 50% { width: 50%; } 75% { width: 75%; } 100% { width: 100%; }
}
.modal-content { background-color: #fefefe; margin: 20px; padding: 20px; border: 1px solid #888;
}
<div id=container>
<button id="open-modal-btn">Open modal</button>
</br>
<div id="my-modal" class="modal">
<div class="modal-content">
<h2>title</h2>
<p>content</p>
</div>
</div>
</div>
以上是你提供的代码的翻译。
英文:
I would like to make my modal contained in the container (the red area).
When I set position to relative, the modal opens from left to right whereas I want it to open from right to left
And the modal goes after the button when I want it to go over everything in the red area.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#container{ background-color:red; width: 100%; height: 300px }
.modal {
display: block;
position: absolute;
z-index: 99;
right: 0; top: 0;
width: 100%; height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
animation: slide-in 1s linear 1;
}
@keyframes slide-in { 0% { width: 0; } 25% { width: 25%; } 50% { width: 50%; } 75% { width: 75%; } 100% { width: 100%; }
}
.modal-content { background-color: #fefefe; margin: 20px; padding: 20px; border: 1px solid #888;
}
<!-- language: lang-html -->
<div id=container>
<button id="open-modal-btn">Open modal</button>
</br>
<div id="my-modal" class="modal">
<div class="modal-content">
<h2>title</h2>
<p>content</p>
</div>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
UPDATE: 对不起,我误读了您原来的帖子。无论如何,下面的技术仍然是首选的;只需交换值。
要使您当前的代码从左边滑入,只需将模态框类中的 right
更改为 left
。
.modal {
/* 将 right 更改为 left */
/* right: 0; */
left: 0;
}
但是,您的滑入技巧有点别扭。您通过增加模态框的宽度来实现滑入,从语义上来说,这是一种“增长式”的效果。您很幸运,您的内容没有扭曲,并且看起来像是“滑入”。要实现更加优雅的滑入效果,可以在模态框上使用 transform: translate(0,0)
。
相反,我会这样做,大致如下:
.modal {
/* ... */
/* 为 transform 等属性的更改提供动画效果 */
transition: all 0.5s ease;
/* 默认情况下在屏幕左侧 */
transform: translate(-100%, 0);
/* 默认情况下在屏幕右侧 */
/* transform: translate(100%, 0); */
}
.modalOpened .modal {
/* 设置为零(在视口的中心) */
transform: translate(0, 0);
}
此外,在模态框打开事件(按钮点击)中,只需在应用程序容器上切换一个 modalOpened
类。无需使用关键帧动画。只需自然而实际的滑入效果。
英文:
UPDATE: Sorry I mis-read your original post. In any case, the technique below is still preferred; just swap values.
To get a from-left slide with your current code as it exists, change right
to left
in your modal class.
.modal {
/* Change right to left */
/* right: 0; */
left: 0;
}
However your slide-in technique is awkward. You're achieving a slide by growing the width of the modal, so semantically this is a "grow-in". You're lucky that your content is not warping, and that it happens to appear as a "slide-in". A more elegant way to actually slide-in would be to use transform: translate(0,0)
on the modal.
Instead I'd do this, roughly:
.modal {
/* ... */
/* Gives motion to changed properties like transform */
transition: all 0.5s ease;
/* Default off-screen left */
transform: translate(-100%,0);
/* Default off-screen right */
/* transform: translate(100%,0); */
}
.modalOpened .modal {
/* Set back to zero (center of viewport) */
transform: translate(0,0);
}
In addition, in your modal open event (the button click) simply toggle a class modalOpened
on the app container. No keyframes needed. Just a natural, and actual, slide-in.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论