HTML Modal Not Opening - javascript

I am currently trying to get a simple modal to work within HTML but am having some trouble. I believe it might be with the <div> placements I have but am not sure. Can someone please take a look a let me know what I am doing wrong here?
Here is the code I am using:
<style type="text/css">
.cdynamic-template h2 {
font-size: 24px;
font-weight: 450;
color: "[theme:neutralPrimary, default:#323130]";
}
.cdynamic-template .cdynamic-items .cdynamic-item {
background: "[theme:bodyBackground, default: #fff]";
border: 1px solid "[theme:neutralLight, default: #edebe9]";
//box-shadow: 0px 0px 6px #bfbebe;
margin-bottom: 15px;
}
.cdynamic-template .cdynamic-items .cdynamic-item h3 {
background: "[theme:accentButtonBackground, default:#0078d4]";
color: "[theme:accentButtonText, default: #fff]";
padding: 5px 5px 7px 10px;
margin: 0px;
background-color: #3a4678;
}
.cdynamic-template .cdynamic-items .cdynamic-item .cdynamic-item-fields {
padding: 10px;
}
.cdynamic-template .cdynamic-items .cdynamic-item .cdynamic-item-fields span {
display: block;
font-size: 14px;
}
.cdynamic-item {
float: left;
width: 325px;
height: 200px;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
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/Box */
.modal-content {
background-color: #fefefe;
margin: 15% auto;
/* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%;
/* Could be more or less, depending on screen size */
}
/* The Close Button */
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
<!-- 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>
<div class="cdynamic-template">
<h2>Most Recent Creatives
<br><a href=https://ewscripps.sharepoint.com/sites/LighthouseIdeas/SitePages/Creatives.aspx>See All...</a>
</h2>
<div class="cdynamic-items">
{{#each items}}
<div class="cdynamic-item">
<h3>{{Title.textValue}}</h3>
<div class="cimage">
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAEYklEQVRoge3Y64tUdRzH8ffvzM7e15ldM9JtvIURQRAUhQRlSkYQIT0RFCLE/gfRQBFJicgHEmSXJ0E+CYKCJFPZjTKjjKB6EOSNzRtedkd3ZndmZ87304NzZtzx7Obcthac75OZc85vfr/X53x/h2EGWtWqVrWqVfdyuWoGXXtlS1+ny6ccSjnZIJAyGMQ0ACSRkhJJUB8ijgSoW6IDCcGkk3ISgOWRGwfSyNISaYeuSbqMMeLwLxR8XcxZ30jq5GeTdQW4+fKmVTFPW3FuPbACSAAEMJAEAhBI4enwXIAPX4LxlWNmOjfL59ANSWfw9bW18f6i745cvmuAzIZNryPeA7orLvzXeClcqzxm1MxeW/TTsa9mDZDZsHkd0hEgNs/wKJgj63msHjh57PcSzat0as88xoPo8Yv25owdmHh184Pma6SiK/MLX7qey/Ra/4rh4VxFB8zXI/Xhw/klCjKKZnOJB9TZd8tWlJjlAM6xsr47H1zL+kUmn3ic7GOPMl4sYGZzgQ+OLfZwido2zbyynjtfep8v+izfvR0Xj5M+cpyxAwfpyU7S7rzm4iV8WDVzB2rCV24RQ7h4HIDki+tIfXKQ/Oonw26oafjwOBoAsyX14stjplXbQD+pt3ayYNc2xro6yMtvDl7gUCoSQHIDDeFVGaBUibXPsuzTD5la/VTQjfLc9eGRwFgUDYCSzcZXdGPfLhK7tzPW3UnO/PrxCOHfFwmA6GkIf5cQAIl1z7H80McUnnma8WIRk9WOl8BcbzQAam8EX4UfCLqx9O3dJPfsIN3TRcGsNnywdmc0gNTeGL7KBGElXnieZYc+ItvdjWrBB9eiARrG1+a/XbXjARUiAUBTDeGr3UNhpY8OcX7jFrozWVwt+OA4XZqn/E2MmCJsTX346gIUb4xyce+7aOh7Ep6H56gNH6w/UwBlgQX14qtpQProEFf37qd3PEO7F6uYowY8km5FA8CYpMV14/8lQPmuD5+g3/NwuEbwII1FAkg2WkbUjJ9dnz46xNV9++kdz9LhxWp/YKP4WbaQ6RLO1Y+/I0T5rn97gn4vhodrEh6cuBIJ4PDOSfX/GEFCUwVce5yxw99w/Z0D9GQn6PDabgOagEfCdzoTCWDOP4uV9mZteEl0eY6/t+1EuTw69StJz8PDazpeCK/I2Rm2UOws+HXhkehybRR/OIUQcS9WCWoiHgmTK3eg/EXmF/VnuFrN+GBfijYH8fBjc4VH5CZi6fORAAuHP78A/FgP/o4F5hKPM32x6vTpfCRAQNYOieJ8xSNlnFfcM91cEaD/+JdDiK0YE/MOb7rhYxsfOPPbH9PNM/65m1770kNm3huYW49sJZD4n/CjGH85OBwz74P7z/985U5rVX+vX12zpjfmdy3F/EH53qBzWirZYtBCRD+mhExJnLoQPSGoE9EV4ick5cOHPeNLk05K49xNZ5Y27Dqmy8iNOHTBsItkGFly6ZeJanytalWrWtWqe7f+AVrzIKbgsx9BAAAAAElFTkSuQmCC">
</div>
<div class="cdynamic-item-fields">
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<span class='cms-DetailsRow-cell'><strong>Industry: </strong>{{Industry.textValue}}</span>
<span class='cms-DetailsRow-cell'><strong>Description: </strong>{{Description.textValue}}</span>
<span class='cms-DetailsRow-cell'><strong>Video Type: </strong>{{VideoType.textValue}}</span>
</div>
</div>
{{/each}}
</div>
</div>
<script>
// 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 on 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";
}
}
</script>
Any help in the right direction will be much appreciated!
Thanks!

You have comment mistake for CSS
CSS uses /* */ (block comments) not //
//box-shadow: 0px 0px 6px #bfbebe;
Checkout this line, replace it with
/*box-shadow: 0px 0px 6px #bfbebe;*/
And everything will work just fine

Related

Dynamically Create a Modal for a Note Taker App

I am working on a simple note taking app using vanilla javascript. I am trying to have the program add the note with a modal that when clicked would show the text. With what I have so far it is adding the note below the input box and along with the modal button. When I click the modal button it does nothing the first click. On the second click the text and modal button disappear.
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Note Tracker</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;
}
.success {
background-color: #ddffdd;
border-left: 6px solid #4CAF50;
}
</style>
</head>
<body>
<h1>Note Tracker Web App</h1>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<label for="iNote">Input Note:</label><br>
<br>
<textarea id="inote" name="inote" rows="4" cols="50">
</textarea>
<br>
<button type="button" id="btn" onclick="addNote()">Add Note</button>
<br><br>
<div id="noteList">
<span class="close">×</span>
</div>
<script src="scripts.js"></script>
</body>
Javascript is the below that creates the note and then add it along with the modal
function addNote(){
var item = document.getElementById("inote").value
var text = document.createTextNode(item)
var newItem = document.createElement("P")
newItem.appendChild(text)
document.getElementById("noteList").appendChild(newItem)
var x = document.createElement("BUTTON");
x.id = "someId";
//x.onclick ="modalOpen()";
x.onclick = function(){
var modal = document.getElementById("noteList");
var btn = document.getElementById("someId");
btn.onclick = function() {
modal.style.display = "none";
}
};
var t = document.createTextNode("Open Modal");
x.appendChild(t);
document.getElementById("noteList").appendChild(x);
var z = document.createElement("BR");
document.getElementById("noteList").appendChild(z);
var newElem = document.createElement("BR");
document.getElementById("noteList").appendChild(newElem);
}
on first time, you are just attach event listener of click, simply put x.onclick outside the function
Hopefully this one will help.
So we have the "note-list" to handle the list.
I create a modal element that can activate when we click on the "new note" button.
In here I play with opacity and z-index to show this modal. Can be better than this.
const newNote = document.getElementById('new-note'),
addNote = document.getElementById('add-note');
let myModal = document.getElementById('my-modal');
newNote.addEventListener('click', () => {
myModal.style.zIndex = 99;
myModal.style.opacity = 1;
});
addNote.addEventListener('click', () => {
let note = document.getElementById('note'),
noteList = document.getElementById('note-list');
if (note.value !== '') {
let _el = document.createElement('li');
_el.innerHTML = note.value;
let _a = document.createElement('a');
_a.innerHTML = 'delete';
_el.appendChild(_a);
noteList.appendChild(_el);
note.value = '';
myModal.style.zIndex = -1;
myModal.style.opacity = 0;
_a.addEventListener('click', (e) => {
e.target.parentNode.remove();
});
} else {
alert('note can not empty');
}
});
#my-modal {
width: 100%: height: 100%;
z-index: -1;
opacity: 0;
position: absolute;
background: rgba(0, 0, 0, 0.5);
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.modal-wrapper {
border-radius: .5rem;
background: #fff;
display: block;
padding: 1rem;
margin-top: 20%;
}
ul {
display: block;
}
#note-list li {
display: block;
margin-bottom: .5rem;
border: 1px solid #efefef;
background: #f7f7f7;
border-radius: .5rem;
position: relative;
padding: 1rem;
width: 70%;
}
#note-list li a{
position: absolute;
right: 0;
top: 0;
background: tomato;
padding: 1rem;
color: #fff;
}
.modal-wrapper * {
display: block;
margin: .5rem auto;
width: 90%;
text-align: center;
}
<h1>Note Taker App</h1>
<div class="note-wrapper">
<ul id="note-list">
</ul>
<button id="new-note">New Note</button>
</div>
<div id="my-modal">
<div class="modal-wrapper">
<label for="note">Your Note</label>
<input type="text" name="note" id="note">
<button id="add-note">add note</button>
</div>
</div>

