forked from weboverhauls/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal_demo.html
194 lines (155 loc) · 5.04 KB
/
modal_demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Modal Dialog Example</title>
<meta name="description" content="Demonstration of an accessible modal dialog." />
<meta name="keywords" content="demo, dialog, modal, accessibility, a11y" />
<meta name="author" content="Dennis Lembree" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#mask {
position: absolute;
z-index: 100;
background-color: #666;
display: none;
top: 0;
left: 0;
-moz-opacity: 0.8;
opacity: 0.8;
}
.modal {
position: fixed;
width: 60%;
max-width: 650px;
min-width: 250px;
z-index: 101;
padding: 25px;
background-color: #efefef;
}
</style>
</head>
<body>
<!-- http://jsfiddle.net/weboverhauls/GPd5u/6/ -->
<div id="wrapper">
<h1>Modal Dialog Example</h1>
<p><a href="#search" class="openModal">Open Modal</a></p>
<div id="search" class="modal" aria-label="Search" tabindex="-1">
<form id="frmSearch" action="/search" method="post">
<label for="query">Enter search terms:</label>
<input name="query" id="query" type="text" size="35" />
<button type="submit">Search</button>
</form>
<div><button class="close">Close</button></div>
</div>
<div id="mask"></div>
<h2>More</h2>
<ul>
<li><a href="CSUN14-Modal-Dialog.pptx">Related CSUN 14 powerpoint</a></li>
<li><a href="modal_demo2.html">Modal demo 2</a></li>
<li>Return to <a href="https://weboverhauls.github.io/demos/">Web Overhauls Demos</a></li>
</ul>
</div>
<!-- jQuery hosted by Google -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
/* ****************************
MODAL LIGHTBOX
**************************** */
// Initialize (these roles can confuse AT user if JS not running)
$(".openModal").attr("role","button");
$(".modal").attr("role","dialog");
$(".modal").css("display","none");
// Function to resize and reposition the modal window
function resizeModal(id) {
// Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
// Position the modal window to center
$(id).css('top', winH/2-$(id).height()/2-50);
$(id).css('left', winW/2-$(id).width()/2-20);
}
// Function to resize mask (grey background)
function resizeMask() {
// Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
// Set height and width to mask to fill up the whole screen
$('#mask').css('width',maskWidth);
$('#mask').css('height',maskHeight);
}
$('a[class=openModal]').click(function(e) {
e.preventDefault();
// Get modal containers
var id = $('.modal');
// Remember what opened me to focus when closing
var lastFocus = $(this);
// Set size of mask to size of screen
resizeMask();
// Transition effect - mask
$('#mask').fadeIn(300);
// Set size and position modal
resizeModal(id);
// Transition effect and focus the modal
$(id).fadeIn(300);
$(id).focus();
// Call function to maintain focus within the modal
var obj = $(this).attr('href').replace("/","");
obj = (obj.replace("#",""));
var el = document.getElementById(obj);
keepFocus(el);
// Close - if close button is clicked
$('.modal .close').click(function (e) {
e.preventDefault();
$('#mask, .modal').hide();
lastFocus.focus();
});
// Close - Escape key
$(document).on('keydown', function (e) {
if (e.keyCode === 27) { // ESC
$('#mask, .modal').hide();
lastFocus.focus();
}
});
});
// Resize modal when window resized
$(window).resize(function () {
var id = $('.modal');
// Resize the mask
resizeMask();
// Call function to resize and reposition modal
resizeModal(id);
});
// Maintain focus within modal
var tabbableElements = 'a[href], area[href], input:not([disabled]),' +
'select:not([disabled]), textarea:not([disabled]),' +
'button:not([disabled]), iframe, object, embed, *[tabindex],' +
'*[contenteditable]';
var keepFocus = function (context) {
var allTabbableElements = context.querySelectorAll(tabbableElements);
var firstTabbableElement = context;
var lastTabbableElement = allTabbableElements[allTabbableElements.length - 1];
var keyListener = function (event) {
var keyCode = event.which || event.keyCode; // Get the current keycode
// Polyfill to prevent the default behavior of events
event.preventDefault = event.preventDefault || function () {
event.returnValue = false;
};
// If it is TAB
if (keyCode === 9) {
// Move focus to first element that can be tabbed if Shift isn't used
if (event.target === lastTabbableElement && !event.shiftKey) {
event.preventDefault();
firstTabbableElement.focus();
// Move focus to last element that can be tabbed if Shift is used
} else if (event.target === firstTabbableElement && event.shiftKey) {
event.preventDefault();
lastTabbableElement.focus();
}
}
};
context.addEventListener('keydown', keyListener, false);
};
</script>
</body>
</html>