forked from eracom/exemples-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.html
124 lines (91 loc) · 2.81 KB
/
window.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
<!doctype html>
<html>
<head>
<title>window</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="jquery-3.1.1.min.js"></script>
<style>
body {
font-family: "Roboto Mono";
margin: 0;
}
#container {
box-shadow: 0px 0px 0px 10px cyan inset ;
height: 100vh;
}
#jscontainer {
box-shadow: 0px 0px 0px 20px rgba(255, 160, 0, 0.5) inset;
height: 100px;
}
ul {
margin: 0;
}
.values {
position: relative;
top: 20px;
width: 70%;
}
.center,
.bottom-right {
position: absolute;
padding: 0.5em;
background: yellow;
line-height: 1
}
.center {
top: calc(50vh - 1.5em);
left: calc(50vw - 5em);
width: 10em;
}
.bottom-right {
right: 0px;
bottom: 0px;
}
</style>
</head>
<body>
<div id="container">
<div id="jscontainer">
<div class="values">
<ul>
<li>La hauteur de la fenêtre, selon JavaScript: <span class="screenh"></span></li>
<li>La hauteur de #container, selon jQuery: <span class="containerh"></span></li>
</ul>
</div>
<div class="center">
positionné au centre avec calc(50vw - 5em)
</div>
<div class="bottom-right">
placé en bas à droite
</div>
</div>
</div>
<script>
function mesurerFenetre() {
// obtenir la hauteur de la fenêtre avec JavaScript:
var jsWinHeight = window.innerHeight;
// afficher ce chiffre:
document.querySelector(".screenh").innerHTML = jsWinHeight+'px';
// appliquer cette hauteur à #jscontainer
document.getElementById("jscontainer").style.height = jsWinHeight+'px';
// obtenir la hauteur de #container avec jQuery
var jqWinHeight = $('#container').height();
// afficher ce chiffre:
$('.containerh').html(jqWinHeight+'px');
// repositionner .center précisément:
var centerHeight = $('.center').height();
var centerPos = (jqWinHeight /2) - (centerHeight /2);
$('.center').css({ 'top' : centerPos+'px' });
}
// exécuter la fonction au chargement:
window.onload = mesurerFenetre;
// exécuter la fonction au redimensionnement:
window.onresize = mesurerFenetre;
// note:
// sur un iPad vertical, la fenêtre selon javascript (window.innerHeight) est de 928 pixels.
// mais la hauteur de #container est de 985 px.
// la différence (57 px) peut s'expliquer par la hauteur de la barre de navigation.
</script>
</body>
</html>