标签:jquery javascript overlay html
我有一个脚本,我需要运行只有一个叠加是不可见的.
所以我使用了以下脚本:
示例如下,按钮显示/隐藏叠加.控制台日志结果
function overlay() {
if( $('div#overlay').is(':visible') ){
console.log("visible");
}
else {
console.log("not visible");
}
};
#overlay {
visibility: hidden;
position: fixed;
left: 0px;
top: 0px;
width:40%;
height: 40%;
text-align:center;
z-index: 1000;
display: inline-block;
background-color: orange;
/*Flexbox*/
display: flex;
align-items: center;
align-content: center;
justify-content: center;
}
form.overlay-form {
width:780px;
}
table.overlay-table {
position: relative;
text-align: center;
}
table.overlay-table tr td {
background: rgb(54, 25, 25);
background: rgba(54, 25, 25, 0);
border-style: none;
margin-right: 40%;
margin-bottom: 30%;
position: relative;
text-align: center;
width: 800px;
}
.button {
z-index:1000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="overlay">
Overlay showing
</div>
</br></br></br></br>
</br></br></br></br>
<input id="clickMe" class="button" type="button" value="clickme" onclick="overlay();" />
EDIT: following correcting . for # due to it being an id not a class. Now, when my overlay is NOT on the screen. It returns
‘visible’.
此脚本始终返回“可见”.救命!!
谢谢
解决方法:
需要使用if($(‘div#overlay’).is(‘:visible’)){因为overlay是id而不是class: –
if( $('div#overlay').is(':visible')){
console.log("visible");
}else {
console.log("not visible");
}
如果(el.style.visibility ==’visible’)使用当前问题,请使用{如下: –
function overlay() {
el = document.getElementById("overlay");
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
if( el.style.visibility =='visible' ){
console.log("visible");
}
else {
console.log("not visible");
}
};
#overlay {
visibility: hidden;
position: fixed;
left: 0px;
top: 0px;
width:40%;
height: 40%;
text-align:center;
z-index: 1000;
display: inline-block;
background-color: orange;
/*Flexbox*/
display: flex;
align-items: center;
align-content: center;
justify-content: center;
}
form.overlay-form {
width:780px;
}
table.overlay-table {
position: relative;
text-align: center;
}
table.overlay-table tr td {
background: rgb(54, 25, 25);
background: rgba(54, 25, 25, 0);
border-style: none;
margin-right: 40%;
margin-bottom: 30%;
position: relative;
text-align: center;
width: 800px;
}
.button {
z-index:1000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="overlay">
Overlay showing
</div>
</br></br></br></br>
</br></br></br></br>
<input id="clickMe" class="button" type="button" value="clickme" onclick="overlay();" />
原因:-
根据文档:-https://api.jquery.com/visible-selector/
如果元素占用文档中的空间,则认为元素是可见的.
由于overlay div始终具有可见性:隐藏所以基本上它的空间就在那里,这就是原因:可见总是返回true.
如果你想使用:visible然后执行display:none;和显示:块;
标签:jquery,javascript,overlay,html 来源: https://codeday.me/bug/20190527/1164493.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。