<div id="popup"> <div id="popup-content"> <h2>这是一个弹窗</h2> <p>这里是弹窗的内容</p> <button id="close-btn">关闭</button> </div> </div>
#popup { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; width: 300px; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); display: none; } #popup-content { text-align: center; } #close-btn { margin-top: 20px; padding: 10px 20px; background-color: #337ab7; color: #fff; border: none; border-radius: 5px; cursor: pointer; }
var popup = document.getElementById('popup'); var closeBtn = document.getElementById('close-btn'); function openPopup() { popup.style.display = 'block'; } function closePopup() { popup.style.display = 'none'; } closeBtn.addEventListener('click', closePopup); window.addEventListener('click', function(event) { if (event.target == popup) { closePopup(); } });
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com