Я делаю для себя персональный сайт-портфолио.
<footer>
<ul class = "icons">
<li><a href = "https://github.com" class = "fa-github">GitHub</a></li>
<li><a href = "Resume.pdf" class = "fa-file-text-o">Resume</a></li>
<li><a href = "#" class = "fa-phone"> Contact</a></li>
</ul>
</footer>
Можно ли в приведенном выше коде включить модальное окно для кнопки контакта? Я создал страницу "contact.html" и хотел бы, чтобы она всплывала при нажатии кнопки, а не открывала целую новую страницу.
Или мне придется использовать тег кнопки вместо тега?
Мне удобно использовать Bootstrap, и при необходимости я могу реализовать модальное окно с этой структурой.
Вы можете использовать этот пользовательский модальный или загрузочный модальный https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
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 */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<button id = "myBtn">Open Modal</button>
<!-- The Modal -->
<div id = "myModal" class = "modal">
<!-- Modal content -->
<div class = "modal-content">
<span class = "close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
Вы можете использовать эту модель:
<html>
<head>
<title>
modal
</title>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
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 */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<ul class = "icons">
<li><a href = "https://github.com" class = "fa-github">GitHub</a></li>
<li><a href = "Resume.pdf" class = "fa-file-text-o">Resume</a></li>
<li><a href = "#" class = "fa-phone" id = "contact"> Contact</a></li>
</ul>
<div id = "myModal" class = "modal">
<!-- Modal content -->
<div class = "modal-content">
<span class = "close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var contact = document.getElementById("contact");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
contact.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>