使用Flexbox使弹出窗口完全响应
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Flexbox使弹出窗口完全响应相关的知识,希望对你有一定的参考价值。
我创建了一个模态/弹出窗口,其中包含像https://www.labtag.ca/这样的图片和文本。它应该在所有设备上完全响应(移动设备和Ipads的垂直布局)。它适用于大型设备,并且在模拟Chrome中的所有标准设备尺寸时,但是当调整浏览器大小并检查它时看起来无序,同时在一定宽度上从大到小以及在移动设备中实时地进行检查时,有一个滚动时添加的不必要的高度。我使用flex-direction:用于大型设备和小型设备的行,我使用flex-direction:column。我使用了大量的媒体查询(不是一个好主意)来确保填充(所有方向)都很好。有关如何解决这些问题的任何见解,并使用较少的媒体查询执行此操作。这是我的html: -
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div class="content-img-wrapper">
<div class="img"><img src="https://cdn2.bigcommerce.com/server3300/p3n7fuwd/product_images/uploaded_images/computer.png" alt="ga-popup-img" /></div>
<div class="content">
<p>You're missing out <span id= "ga-icons">■ ■ ■</span></p>
<ul>
<li><i class="arrow right"></i>Access the latest products</li>
<li><i class="arrow right"></i>Get discounts</li>
<li><i class="arrow right"></i>Checkout in $CAD</li>
<li><i class="arrow right"></i>Live Chat</li>
</ul>
<a href="https://www.labtag.com/" class="btn-ga">
Take me to the new website <i class="arrow-button"></i></a>
</div>
</div>
</div>
</div>
这是我的css: -
/*start of ga popup*/
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 2; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: hidden; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
i.right{ /*Style for the arrows*/
border: solid #fc7a22;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
transform: translate(0%,-40%) rotate(-45deg);
margin-right: 10px;
}
i.arrow-button{ /*Style for the arrows*/
border: solid #fff;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
transform: rotate(-45deg);
margin-right: 10px;
}
/*Making the image responsive*/
.content-img-wrapper .img img{
width:100%;
height:auto;
}
/*button inside the modal */
a.btn-ga {
color: #fff;
background-color: #fc7a22;
box-shadow: 0 2px 10px 0 rgba(0,0,0,.1);
transition: all .4s ease;
margin-right: auto;
margin-left: auto;
position: relative;
z-index: 2;
display: inline-block;
font-weight: 400;
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 14.7rem;
/* height: 4.8rem; */
border-radius: .5rem;
font-family: Amsi Pro Cond Bold;
font-size: 1rem;
/*letter-spacing: .07rem;*/
line-height: 2rem;
text-decoration:none;
transform: translate(0%,140%);
/* padding-top: 1.3rem; */
/* border: 0; */
}
/*styling the content */
.content-img-wrapper{
display: flex;
flex-flow:row wrap;
}
.content-img-wrapper .content p {
/* justify-content: space-around; */
font-size: 45px;
font-family:'Atlas Grotesk Bold';
font-weight:bold;
color: #351f65;
/*transform: translateY(-120%);*/
}
.content-img-wrapper .content ul {
list-style-type: none;
border: unset;
}
.content-img-wrapper .content ul li {
/* justify-content: space-around; */
font-size: 33.5px;
color: #351f65;
font-family:'Atlas Grotesk Regular';
/*transform: translate(-10%,-80%);*/
margin-bottom: 10px;
border-bottom: unset;
}
.content-img-wrapper .content ul li:before {
/* content: "• ";
color: #53c5e9; */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
border-radius: 6px;
border: 1px solid #888;
width: 70%;
height: 70%;
transform: translate(0%, 8%);
}
.content-img-wrapper{
/*padding-top: 80px;*/
}
.content-img-wrapper .img {
width: unset;
height: unset;
padding: 20px
}
.content-img-wrapper.content{
padding: 20px
}
a.btn-ga:hover{
color:#fff !important;
}
/* The Close Button */
.close {
/*color: #aaaaaa;*/
color:#c2c2c2;
float: right;
font-size: 28px;
font-weight: bold;
transform: translate(-40%,40%);
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
/*unicode icons*/
#ga-icons{
font-size:10px;
display:inline-block;
}
/*For phones till ipad pro*/
@media screen and (max-width: 1024px) {
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
border-radius: 5px;
border: 1px solid #888;
width: 100%;
height: 80%;
}
.content-img-wrapper .img img{
/*width:90%;*/
margin-left: 8px;
}
}
/* For just phones*/
@media screen and (max-width: 767px) {
.content-img-wrapper{
flex-direction: column;
}
.content-img-wrapper .content p {
/* justify-content: space-around; */
font-size: 25px;
transform:unset;
}
.content-img-wrapper .content ul li {
font-size: 15px;
transform: translate(-10%,100%);
}
.content {
transform: translate(15%);
}
.modal-content {
width:90%;
min-height: 90%;
transform:unset;
}
i.right{
transform: scale(0.7)translate(0%,-20%) rotate(-45deg);
}
}
@media screen and (min-width: 320px) and (max-width: 360px) {
.modal{
padding-top:unset;
}
.modal-content {
height: 82%;
}
.content-img-wrapper .content p {
font-size: 22px;
}
}
@media screen
and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3) {
.modal-content {
width: 90%;
height: 64%;
}
}
@media screen
and (device-width: 411px)
and (device-height: 823px)
{
.modal-content {
width: 90%;
height: 66%;
}
}
@media screen
and (device-width: 320px)
and (device-height: 568px)
{
.content {
transform: translate(11%);
}
}
/*Ipad and Ipad pro specific styles*/
@media screen
and (min-width: 768px)
and (max-width: 1279px)
{
.content {
transform: translate(30%);
}
.content-img-wrapper .content ul li {
transform: translate(-2%,132%);
margin-bottom: 20px;
}
.content-img-wrapper {
padding-top: 20px;
flex-direction: column;
flex-wrap:unset;
}
.content-img-wrapper .img img {
width: 90%;
height: auto;
margin-left: 60px;
}
a.btn-ga {
transform: translate(0%,80%);
font-size: 2rem;
line-height: 3rem;
width: 29rem;
height: 3rem;
}
i.arrow-button {
transform: scale(1.5)translateY(-25%) rotate(-45deg);
margin-right: 10px;
}
}
/*For laptops and bigger*/
@media screen and (min-width: 1280px) {
.content-img-wrapper{
justify-content: space-around;
transform: translateY(30%);
}
.content-img-wrapper .content {
padding-top: 30px;
}
.modal-content {
width: 1250px;
}
.content-img-wrapper .content ul li{
transform: translate(-4%,200%);
padding-bottom: 10px;
}
a.btn-ga{
transform: translate(0%,200%);
width:483px;
font-size: 2rem;
line-height: 4rem;
height:58px;
}
i.arrow-button {
border: solid #fff;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
transform: scale(1.5) translateY(-33%) rotate(-45deg);
margin-right: 10px;
}
#ga-icons {
font-size: 20px;
}
}
@media screen and (min-width: 1280px) and (max-height: 800px){
.content-img-wrapper {
transform: translateY(20%);
}
}
/*end of ga popup*/
这是我的js: -
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
jQuery(document).ready( function($) {
if(window.location.href !='https://www.labtag.ca/'){
return;
}
modal.style.display = "block";
$('#Header,#TopMenu').addClass('blur-in');
});
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
$('#Header,#TopMenu').removeClass('blur-in');
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
$('#Header,#TopMenu').removeClass('blur-in');
}
}
</script>
答案
正如另一位用户指出的那样,您似乎缺少媒体查询。这些有助于为特定断点定义样式,从而有助于定义各种屏幕尺寸。更好,更彻底的解释可以是found here。
在得到一般性的理解后,我会跳到标题为“典型设备断点”的部分,以获得您想要的位置。
另一答案
对不起,这是一个冗长的CSS,在上面的CSS中,这里是媒体查询: -
/*For phones till ipad pro*/
@media screen and (max-width: 1024px) {
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
border-radius: 5px;
border: 1px solid #888;
width: 100%;
height: 80%;
}
.content-img-wrapper .img img{
/*width:90%;*/
margin-left: 8px;
}
}
/* For just phones*/
@media screen and (max-width: 767px) {
.content-img-wrapper{
flex-direction: column;
}
.content-img-wrapper .content p {
/* justify-content: space-around; */
font-size: 25px;
transform:unset;
}
.content-img-wrapper .content ul li {
font-size: 15px;
transform: translate(-10%,100%);
}
.content {
transform: translate(15%);
}
.modal-content {
width:90%;
min-height: 90%;
transform:unset;
}
i.right{
transform: scale(0.7)translate(0%,-20%) rotate(-45deg);
}
}
@media screen and (min-width: 320px) and (max-width: 360px) {
.modal{
padding-top:unset;
}
.modal-content {
height: 82%;
}
.content-img-wrapper .content p {
font-size: 22px;
}
}
@media screen
and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3) {
.modal-content {
width: 90%;
height: 64%;
}
}
@media screen
and (device-width: 411px)
and (device-height: 823px)
{
.modal-content {
width: 90%;
height: 66%;
}
}
@media screen
and (device-width: 320px)
and (device-height: 568px)
{
.content {
transform: translate(11%);
}
}
/*Ipad and Ipad pro specific styles*/
@media screen
and (min-width: 768px)
and (max-width: 1279px)
{
.content {
transform: translate(30%);
}
.content-img-wrapper .content ul li {
transform: translate(-2%,132%);
margin-bottom: 20px;
}
.content-img-wrapper {
padding-top: 20px;
flex-direction: column;
flex-wrap:unset;
}
.content-img-wrapper .img img {
width: 90%;
height: auto;
margin-left: 60px;
}
a.btn-ga {
transform: translate(0%,80%);
font-size: 2rem;
line-height: 3rem;
width: 29rem;
height: 3rem;
}
i.arrow-button {
transform: scale(1.5)translateY(-25%) rotate(-45deg);
margin-right: 10px;
}
}
/*For laptops and bigger*/
@media screen and (min-width: 1280px) {
.content-img-wrapper{
justify-content: space-around;
transform: translateY(30%);
}
.content-img-wrapper .content {
padding-top: 30px;
}
.modal-content {
width: 1250px;
}
.content-img-wrapper .content ul li{
transform: translate(-4%,200%);
padding-bottom: 10px;
}
a.btn-ga{
transform: translate(0%,200%);
width:483px;
font-size: 2rem;
line-height: 4rem;
height:58px;
}
i.arrow-button {
border: solid #fff;
border-width: 0 3px 3px 0;
display: inline-block;
padding: 3px;
transform: scale(1.5) translateY(-33%) rotate(-45deg);
margin-right: 10px;
}
#ga-icons {
font-size: 20px;
}
}
@media screen and (min-width: 1280px) and (max-height: 800px){
.content-img-wrapper {
transform: translateY(20%);
}
}
以上是关于使用Flexbox使弹出窗口完全响应的主要内容,如果未能解决你的问题,请参考以下文章
在Iframe中使用jquery ui dialog 怎么使弹出窗口居于浏览器中间