我想为我的网站添加自动弹出的Lightbox。
我需要将其添加到我的Html页面中。我正在使用WordPress,但我拥有的脚本只允许我插入HTML代码。我无法使用插件。
任何JavaScript都可以。我想发布一张图片,并希望它在页面上停留,直到用户关闭它。但我也希望该图像可点击。
谢谢
我想为我的网站添加自动弹出的Lightbox。
我需要将其添加到我的Html页面中。我正在使用WordPress,但我拥有的脚本只允许我插入HTML代码。我无法使用插件。
任何JavaScript都可以。我想发布一张图片,并希望它在页面上停留,直到用户关闭它。但我也希望该图像可点击。
谢谢
function showPopup() {
document.getElementById('blackOverlay').style.display = 'block';
document.getElementById('popup').style.display = 'block';
}
function closePopup() {
document.getElementById('blackOverlay').style.display = 'none';
document.getElementById('popup').style.display = 'none';
}
.blackOverlay {
display:none;
background:rgba(0,0,0,.6);
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
z-index:1;
}
.popup {
display:none;
background:#fff;
position:fixed;
top:10%;
left:50%;
width:600px;
height:80%;
margin-left:-300px;
z-index:10;
border:2px solid #000;
}
.closePopup {
float:right;
font-weight:bold;
color:red;
cursor:pointer;
}
img {
max-width:100%;
max-height:100%;
}
<body onload="showPopup()">
<div>
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
</div>
<div id="blackOverlay" class="blackOverlay"></div>
<div id="popup" class="popup">
<span class="closePopup" onclick="closePopup()">X</span>
<img src="http://dummyimage.com/600x400/fff/000.png" />
</div>
</body>
这里有更多的示例,下载所需的JS文件并将其添加到您的模板文件中,然后启动JS调用。
function showPopup() {
document.getElementById('blackOverlay').style.display = 'block';
document.getElementById('popup').style.display = 'block';
}
function closePopup() {
document.getElementById('blackOverlay').style.display = 'none';
document.getElementById('popup').style.display = 'none';
}
.blackOverlay {
display:none;
background:rgba(0,0,0,.6);
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
z-index:1;
}
.popup {
display:none;
background:#fff;
position:fixed;
top:10%;
left:50%;
width:600px;
height:80%;
margin-left:-300px;
z-index:10;
border:2px solid #000;
}
.closePopup {
float:right;
font-weight:bold;
color:red;
cursor:pointer;
}
img {
max-width:100%;
max-height:100%;
}
<body onload="showPopup()">
<div>
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
Content behind!Content behind!Content behind!Content behind!Content behind!<br />
</div>
<div id="blackOverlay" class="blackOverlay"></div>
<div id="popup" class="popup">
<span class="closePopup" onclick="closePopup()">X</span>
<img src="http://dummyimage.com/600x400/fff/000.png" />
</div>
</body>