HTML/CSS/JS - Overlay backgrounds for modal popups - Issues with

I'm trying to add a semi-opaque overlay to my page that supports multiple modal popups.
The three popup boxes open OK without the need for Javascript, and, with the help of some Javascript, they close by mouse-clicking outside the popups.
Unfortunately, I can't get my overlay to work, without blocking the 'open-modal' buttons. I've tried wrapping the entire 'overlay' div around all the popup boxes, and I've tried keeping the popups outside of the overlay div.
Is there a way to fix this without blocking access to the buttons, and without messing up the 'external close' feature as facilitated by the Javascript?
Three files are attached: ‘.index.html’, ‘style.css’, and ‘modal-script.js’.
Apologies if my terminology is sometimes ‘homespun’, but I’m just and enthusiast doing the best I can.
My code so far is below in this same document. I would be grateful for any suggestions.
HTML CODE:
~ Main Document
CSS (STYLESHEET):
~ Modal Environment
JAVASCRIPT:
~ External Close of Popup Boxes
// JAVASCRIPT FILE: js/modal-script.js
// Closes multi-modals in an HTML page
// SET VARIABLES:
var boxArray = ['box1','box2','box3'];
// LISTEN FOR WINDOW EVENT
window.addEventListener('mouseup', function(event){
// LOOP...
for(var i = 0; i < boxArray.length; i++) {
var box = document.getElementById(boxArray[i]);
// IF...
if(event.target != box && event.target.parentNode != box){
// THEN...
box.style.display = 'none';
} // END IF/THEN STATEMENT
} // END LOOP
}); // END EVENT
/* STYLESHEET FOR MODAL ENVIRONMENT */
/* Pesets */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a:link, a:visited {
text-decoration: none;
}
p {
margin-top: 0;
}
body {
font-family: 'Halvetica', Arial, sans-serif; /* Default font family */
}
/* MODAL ENVIRONMENT */
.modal { /* Format the 'modal-window', which is the modal environment background containing the 'modal-box(es)' */
background: rgba(0, 0, 0, 0.8);
position: absolute;
width: 100%;
height: 100vh;
top: 0;
left: 0;
z-index: 10;
display: none;
}
.modal-content { /* Framework and default settings for popup boxes */
position: absolute;
background: #e2e2e2;
width: 80%;
height: 60%;
top: 50%;
left: 50%;
padding: 20px;
transform: translate(-50%, -50%);
border-radius: 1em;
display: none;
}
.modal:target { /* Where '.modal' is the target, make it visible */
display: block;
}
.modal:target .modal-content { /* Where 'modal-content' inside of 'modal' is the target, make both visible */
display: block;
}
/* MY POPUP BOXES */
#box1 {
}
#box2 {
}
#box3 {
}
/* Formatting: */
.button {
width: 250px;
height: 30px;
}
.type_1-button {
width: 250px;
height: 30px;
font-size: 0.9em;
font-weight: normal;
color: #000;
margin: 20px;
}
.type_1-button:hover {
background: dodgerblue;
font-size: 1em;
font-weight: bold;
color: #fff;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Multi-modal</title>
<script src="modal-script.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!--Button controls to trigger pop-up boxes-->
<button onclick="document.getElementById('box1').style.display = 'block'" class="type_1-button">Open Box 1</button>
<button onclick="document.getElementById('box2').style.display = 'block'" class="type_1-button">Open Box 2</button>
<button onclick="document.getElementById('box3').style.display = 'block'" class="type_1-button">Open Box 3</button>
<!--MODAL CODE-->
<div id="overlay" class="modal"> <!--Create modal window/environment/background-->
<!--PROBLEM HERE... WHAT TO DO???-->
</div> <!--End 'overlay' div and 'modal' class-->
<!--myBoxes: box1-->
<div id="box1" class="modal-content">
<h2>Pop-out Interface - Box1</h2>
</div> <!--End 'box1'-->
<!--myBoxes: box2-->
<div id="box2" class="modal-content">
<h2>Pop-out Interface - Box2</h2>
</div> <!--End 'box2'-->
<!--myBoxes: box3-->
<div id="box3" class="modal-content">
<h2>Pop-out Interface - Box3</h2>
</div> <!--End 'box3'-->
<!--END MODAL CODE-->
</body>
</html>
Add z-index:11 to .modal-content class and remove display:none from .modal class or add display:block to .modal class when clicking button.
If you want to access 3 buttons also when modal popup is appear add z-index: 11; position: relative; in .type_1-button class.
Add z-index:-1; to .modal and change some javascript as below
// JAVASCRIPT FILE: js/modal-script.js
// Closes multi-modals in an HTML page
// SET VARIABLES:
var boxArray = ['box1','box2','box3'];
// LISTEN FOR WINDOW EVENT
window.addEventListener('mouseup', function(event){
// LOOP...
for(var i = 0; i < boxArray.length; i++) {
var box = document.getElementById(boxArray[i]);
// IF...
if(event.target != box && event.target.parentNode != box){
// THEN...
debugger;
box.style.display = 'none';
} // END IF/THEN STATEMENT
} // END LOOP
}); // END EVENT
document.getElementById("overlay").addEventListener("click", function(event){
document.getElementById('overlay').style.display = 'none';
});
/*STYLESHEET FOR MODAL ENVIRONMENT*/
/*Pesets*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a:link, a:visited {
text-decoration: none;
}
p {
margin-top: 0;
}
body {
font-family: 'Halvetica'; Arial, sans-serif; /* Default font family */
}
/*MODAL ENVIRONMENT*/
.modal { /*Format the 'modal-window', which is the modal environment background containing the 'modal-box(es)'*/
background: rgba(0,0,0,.8);
position: absolute;
width: 100%;
height: 100vh;
top: 0;
left: 0;
z-index: 10;
display: none;
}
.modal-content{ /*Framework and default settings for popup boxes*/
position: absolute;
background: #e2e2e2;
width: 80%;
height: 60%;
top: 50%;
left: 50%;
padding: 20px;
transform: translate(-50%,-50%);
border-radius: 1em;
display: none;
}
.modal:target { /* Where '.modal' is the target, make it visible */
display: block;
}
.modal:target .modal-content { /* Where 'modal-content' inside of 'modal' is the target, make both visible */
display: block;
}
/*MY POPUP BOXES*/
#box1 {
}
#box2 {
}
#box3 {
}
// Formatting:
.button {
width: 250px;
height: 30px;
}
.type_1-button {
width: 250px;
height: 30px;
font-size: 0.9em
font-weight: normal;
color: #000;
margin: 20px;
}
.type_1-button:hover {
background: dodgerblue;
font-size: 1em;
font-weight: bold;
color: #fff;
}
.modal{
z-index:-1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Multi-modal</title>
<script src="modal-script.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!--Button controls to trigger pop-up boxes-->
<button onclick="document.getElementById('box1').style.display = 'block';document.getElementById('overlay').style.display = 'block'"
class="type_1-button">Open Box 1</button>
<button onclick="document.getElementById('box2').style.display = 'block';document.getElementById('overlay').style.display = 'block'"
class="type_1-button">Open Box 2</button>
<button onclick="document.getElementById('box3').style.display = 'block';document.getElementById('overlay').style.display = 'block'"
class="type_1-button">Open Box 3</button>
<!--MODAL CODE-->
<div id="overlay" class="modal"> <!--Create modal window/environment/background-->
<!--PROBLEM HERE... WHAT TO DO???-->
</div> <!--End 'overlay' div and 'modal' class-->
<!--myBoxes: box1-->
<div id="box1" class="modal-content">
<h2>Pop-out Interface - Box1</h2>
</div> <!--End 'box1'-->
<!--myBoxes: box2-->
<div id="box2" class="modal-content">
<h2>Pop-out Interface - Box2</h2>
</div> <!--End 'box2'-->
<!--myBoxes: box3-->
<div id="box3" class="modal-content">
<h2>Pop-out Interface - Box3</h2>
</div> <!--End 'box3'-->
<!--END MODAL CODE-->
</body>
</html>
Thanks for your feedback, people. I wasn't happy with my own solution, so I took it back to the 'drawing board' with all of your valuable comments in mind.
My code now produces a set of three interchangeable modals with two options for closing - press the 'X' symbol or mouse click the background. I will be looking for the third option of closing by [Esc.] key later. Pleas let me know if you have any recommended methods for closing with the escape key.
The overlay window opens and closes at the right moments without interfering with other elements. I hope this make a worthwhile exemplar for anyone looking for modal solutions.
The code is about to follow.
Thank you all,
Adrian McG
// MODAL CODE
// Open and close multiple modal boxes
// Project Title: Muli-modals */
// GET MODAL-OPEN BUTTONS
var modalBtns = document.querySelectorAll(".modal-open"); // Get 'ALL' buttons with the '.modal-open' class
// Make a forLoop to work for 'each' individual modal button...
modalBtns.forEach(function(btn) { // Create a function called "btn" to work in a forLoop for each one that equals 'modalBtns'
btn.onclick = function() { // On mouse-click, activate the 'btn' function and let it do the following...
var modal = btn.getAttribute("data-modal"); // Declare and set a variable called 'modal' to have the same attribute...
//as any element that has the property of 'data-modal' (attached to the modal buttons)
document.getElementById(modal).style.display = "block"; // ...then get the 'modal' document, stored in 'data-modal',
//and set its display style attribute to "block", which will display all elements with the '.modal' class
}; // End function
}); // End forLoop
// CLOSE MODALS: Method 1 - Close by button click
// Get all butons with the '.modal-close' class
var closeBtns = document.querySelectorAll(".modal-close");
closeBtns.forEach(function(btn) { // Create a function called "btn" to work in a forLoop
btn.onclick = function() { // On mouse-click, activate the 'btn' function and let it do the following...
var modal = btn.closest(".modal").style.display = "none"; // ...then get the 'modal' document, and set its display style
// attribute to "none", which will make all elements with the '.modal' class invisible
} // End function
}) // End forLoop and function
// CLOSE MODALS: Method 2 - Close by external click on the overllay window
window.onclick = function(e) { // Creat a function named 'e' (for event) to work on mouse-click event
if(e.target.className === "modal") { // If the target of the mouse-click event is strictly equivalent to the class 'modal'...
e.target.style.display = "none"; // ...then get set the targeted element to 'none'; or, in other words, make the
// modal invisible
} // End if/then statement
} // End function
/* Default CSS */
/* PRESETS */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Halvetica', Arial, sans-serif; /* Default font family */
}
a:link, a:visited { /* Prevents links from automaticilly being underlined, unless otherwide specified */
text-decoration: none;
}
p {
margin-top: 0;
}
body {
margin: 0 auto;
}
/* Main HTML page as starting point */
.container { /* Create a wrapper to center the button objects on screen ...
... This obviously will change according to main page layout */
position: fixed;
width: 500px;
height: 300px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: aqua;
padding: 20px 20px 0 20px;
}
/* MODAL ENVIRONMENT */
.modal { /* This is the modal window-overlay that masks out the page we started on */
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.6);
animation: modal-open .5s;
z-index: 200;
display: none;
}
/*MODAL BOXES AND CONTENT*/
.modal-content { /* The modal box that pops up inside the modal window-overlay */
position: relative;
background: #fff;
width: 400px;
height: 300px;
top: 25%;
left: 50%;
transform: translate(-50%,-50%);
border-radius: 15px;
z-index: 400;
display: inline-block;
}
.modal-header {
height: 15%;
width: 100%;
background-color: #284254;
padding: 5px 15px;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
border-style: outset;
border-width: medium;
border-color: #7B919D;
border-bottom-style: outset; /* Strengthen shadow at bottom-border bevelled edge */
border-bottom-width: 4px; /* Strengthen shadow at bottom-border bevelled edge */
border-bottom-color: #1F323F; /* Strengthen shadow at bottom-border bevelled edge */
display: inline-block;
}
.modal-body {
width: 100%;
height: 72.75%;
color: #7b7b7b7;
padding: 15px 0;
background-color: #fff;
background: linear-gradient(#fff, #999); /* Adds gradient to the modal box background,...
from white (top) to light grey (bottom) */
}
.modal-footer {
width: 100%;
height: 12.25%;
font-size: 14px;
padding: 5px 15px;
border: none;
outline: none;
border-radius: 15px;
color: #1a73e8;
background-color: #fff;
background: linear-gradient(#fff, #999); /* Adds gradient to the modal box background,...
from white (top) to light grey (bottom) */
}
/*MODAL CONTROLS*/
.modal-open {
width: 150px;
height: 30px;
font-size: 0.9em;
font-weight: normal;
color: #000;
}
.modal-open:hover {
font-size: 1em;
font-weight: bold;
background: dodgerblue;
color: #fff;
}
.modal-close {
position: relative;
background: #c3c3c3c;
width: 42px;
height: 42px;
top: -60px;
left: 38px;
border-radius: 50%;
color: #5b5b5b;
font-size: 2.5em;
font-weight: bold;
line-height: 0.7;
border: solid aqua 5px;
box-shadow: 2px 4px 10px #2d2d2d;
float: right;
display: inline-block;
}
.modal-close:hover {
background: #5b5b5b;
color: #c3c3c3;
}
/*FONTS AND PARAGRAPH SPACING*/
.modal-header-text {
font-size: 1.15em;
font-weight: bold;
text-align: left;
color: #00FFFF;
margin: 5px 5px 5px 5px;
}
.modal-heading{
height: 40px;
font-size: 1.25em;
font-weight: bold;
color: dodgerblue;
text-align: center;
margin: 6px 5px 0 5px;
}
.modal-paragraph{
font-size: 1em;
color: #000000;
line-height: 1.5em;
text-align: center;
margin: 0px 5px 10px 5px;
}
.modal-footer-text {
font-size: 0.9em;
font-weight: normal;
font-style: italic;
text-align: center;
color: #000000;
margin: 5px 5px 5px 5px;
}
/*Footer Anchor Links - /*Anchor-link behaviour in footer*/
.modal-footer-text a:link {
color: #6900CC;
text-decoration: none;
background-color: transparent;
}
.modal-footer-text a {
color: #6900CC;
}
.modal-footer-text a:visited {
color: #505050;
text-decoration: none;
background-color: transparent;
}
.modal-footer-text a:hover {
color: #0000FF;
text-decoration: underline;
background-color: transparent;
}
.modal-footer-text a:selected {
color: #0000FF;
font-weight: bold;
text-decoration: none;
background-color: transparent;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="dcterms.created" content="Tue, 05 Feb 2019 11:43:55 GMT">
<title>Multi-modals</title>
<!--JAVASCRIPT-->
<!--Microsoft Internet Explorer - Finds free sourcecode to translate old versions-->
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!--STYLESHEETS-->
<link rel="stylesheet" type="text/css" href="css/modal-style.css" />
<!--END STYLESHEETS-->
</head>
<body>
<div class="container">
<!--BUTTONS TO TRIGGER MODALS-->
<button class="modal-open" data-modal="search">Search</button>
<button class="modal-open" data-modal="login">Log-in/Sign-up!</button>
<button class="modal-open" data-modal="spare">Spare</button>
</div> <!--End 'container' div-->
<!--MODAL CODE-->
<!--Modal 1-->
<div class="modal" id="search"> <!--Overlay window to act as backdrop to the modal box-->
<div class="modal-content"> <!--Format layout and content of modal box-->
<div class="modal-header"> <!--Format the modal box header-->
<h1 class="modal-header-text">Modal: Search eruditeAlpha.com</h1>
<button class="modal-close">×</button> <!--Button to close modal-->
</div><!--End modal header-->
<div class="modal-body"> <!--Format the modal box body content-->
<h2 class="modal-heading">Search Contents</h2>
<p class="modal-paragraph">This modal is not yet fully functional...</p>
</div> <!--End modal body-->
<div class="modal-footer"> <!--Format the modal box footer-->
<p class="modal-footer-text">Follow me at
<a href="http://adrian-mcglinchey.blogspot.com/" target="blank">
"Adrian's Write"</a> blog space...</p>
</div> <!--End modal footer-->
</div> <!--End modal content-->
</div> <!--End 'modal1' overlay/backdrop-->
<!--Modal 2-->
<div class="modal" id="login"> <!--Overlay window to act as backdrop to the modal box-->
<div class="modal-content"> <!--Format layout and content of modal box-->
<div class="modal-header"> <!--Format the modal box header-->
<h1 class="modal-header-text">Modal: Log-in / Sign-up!</h1>
<button class="modal-close">×</button> <!--Button to close modal-->
</div><!--End modal header-->
<div class="modal-body"> <!--Format the modal box body content-->
<h2 class="modal-heading">Members' Area</h2>
<p class="modal-paragraph">This modal is not yet fully functional...</p>
</div> <!--End modal body-->
<div class="modal-footer"> <!--Format the modal box footer-->
<p class="modal-footer-text">Follow me at
<a href="http://adrian-mcglinchey.blogspot.com/" target="blank">
"Adrian's Write"</a> blog space...</p>
</div> <!--End modal footer-->
</div> <!--End 'modal2' content-->
</div> <!--End 'modal2' overlay/backdrop-->
<!--Modal 3-->
<div class="modal" id="spare"> <!--Overlay window to act as backdrop to the modal box-->
<div class="modal-content"> <!--Format layout and content of modal box-->
<div class="modal-header"> <!--Format the modal box header-->
<h1 class="modal-header-text">Modal: Spare</h1>
<button class="modal-close">×</button> <!--Button to close modal-->
</div><!--End modal header-->
<div class="modal-body"> <!--Format the modal box body content-->
<h2 class="modal-heading">Keep in Reserve</h2>
<p class="modal-paragraph"> This modal is not yet fully functional...</p>
</div> <!--End modal body-->
<div class="modal-footer"> <!--Format the modal box footer-->
<p class="modal-footer-text">Follow me at <a href="http://adrian-mcglinchey.blogspot.com/"
target="blank">
"Adrian's Write"</a> blog space...</p>
</div> <!--End modal footer-->
</div> <!--End 'modal2' content-->
</div> <!--End 'modal3' overlay/backdrop-->
<!--JAVASCRIPT CODE FILE-->
<!--External closing of modal popup boxes-->
<script src="js/modal-script.js" type="text/javascript"></script>
</body>
</html>
With that all said, feel free to comment constructively, people, and please don't be too harsh on me if I've made mistakes; I am a web development enthusiast, not a webGuru.
Thanks once again,
Adrian McG

How make a modal transition?

I'm trying to animate a modal with a transition. I'm just looking for open it slowly but i don't understand how it works...
The modal have to be open from the center of the screen or from the screen down side.
I found this code from google :
https://codepen.io/designcouch/pen/obvKxm
But it's really too complicated for me. I can't understand how adapt it with my code...
/*Ouvrir le popup stress */
// Get the modal
var hydricstressmodal = document.getElementById('hydricstressmodal');
// Get the button that opens the modal
var stress = document.getElementById("stress");
// Get the <span> element that closes the modal
var hydricstressspan = document.getElementsByClassName("stressclose")[0];
// When the user clicks the button, open the modal
stress.onclick = function() {
hydricstressmodal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
hydricstressspan.onclick = function() {
hydricstressmodal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == hydricstressmodal) {
hydricstressmodal.style.display = "none";
}
}
/*Ouvrir le popup vegetal */
// Get the modal
var vegetalmodal = document.getElementById('vegetalmodal');
// Get the button that opens the modal
var vegetal = document.getElementById("vegetal");
// Get the <span> element that closes the modal
var vegetalspan = document.getElementsByClassName("vegetalclose")[0];
// When the user clicks the button, open the modal
vegetal.onclick = function() {
vegetalmodal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
vegetalspan.onclick = function() {
vegetalmodal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == vegetalmodal) {
vegetalmodal.style.display = "none";
}
}
#charset "UTF-8";
/* CSS Document */
body {
margin: 0;
font-size: 28px;
background-color: #00011f;
display: flex;
flex-direction: column;
margin : auto;
}
/*popup hydric stress*/
.hydricstressmodal {
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.7); /* Black w/ opacity */
}
/* stress Modal Content */
.stress-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
height: 70%;
border-radius: 30px;
overflow: scroll;
}
.popstress img{
width: 20%;
}
/* The Close Button */
.stressclose {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.stressclose:hover,
.stressclose:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
/*popup Vegetal*/
.vegetalmodal {
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.8); /* Black w/ opacity */
scale
}
/* stress Modal Content */
.vegetal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
height: 70%;
border-radius: 30px;
overflow: scroll;
}
.popvegetal img{
width: 40%;
}
/* The Close Button */
.vegetalclose {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.vegetalclose:hover,
.vegetalclose:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
<div id="content">
<h3>Electrophotonique Ingénierie : Nouvelle approche de l'imagerie macroscopique par effet de couronne dans le domaine de la santé et des biotechnologies.</h3>
<div id="file" action="" class = "container">
<input id = "stress" type="image" src="IMAGES/PNG/hydricstress.png" />
<div class = "text">
Stress hydrique
</div>
</div>
<!-- The hydric stress Modal -->
<div id="hydricstressmodal" class="hydricstressmodal">
<div class="stress-content">
<span class="stressclose">×</span>
<div class ="popstress" ><img src="images/png/hydricstress.png"></div>
<p>Some text in the Modal..</p>
</div>
</div>
<div id="file" action="" class = "container">
<input id = "vegetal" type="image" src="IMAGES/PNG/vegetal.png" />
<div class = "text">
Biophotonique appliquée aux végétaux
</div>
</div>
</div>
<!-- The vegetal Modal -->
<div id="vegetalmodal" class="vegetalmodal">
<div class="vegetal-content">
<span class="vegetalclose">×</span>
<div class ="popvegetal" ><img src="images/png/vegetal.png" ></div>
<p>Some text in the Modal..</p>
</div>
</div>
<div id="file" action="" class = "container">
<img src="IMAGES/PNG/pont.png" width="100%" />
<div class = "text">
Etudes des ponts photoniques
</div>
</div>
<script type="text/javascript" src="JS/sticky_navbar.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src="js/index.js"></script>
<script src="js/button.js"></script>
A simple solution with css animations:
$('#open').click(function() {
$('#modalOverlay').show().addClass('modal-open');
});
$('#close').click(function() {
var elem = $('#modalOverlay');
elem.removeClass('modal-open');
setTimeout(function() {
elem.hide();
},200);
});
#modalOverlay {
position: fixed;
top:0;
left:0;
right:0;
bottom:0;
background-color: rgba(0,0,0,0.8);
z-index:9999;
}
#modal {
position:fixed;
width:60%;
top:55%;
left:50%;
padding:15px;
text-align:center;
background-color:#fafafa;
box-sizing:border-box;
opacity:0;
transform: translate(-50%,-50%);
transition:all 150ms ease-in-out;
}
#modalOverlay.modal-open #modal {
opacity:1;
top:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="open" type="button">View modal</button>
<div id="modalOverlay" style="display:none;">
<div id="modal">
<h1>My modal</h1>
<p>This is a simple modal</p>
<button id="close" type="button">Close</button>
</div>
</div>
If you don't want to spend too much time on this kind of feature you could use a library like https://sweetalert2.github.io/ which is very easy to use and do the job.
You can also use library like bootstrap. For bootstrap modal you can refer below link.
https://getbootstrap.com/docs/4.0/components/modal/

