Navigate to the hover image with a swift roll

I have been searching extensively on the web but unfortunately, I haven't come across any relevant links. My goal is to create a customized menu.

The menu should be an area-based rollover menu that transitions based on the section of the image being hovered over by the user. The image and associated link should change based on the specific area of the image being hovered over. Take a look at the image below:

Upon clicking, the next image will appear as shown below,

Lastly, when hovering over a particular slice of pizza, the image displayed should change accordingly. An example can be seen in the image below:

Could this functionality be achieved using CSS3 and JavaScript?

Answer №1

There are two different methods that come to mind. As you have tagged JQuery, I assume that is acceptable. Using pageX and pageY allows you to track mouse movements on the screen and determine the specific area being hovered over. Check out this fiddle I created to see how the coordinates can be determined: http://jsfiddle.net/cpk3mqxw/

$("#image").mousemove(function (e) {
    mousex = e.pageX;
    mousey = e.pageY;
    $("#update").text("x: " + mousex + " y:" + mousey);
});

For checking the coordinates, visualize the page as a grid system in JavaScript. For example, if mousex is between 132 and 155, it indicates a particular part of the image is being hovered over. Having a separate span tag showing updated x and y coordinates will be useful. For instance:

if(mousex >= 102 && mousex <= 220 && mousey >= 26) {
    // do something
}

While it may require additional effort, it proves beneficial in the end. Another approach is using an image map.

<img src="image.jpg" id="image1" usemap="#one">
<map name="one">
<area coords="30, 50, 70, 100" href="gohere.html">
</map>

If you want to highlight or style the area, consider using maphilight.js for more information:

Answer №2

If you're looking for a clever solution to your issue, consider creating a sprite that features an image of a pizza with one slice missing in each variation. Next, set up a div element to match the size of one pizza image:

   <div id="myPizza"></div>

Using a script like the one found at , you can track the X and Y coordinates of the mouse hovering over this div.

Based on where the cursor is positioned on the image, you can apply a specific class with the appropriate background position. For generating these classes, I recommend using .

I trust that this information proves useful to you—employing sprites in conjunction with a straightforward script!

Answer №3

The solution to the problem has been successfully found.

To tackle this issue, I utilized <map> and <area> tags to identify each section (pizza slices) required. Subsequently, I integrated an event handler for onclick() and onmouseover() on each area to trigger a function that alters the image.

HTML

<area shape="circle" coords="200,250,25" onmouseover = "changePic('newImage.png')" />
<img src='start_pic.png' name='mainPic' />

JavaScript

function changePic(img){
   mainPic.src = img;
}

PREVIEW LINK

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with geocoding to retrieve a list of terrestrial coordinates

I am currently working on developing a functionality that can return an array of land-based coordinates. I have utilized Google's geocoder to create individual land-based coordinates successfully, but now I want to implement it in a way where an array ...

Effortless code formatting with VS Code for TypeScript and JavaScript

Does anyone know of any extensions or JSON settings that can help me format my code like this: if(true) { } else { } Instead of like this: if(true){ } else { } ...

Please ensure not to include any duplicate elements in the array

