Moving icon that appears when hovering over a menu button

Before diving into this, please take a moment to visit the following website to understand my goal:

You'll notice there is a noticeable RED arrow positioned below the menu. What I aim to accomplish is... when I hover over a menu button, the arrow smoothly transitions to that same button without any page reload.

My ideal scenario would be for the arrow to revert back to its original position once I'm no longer hovering over any buttons. Additionally, the default button should change if a different menu button is clicked on.

If you have any examples or resources to share, I would truly appreciate it!

Thank you for your attention,

Kerry x

Answer №1

One issue to address is an incorrect DOCTYPE declaration.

<!DOCTYPE html PUBLIC "">

This is causing your page to load in quirk mode. Correct it to

<!DOCTYPE html>

for HTML5 or use the complete version with FSI & FPI included.

Another point to consider is the use of a <table> for navigation. While not necessarily a problem, using ul is more common.

When styling the :hover effect, a simple approach would be:

#MenuPosition table tbody tr td:hover
{
background-image: url("/images/Arrow.jpg");
}

You may need to adjust paddings and margins, or utilize display: block or display: inline-block to position the arrow correctly.

Answer №2

Convert the "buttons" into anchors. Use CSS to create a rule for :hover in order to set a background image with an arrow.

There are numerous resources available for learning CSS techniques, such as browsing through tutorials on sites like Nettuts and Webdesigntuts, which offer a plethora of navigation articles. Alternatively, if you're comfortable with reverse engineering, analyze a website you admire and dissect its source code to understand how they achieved their design.

Note that using javascript is not essential for achieving your goal. Unless you require specific animations, CSS can handle most tasks effectively; I personally believe that relying solely on CSS is the superior approach.

Answer №3

CSS ONLY SOLUTION

Take a look at this response.

Can you hover over one element and impact another element?

One possibility could be:

#someelement {
    padding: 0;
}
.classhovered:hover + #someelement {
    padding-top: 15px;
    padding-left: 15px;
}

This works if they are siblings within the same parent container.

Answer №4

Adjust the arrow position using the margin-left CSS property in relation to the left side of the td. DEMO

