I am trying to build a single page website using beginner friendly html, css, and javascript code. The website is for a map of a school campus which has a height of 1169px and a width of 856px. I want the map to be interactive in the sense that the user can hover over each individual building on the map and each building will be highlighted on mouseover. I also want the map to be interactive in the sense that the user can click on each individual building to bring up an on the right side of the page that can be filled with more detailed information, images, and attached links for each building. There are 13 buildings.
Please give me detailed instructions in the form of a step-by-step tutorial of how to complete this project.
Assistant
User
please continue from where you left off starting from Step 3:
Assistant
Sure, I can help you with that. Here's a step-by-step tutorial on how to build an interactive single page website for a school campus map using beginner-friendly HTML, CSS, and JavaScript code.
Step 1: Set up your HTML file
Create a new HTML file and add the following code:
In this code, we have added an image of the school campus map and defined clickable areas for each building using the area tag. We have also added an aside element to the right side of the page where we will display detailed information about each building.
In this code, we have defined the styles for the container, building-info, and area-highlight classes. We have set the position of the container to relative and the position of the building-info to absolute so that it is positioned to the right side of the page. We have also set the width and height of the building-info element and added a background color and padding to make it look like a sidebar. We have set the display property of building-info to none so that it is hidden by default. Finally, we have defined the styles for the area-highlight class which will be used to highlight the clickable areas on mouseover.
Step 3: Add JavaScript code
Create a new JavaScript file and add the following code:
js
const areas = document.getElementsByTagName('area');
const buildingInfo = document.getElementById('building-info');
for (let i = 0; i < areas.length; i++) {
const area = areas[i];
area.addEventListener('mouseover', function () {
area.classList.add('area-highlight');
});
area.addEventListener('mouseout', function () {
area.classList.remove('area-highlight');
});
area.addEventListener('click', function (event) {
event
Sure, here is the continuation of Step 3:
Step 3: Add JavaScript code (continued)
js
const areas = document.getElementsByTagName('area');
const buildingInfo = document.getElementById('building-info');
for (let i = 0; i < areas.length; i++) {
const area = areas[i];
area.addEventListener('mouseover', function () {
area.classList.add('area-highlight');
});
area.addEventListener('mouseout', function () {
area.classList.remove('area-highlight');
});
area.addEventListener('click', function (event) {
event.preventDefault();
const buildingName = area.getAttribute('alt');
const buildingDesc = "Building " + (i+1) + " Description.";
const buildingImage = "building-" + (i+1) + ".jpg";
const buildingLinks = [
{ text: "Link 1", href: "#" },
{ text: "Link 2", href: "#" },
{ text: "Link 3", href: "#" },
];
// Update building-info element with new content
buildingInfo.innerHTML = `
<h2>${buildingName}</h2>
<p>${buildingDesc}</p>
<img src="${buildingImage}" alt="${buildingName}" />
<ul>
${buildingLinks.map(link => `<li><a href="${link.href}">${link.text}</a></li>`).join('')}
</ul>
`;
// Show building-info element
buildingInfo.style.display = 'block';
});
}
// Hide building-info element on click outside
document.addEventListener('click', function (event) {
if (!event.target.closest('#building-info') && !event.target.closest('area')) {
buildingInfo.style.display = 'none';
}
});
In this code, we have first selected all the area elements and the building-info element using their IDs and tags respectively. We have then added event listeners for mouseover, mouseout, and click events on each area element. On mouseover event, we add the area-highlight class to the element to highlight it. On mouseout event, we remove the area-highlight class to unhighlight it. On click event, we prevent the default behavior of the link and get the building name, description, image, and links using the getAttribute() method and hard-coded values for this example.
We then update the content of the building-info element with the new values using template literals and the map() method to create the list of links dynamically. Finally, we set the display property of building-info to block to show it on the page.
We also add an event listener to the document object to hide the building-info element when the user clicks outside of it or any area element using the closest() method.
Step 4: Add images and links
Add images for each building with filenames building-1.jpg, building-2.jpg, etc. and add links for each building as desired.
Step 5: Test the website
Save all your files and open the HTML file in a web browser to test the website. Hover over each building to highlight it and click on it to display the detailed information in the building-info element on the right side of the page.
That's it! You have now successfully created an interactive single page website for a school campus map using beginner-friendly HTML, CSS, and JavaScript code.