function findNeighbors(color_indices) { var neighbor_face = []; var temp_indexes = []; //initializing to specified length var len_color_indices = color_indices.length; for (var i = 0; i < len_color_indices; i++) { if (c ...

Is it possible to generate a form dynamically in a Vue.js HTML file based on JSON data? Check out the provided f

Currently, I am diving into the world of vue.js and experimenting with building a dynamically created form. To explain further, I have JSON files that define what input types should be included in the form; now I am figuring out how to implement this usin ...

Difficulties arise when trying to pass all data inputted on a form using formData in conjunction with the fetch API

Why is my formData appearing empty when sent to my express+mongodb server? I'm having some issues with querySelector and addEventListener, but for now that's manageable. However, I am struggling to find a way to successfully send all the values o ...

Simulating npm package with varied outputs

In my testing process, I am attempting to simulate the behavior of an npm package. Specifically, I want to create a scenario where the package returns a Promise that resolves to true in one test and rejects with an error in another. To achieve this, I hav ...

Visualizing JSON data in React.js using Chart.js

Currently, as a beginner in ReactJS, I am working on an application that displays COVID-19 data from a JSON API in a visual format using Chart.js. However, despite trying various solutions, I am unable to visualize the data. Below is my source code: App ...

Issue with Bootstrap 5 Carousel not transitioning between slides

I am trying to add a carousel to my webpage, but it seems like it's not sliding and only displaying the first image. I've double-checked my code and everything else from Bootstrap works fine. Here's the snippet of code I'm using: < ...

Embed numerous HTML pages within a designated DIV by utilizing a list of hyperlinks

I'm currently facing an issue and can't seem to figure out the solution. I am looking for a way to achieve this without using PHP or JAVA as I am not proficient in those languages yet. The scenario is that I have 18 products/links and a single d ...

What methods can I use to prevent jquery.address from modifying the URL in specific scenarios?

I am utilizing a plugin called jquery.address to manage browser history states on my ajax-heavy website. However, there is a specific scenario where I need to prevent the $.address.change() function from being triggered by a certain link. For instance, in ...

Transform your image using a stunning zoom-in effect

Currently, I am experimenting with a feature for my website where I want images to switch upon hover. While I have successfully achieved the image switch on hover, I am looking to create smooth transitions between the switches, with the second image being ...

The watch functionality is failing to work within a specialized attribute directive that is being utilized within a separate custom element directive

I'm currently working with two directives that handle separate functionalities. I am attempting to utilize the attribute directive within an element directive, but I'm encountering issues with the watch function not working on the attribute direc ...

Is Protractor compatible with Internet Explorer 9?

For my Angular App that is running on IE9, I need to create end-to-end acceptance tests. I'm curious to know if the browser simulated by Protractor matches the behavior of IE9 or a newer version? ...

Rails Navigation Issue: JQuery Confirmation Not Functioning Properly

Having a Rails app, I wanted to replicate the onunload effect to prompt before leaving changes. During my search, I came across Are You Sure?. After implementing it on a form, I noticed that it only works on page refreshes and not on links that take you a ...

The color of the last clicked DIV is supposed to stay permanent, but for some unknown reason

I'm attempting to replicate this design in my project: http://jsfiddle.net/AYRh6/26/ However, I am facing issues with it and cannot pinpoint the exact problem in the code. I am using code for a toggle effect. Any assistance would be greatly appreciat ...

Turn off PrettyPhoto functionality in the code base

Is there a way to deactivate PrettyPhoto once it has been activated? $(document).ready(function () { $("a[rel^='prettyPhoto']").prettyPhoto(); } $("#disablePrettyphoto").click(function (e) { $("a[rel^='prettyPhoto']").KILLPRET ...

Ways to determine if the popupState component in Material UI is currently opened

I am currently utilizing PopupState and the Popover component to create a specific element. It is functioning properly, but I have a requirement to modify the icon displayed based on whether the popup is open or closed. Here is the code for my component: ...

The final row continues to display the <div> element

Currently, I am in the process of learning PHP/MySQLi and there is something I need assistance with that I haven't been able to find a solution for online. In the forum script that I am developing, I have been using <div class="hr"> </div> ...

iOS datetime-local height adjustment

I have added a datetime-local input field to my website that is accessed through my mobile phone. However, I am facing an issue where the datetime-local field is positioned higher than the other textboxes in the row. This creates an awkward offset, as sh ...

Creating a bootstrap form field that spans 100% in width:

I am encountering an issue with two column divs in a row, each containing two text fields. When resizing on mobile, the width of the textbox is not expanding to 100% and looks unattractive. Below is my Bootstrap code, but strangely, the width looks fine f ...