Is it possible to implement a modal with a list tag? Or would I have to use a button?

I am making a personal portfolio website for myself.
<footer>
<ul class="icons">
<li>GitHub</li>
<li>Resume</li>
<li> Contact</li>
</ul>
</footer>
In the code that is above, would it be possible to include a modal for the contact button? I have created a "contact.html" page and would like it to pop-up when the button is clicked rather than have a whole new page open up.
Or would I have to use the button tag instead of the tag?
I am comfortable with using Bootstrap and can implement a modal with that framework if necessary.
You can Use this custom modal or bootstrap modal 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>
You can use this model:
<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>GitHub</li>
<li>Resume</li>
<li> Contact</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>

Modal popup is not showing in asp.net

I am trying to make a modal popup in asp.net (just css and js) but when click the button. the background shows up and closes really fast. and the modal doesnt show. this is the code.
asp.net
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
css:
/* 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 {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
#-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
#keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
javaScript:
<script type="text/javascript">
// 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";
}
}
</script>
but it doesnt work. I followed this tuttorial here: W3Schools
EDIT: I removed the snipped because the code was just too big and just repeated it self. but also published the page and you can test it here.
http://detos.azurewebsites.net/Test.aspx
the modal closes it self after 1 second
I'm not sure if I'm allowed to post an answer for my post. but I found the error.
somehow when clicking a button in asp.net it refreshes the page. and after changing that button to it worked perfectly.
Try to remove this code, I think that's the problem
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}

Categories