-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoggle.html
More file actions
57 lines (50 loc) · 1.58 KB
/
toggle.html
File metadata and controls
57 lines (50 loc) · 1.58 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Model: toggle</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Model: toggle</h1>
<hr />
<p>
A model is dynamically added or removed from the document when the button is clicked.
</p>
<p>
<button type="button" id="toggle">Add model</button>
</p>
<div id="model-slot"></div>
</div>
<template id="model-template">
<model alt="A model of a teapot">
<source src="./teapot.usdz" type="model/vnd.usdz+zip" />
<source src="./teapot.glb" type="model/gltf-binary" />
<img
src="./teapot-fallback-black.jpg"
alt="An image of a model of a teapot" />
</model>
</template>
<script type="module">
const slot = document.getElementById('model-slot');
const button = document.getElementById('toggle');
const template = document.getElementById('model-template');
let modelEl = null;
const toggleModel = () => {
if (modelEl) {
modelEl.remove();
modelEl = null;
button.textContent = 'Add model';
} else {
slot.innerHTML = template.innerHTML;
modelEl = slot.querySelector('model');
button.textContent = 'Remove model';
}
};
button.addEventListener('click', toggleModel);
</script>
</body>
</html>