gpt4 book ai didi

html模态弹出窗口

转载 作者:技术小花猫 更新时间:2023-10-29 12:25:36 24 4
gpt4 key购买 nike

如何为以下代码制作一个简单的模态弹出窗口。点击背景时,模态弹出窗口不应消失。

<html>
<input type="textarea"></input>
</html>

最佳答案

这是一个纯 JavaScript 示例:

var modal = document.getElementById('modal');
var shade = document.getElementById('shade');
document.getElementById('start').onclick = function() {
modal.style.display = shade.style.display = 'block';
};
document.getElementById('close').onclick = function() {
modal.style.display = shade.style.display = 'none';
};

// This code is a workaround for IE6's lack of support for the
// position: fixed style.
//
if (!('maxHeight' in document.body.style)) {
function modalsize() {
var top = document.documentElement.scrollTop;
var winsize = document.documentElement.offsetHeight;
var docsize = document.documentElement.scrollHeight;
shade.style.height = Math.max(winsize, docsize) + 'px';
modal.style.top = top + Math.floor(winsize / 3) + 'px';
};
modal.style.position = shade.style.position = 'absolute';
window.onscroll = window.onresize = modalsize;
modalsize();
}
body {
margin: 0;
}

#shade,
#modal {
display: none;
}

#shade {
position: fixed;
z-index: 100;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

#modal {
position: fixed;
z-index: 101;
top: 33%;
left: 25%;
width: 50%;
}

#shade {
background: silver;
opacity: 0.5;
filter: alpha(opacity=50);
}
<div id="shade"></div>
<div id="modal">
<textarea rows="5" cols="25"></textarea>
<button id="close">Close</button>
</div>

<p>
<button id="start">Start</button>
</p>

您可以从那里进行各种改进,例如 iframe hack 以修复 IE z-indexing,或将其封装在可重用对象中,但这是完成的基本方式。

关于html模态弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1992142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com