forked from lsvekis/JavaScript-Exercises-Book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractive Periodic Table
More file actions
78 lines (78 loc) · 2.67 KB
/
Interactive Periodic Table
File metadata and controls
78 lines (78 loc) · 2.67 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Periodic Table</title>
<style>
.element {
border: 1px solid black;
width: 50px;
height: 50px;
text-align: center;
line-height: 50px;
float: left;
margin: 2px;
cursor: pointer;
}
.element:hover {
background-color: lightgray;
}
#info {
clear: both;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="periodic-table">
<!-- Element Blocks will be dynamically added here -->
</div>
<div id="info">
<!-- Detailed Information will be displayed here -->
</div>
<script>
// Sample element data for demonstration
const elements = [
{ symbol: "H", name: "Hydrogen", atomic_number: 1, atomic_mass: 1.008 },
{ symbol: "He", name: "Helium", atomic_number: 2, atomic_mass: 4.0026 },
// Add more elements here
];
// Function to generate HTML for element blocks
function createElementBlock(element) {
return `<div class="element" data-symbol="${element.symbol}"
data-name="${element.name}"
data-atomic_number="${element.atomic_number}"
data-atomic_mass="${element.atomic_mass}">
${element.symbol}
</div>`;
}
// Function to display detailed information about an element
function showElementInfo(element) {
const infoDiv = document.getElementById('info');
infoDiv.innerHTML = `
<h2>${element.name} (${element.symbol})</h2>
<p>Atomic Number: ${element.atomic_number}</p>
<p>Atomic Mass: ${element.atomic_mass}</p>
`;
}
// Generate element blocks and append them to the periodic table
const periodicTable = document.getElementById('periodic-table');
elements.forEach(element => {
const elementBlock = createElementBlock(element);
periodicTable.innerHTML += elementBlock;
});
// Event listener for element blocks
const elementBlocks = document.querySelectorAll('.element');
elementBlocks.forEach(element => {
element.addEventListener('click', function () {
const symbol = this.getAttribute('data-symbol');
const name = this.getAttribute('data-name');
const atomic_number = this.getAttribute('data-atomic_number');
const atomic_mass = this.getAttribute('data-atomic_mass');
const clickedElement = { symbol, name, atomic_number, atomic_mass };
showElementInfo(clickedElement);
});
});
</script>
</body>
</html>