$("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});

To achieve this, include the jQuery library in the head section of your webpage.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Add this code to an external JavaScript file and include it in the head section of your page.

$(function(){
  $("#MenuPosition").on("hover","td",function(){
    $("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});

  });

});

EDIT : To reset the arrow to its original position, use the following:

$(function(){
  currentPos = $("#Arrow").css("margin-left");

  $("#MenuPosition").on("hover","td",function(){
    $("#Arrow").css({"margin-left":$(this).position().left});

  });


   $("#MenuPosition").on("mouseout","td",function(){

     $("#Arrow").css({"margin-left":currentPos});        
  });

});

NOTE : PLEASE REVIEW THE CALCULATIONS AND MAKE NECESSARY CORRECTIONS.

PS: Unable to correct now as I am logging out from office ;) . but I believe you have understood the logic to make the corrections

Answer №5

Here's a neat trick you can use:

To add a background arrow below the navigation/menu links in your HTML, you can utilize a span element like so:

<ul class="menu">
    <li>
        <a href="#">Home</a>
        <span class="arrow">&nbsp;&nbsp;</span>
    </li>
    <li>
        <a href="#">About</a>
        <span class="arrow">&nbsp;&nbsp;</span>
    </li>
</ul>

The corresponding CSS styles are as follows:

.menu {
    font-size: 16px;
    list-style: none;
    margin: 0;
    padding: 0;
}

.menu li {
    background: #ccc;
    display: inline-block;
    padding: 10px;
    position: relative;
}

.menu li a {
    color: #333;
    text-decoration: none;
}

.arrow {
    background: url("images/arrow.png") no-repeat center bottom;
    display: none;
    height: 20px;
    width: 20px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

Finally, the JavaScript code that brings it all together is:

$(document).ready(function(){     
    AddArrowToMenu();
});

function AddArrowToMenu(){

    $(".menu li").hover(function(){
            $(this).find('.arrow').fadeIn(200);
        }, function(){
            $(this).find('.arrow').fadeOut(200);
    });

}   

Check out this website for a live example:

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

Why is it that one of my useQuery hooks is returning a value while the other one is returning undefined?

I'm currently facing an issue with React Query where one of my useQuery hooks is logging undefined while the other one is displaying the correct data. Both functions are async and perform similar tasks. I've been troubleshooting this problem for ...

Converting HTML into an image in a Node.js Buffer

Can you suggest a simple method to convert a raw HTML string, like the following: <b>Hello World</b> into a regular PNG image in Node.js Buffer form? I've been exploring various npm packages for a solution that supports this functionali ...

Interacting with an API and retrieving data using JavaScript

I have hit a roadblock. This question might be simple, but I am struggling to figure it out. I am attempting to retrieve a response from an API (mapquest), but I can't seem to navigate the response to extract the necessary information. Here is my con ...

What is the reason behind having 3 watchers for 1 binding in AngularJS?

Take a moment to view the screenshot provided below In the screenshot, it is evident that there are #3 watchers for a single binding. Would someone care to explain the reason behind this? P.S: I am utilizing AngularJS Batarang to assess performance. ...

Alert: The comment index is not defined on line 5 of the comment.php file located in C:xampphtdocsindex

There is a piece of code I am struggling with: <?php if($_POST){ $name = $_POST['name']; $content = $_POST['comment']; $handle = fopen("comments.html","a"); fwrite($handle,"<b>" . $name . "</b><br ...

Alexa Skill: Successful with the initial query but fails with the subsequent one

An issue has been identified with the Alexa skill where it works for the first question but not the second one. The skill involves a jumbled letters quiz utilizing the following array: var arr = [{"Q":"dcha","A":"chad"},{"Q":"goto","A":"togo"},{"Q":"alim" ...

What are some creative ways to customize the background of a full component?

Is there a way to modify the background color of an entire component? I want the color to cover the full width and height of the component. I attempted using the body tag, but it was ineffective. Do I need to enclose all components in a div and then appl ...

Employing an item as a function

My query pertains to utilizing an express application object (app object) as a callback function. The express application is known for its methods and properties that aid in handling HTTP requests. When integrating the app object with an HTTP server, it se ...

steps for setting up babel-cli and babel-preset-react

I attempted various methods of installing babel-cli babel-preset-react Here's what I tried: npm install --save-dev babel-cli babel-preset-react However, when I run babel -h An error message appears saying The program 'babel' can be found ...

Using Selenium in conjunction with browsermob-proxy to generate new HAR files for redirected web pages

Another interesting scenario I have encountered involves combining Selenium with browsermob-proxy: A new Har is created for the initial page access The initial request can be redirected multiple times And then further redirected by JavaScript For exampl ...

Error received - CORS request denied on Firefox browser (Ubuntu)

I encountered a CORS error (CORS request rejected: https://localhost:3000/users) while attempting to register a new user. This issue arose from content in the book Building APIs with node.js, Chapter 12. I am currently using Firefox on Ubuntu and have tr ...

Require this form to be centered flawlessly

<form class="form-horizontal" role="form" id="contactf" action="http://titan.csit.rmit.edu.au/~e54061/wp/form-tester.php" method="post"> <div class="form-group"> <label class="control-label col-sm-2 lb" for="email">Email : </ ...

"Resolving the Issue of jQuery Displaying NaN Value

I've got the code below, and it's all working smoothly. However, I'm encountering an issue where jQuery is returning $NaN when I try to run it through my WordPress theme using the format shown here: jQuery: jQuery(document).ready(function( ...

Turn off the nicescroll scroll bar that appears as a default feature

Is there a way to disable the nicescroll scroll bar that appears in red by default on my html page? It's causing issues with zooming in and breaking the layout. ...

What is the best way to ensure that two divs in the same row float in opposite directions?

Check out this JSFiddle to see what I have done so far: JSFiddle Link html <div class="results"> <h2>Some data</h2> <ul style="margin:0;padding:0;"> <li class="resultsListItem" style="list-style-type: none;"> ...

Steps to display a single entry in a Laravel/Vue implementation

My goal is to combine Laravel with Vue and eventually Nuxt in order to incorporate dynamic page transitions similar to the ones showcased on into my websites. I recently followed a tutorial on using Vue with Laravel at https://scotch.io/tutorials/build-a ...

Creating optimized CSS builds for Next.js production

In dev mode, I have separate Custom CSS files for different layouts which work fine. However, when I publish my app in production mode, Nextjs combines all the CSS files together causing issues with my layouts. ...

Learn how to effortlessly update models by integrating AngularJS with Django and Django Rest Framework

Here is a JSON representation of a post based on its ID: http://127.0.0.1:8000/update/1?format=json {"title": "about me", "content": "I like program", "created": "2014-11-29T18:07:18.173Z", "rating": 1, "id": 1} I am attempting to update the rating ...

JS: Issue with iterating over objects

Within my JavaScript code, there exists an object named box_object with the following structure: ({ id:"3", text:"this is a box object", connection_parent:["1", "2"], connection_child:["5", "6"], connectiondata_child:{ 0:{id:"5", ...

The function .classList.remove() is effective when applied to one element, but fails to work on a different element

I am facing an issue where only one element is getting affected when trying to remove classes from multiple elements if certain email input requirements are met. Can someone help me understand why this is happening? Here is the code snippet: const emailI ...