【发布时间】:2021-10-01 23:56:54
【问题描述】:
我正在尝试制作一个在满足条件时显示在屏幕上的模型(在我的例子中是一个广告拦截器)。该模型大部分有效。
我无法弄清楚或解决的两件事是......
- 当该框首次出现在页面上时,所有框都在其下方排列(运行代码或查看下图以了解我的意思),但是,在选择选项卡后问题得到解决。
- 如果页面上有额外的文本或内容,框会被推到所有内容的下方。尽管设置了 z-index,但它并没有像我想要的那样显示在所有其他内容的前面(我认为这与 JS 调用函数的方式有关,详见图片)。
*** 请注意,以下代码需要打开广告拦截器才能工作 ***
// Get the modal
var modal = document.getElementById("myModal");
window.onload = function() { if(typeof(window.google_render_ad)=="undefined"){
modal.style.display = "block";
}
}
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box}
/* Modal Content */
#myModal {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Style the tab */
.tab {
float: left;
border-top: 2px solid #ccc;
background-color: #f1f1f1;
width: 30%;
height: 300px;
}
/* Style the buttons that are used to open the tab content */
.tab button {
display: block;
background-color: inherit;
color: black;
padding: 22px 16px;
width: 100%;
border: none;
outline: none;
text-align: left;
cursor: pointer;
transition: 0.3s;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
border-radius: 5px;
}
/* Create an active/current "tab button" class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
float: left;
padding: 0px 12px;
border-top: 1px solid #ccc;
width: 70%;
height: 300px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- The Modal -->
<div id="myModal" class="modal">
<h2>Vertical Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>
<!-- Modal content -->
<p>Some text in the Modal..</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'one')" id="defaultOpen">number 1</button>
<button class="tablinks" onclick="openCity(event, 'two')">Number 2</button>
<button class="tablinks" onclick="openCity(event, 'three')">number 3</button>
<button class="tablinks" onclick="openCity(event, 'four')">number 4</button>
<button class="tablinks" onclick="openCity(event, 'five')">number 5</button>
<button class="tablinks" onclick="openCity(event, 'six')">number 6</button>
<button class="tablinks" onclick="openCity(event, 'seven')">number 7</button>
<button class="tablinks" onclick="openCity(event, 'eight')">number 8</button>
</div>
<div id="one" class="tabcontent">
<h3>one</h3>
<p>London is the capital city of England.</p>
</div>
<div id="two" class="tabcontent">
<h3>two</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="three" class="tabcontent">
<h3>three</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="four" class="tabcontent">
<h3>four</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="five" class="tabcontent">
<h3>five</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="six" class="tabcontent">
<h3>six</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="seven" class="tabcontent">
<h3>seven</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="eight" class="tabcontent">
<h3>eight</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
</div>
</body>
</html>
图片:
页面加载
第一次点击标签后
与页面上的其他内容
编辑:
一个答案后,原来的一切都是固定的!现在盒子没有居中。有没有什么办法让弹窗居中,并在其后面放置一个浅灰色(所以原来的页面内容更多在后台)?
这是我现在的代码:
var modal = document.getElementById("myModal");
window.onload = function() {
if (typeof(window.google_render_ad) == "undefined") {
modal.style.display = "block";
document.getElementById('defaultOpen')?.click();
}
}
function openCity(evt, cityName) {
document.querySelector('.tabcontent.open')?.classList.remove('open');
document.getElementById(cityName).classList.add('open')
document.querySelector('.tablinks.active')?.classList.remove('active');
evt.currentTarget.classList.add('active');
}
body {
font-family: Arial, Helvetica, sans-serif;
}
* {
box-sizing: border-box
}
/* Modal Content */
#myModal {
position: absolute;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
height:auto;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {
padding: 2px 16px;
}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Style the tab */
.tab {
float: left;
border-top: 2px solid #ccc;
background-color: #f1f1f1;
width: 30%;
height: 300px;
}
/* Style the buttons that are used to open the tab content */
.tab button {
display: block;
background-color: inherit;
color: black;
padding: 22px 16px;
width: 100%;
border: none;
outline: none;
text-align: left;
cursor: pointer;
transition: 0.3s;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
border-radius: 5px;
}
/* Create an active/current "tab button" class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
float: left;
padding: 0px 12px;
border-top: 1px solid #ccc;
width: 70%;
height: 300px;
display: none; /* this hides all tabs */
}
.tabcontent.open {
display: block; /* show current tab */
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>dtnhdrtytntnntfyndtmntfnytnrtbdrtbAnimated Modal with Header and Footer</h2>
<h2>tdrnbtrrdtrdtbtbdtdnbdrntdAnimated Modal with Header and Footer</h2>
<h2>tndrntbtdbtrdtdrbfntyftnrgbAnimated Modal with Header and Footer</h2>
<h2>tbtryfbdtbyrdbfnyfnynydbrdbAnimated Modal with Header and Footer</h2>
<h2>dtnhdrtytntnntfyndtmntfnytnrtbdrtbAnimated Modal with Header and Footer</h2>
<h2>tdrnbtrrdtrdtbtbdtdnbdrntdAnimated Modal with Header and Footer</h2>
<h2>tndrntbtdbtrdtdrbfntyftnrgbAnimated Modal with Header and Footer</h2>
<h2>tbtryfbdtbyrdbfnyfnynydbrdbAnimated Modal with Header and Footer</h2>
<h2>dtnhdrtytntnntfyndtmntfnytnrtbdrtbAnimated Modal with Header and Footer</h2>
<h2>tdrnbtrrdtrdtbtbdtdnbdrntdAnimated Modal with Header and Footer</h2>
<h2>tndrntbtdbtrdtdrbfntyftnrgbAnimated Modal with Header and Footer</h2>
<h2>tbtryfbdtbyrdbfnyfnynydbrdbAnimated Modal with Header and Footer</h2>
<h2>dtnhdrtytntnntfyndtmntfnytnrtbdrtbAnimated Modal with Header and Footer</h2>
<h2>tdrnbtrrdtrdtbtbdtdnbdrntdAnimated Modal with Header and Footer</h2>
<h2>tndrntbtdbtrdtdrbfntyftnrgbAnimated Modal with Header and Footer</h2>
<h2>tbtryfbdtbyrdbfnyfnynydbrdbAnimated Modal with Header and Footer</h2>
<!-- The Modal -->
<div id="myModal" class="modal">
<h2>Vertical Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>
<!-- Modal content -->
<p>Some text in the Modal..</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'one')" id="defaultOpen">number 1</button>
<button class="tablinks" onclick="openCity(event, 'two')">Number 2</button>
<button class="tablinks" onclick="openCity(event, 'three')">number 3</button>
<button class="tablinks" onclick="openCity(event, 'four')">number 4</button>
<button class="tablinks" onclick="openCity(event, 'five')">number 5</button>
<button class="tablinks" onclick="openCity(event, 'six')">number 6</button>
<button class="tablinks" onclick="openCity(event, 'seven')">number 7</button>
<button class="tablinks" onclick="openCity(event, 'eight')">number 8</button>
</div>
<div id="one" class="tabcontent">
<h3>one</h3>
<p>London is the capital city of England.</p>
</div>
<div id="two" class="tabcontent">
<h3>two</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="three" class="tabcontent">
<h3>three</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="four" class="tabcontent">
<h3>four</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="five" class="tabcontent">
<h3>five</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="six" class="tabcontent">
<h3>six</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="seven" class="tabcontent">
<h3>seven</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="eight" class="tabcontent">
<h3>eight</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
</div>
</body>
</html>
【问题讨论】:
标签: javascript html css popup