-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
112 lines (99 loc) · 3.94 KB
/
script.js
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
// Functions for index.html
// Show image near cursor on hover
function showImage(image) {
const hoverImage = document.getElementById('hover-image');
hoverImage.src = `images/${image}`;
hoverImage.style.display = 'block';
// Position the image near the cursor with slight offset
hoverImage.style.top = `${event.clientY + 10}px`;
hoverImage.style.left = `${event.clientX + 10}px`;
}
// Hide the hover image
function hideImage() {
const hoverImage = document.getElementById('hover-image');
hoverImage.style.display = 'none';
}
// Open Discovery by redirecting to discoveries.html with hash
function openDiscovery(title) {
// Map title to corresponding discovery ID
const titleToIdMap = {
"Galileo Telescope": "galileo",
"Penicillin": "penicillin",
"Discovery of Electricity": "electricity",
"Theory of Relativity": "relativity",
"DNA Structure": "dna",
"The Internet": "internet",
"Quantum Computing": "quantum-computing" // Add mapping for the 7th item
};
const id = titleToIdMap[title];
if(id) {
window.location.href = `discoveries.html#${id}`;
} else {
alert(`More information about: ${title}`);
}
}
// Smooth scroll for navigation links within the same page
document.querySelectorAll('nav a').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const href = this.getAttribute('href');
if (href.startsWith('#')) {
e.preventDefault();
const targetId = href.substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth' // Smooth scroll effect
});
}
}
});
});
// Modal Functionality for discoveries.html
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById("myModal");
const modalTitle = document.getElementById("modal-title");
const modalDescription = document.getElementById("modal-description");
const modalFacts = document.getElementById("modal-facts");
const modalImage = document.getElementById("modal-image");
const closeModal = document.getElementsByClassName("close")[0];
// Function to open modal with specific discovery details
function openModal(title, description, facts, image) {
modalTitle.textContent = title;
modalDescription.textContent = description;
modalFacts.textContent = facts;
modalImage.src = image;
modal.style.display = "block";
}
// Add click event to each discovery card
document.querySelectorAll('.discovery-card').forEach(card => {
card.addEventListener('click', function() {
const title = this.getAttribute('data-title');
const description = this.getAttribute('data-description');
const facts = this.getAttribute('data-facts');
const image = this.getAttribute('data-image');
openModal(title, description, facts, image);
});
});
// Close modal when user clicks on <span> (x)
closeModal.onclick = function() {
modal.style.display = "none";
}
// Close modal when user clicks anywhere outside of the modal
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
// Open modal based on URL hash
const hash = window.location.hash.substring(1); // Remove the #
if(hash) {
const card = document.getElementById(hash);
if(card) {
const title = card.getAttribute('data-title');
const description = card.getAttribute('data-description');
const facts = card.getAttribute('data-facts');
const image = card.getAttribute('data-image');
openModal(title, description, facts, image);
}
}
});