简体   繁体   中英

Icon to move when hovering over a menu button

Before you read this please get up this website to see what I am trying to do:

https://www.kris-willis.com

As you can see there is a RED arrow located below the menu and what it is that I'm trying to achieve is... when I hover over a menu button the arrow moves to the same button I'm hovering over without reloading the page.

Ideally I'd like the arrow to move back to a default button.. and also for the default button to change if clicked on a different menu button.

If you know any links to examples etc... I would really appreciate it!

Thank you for your time,

Kerry x

Make the "buttons" anchors. Using css set create a rule for :hover to set a background image that contains the arrow.

There are plenty of CSS tutorials out there, Nettuts and Webdesigntuts have a lot of navigation articles. Or if you are comfortable with emulating others, find a site you like and pick apart the source until you figure out how they did it.

Keep in mind that javascript is not at all necessary to accomplish what you are doing. Unless you want some animations, and even then CSS can handle most of that work, pure CSS in my opinion is the better approach.

Just move the arrow by margin-left with respect to left of the td DEMO

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

Tp do this Add jQuery libirary to the head section of your page

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

Add this code in a external js file and add it to head section of your page

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

  });

});

EDIT : For restoring the arrow orignal position use

$(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 SEE THE CALCULATION PART AND CORRECT IT.

PS: cant correct is because its my log out time from office ;) . but i thing you got the logic to do it

The first thing is that you have a wrong DOCTYPE.

<!DOCTYPE html PUBLIC "">

This causes you page to load in quirk mode. Change it to

<!DOCTYPE html>

for HTML5 or use the complete one including the FSI & FPI.

Second is you are using a <table> for navigation. Nothing seriously wrong with it but people tend to use ul

For the :hover , you can simply use

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

You might have to play with paddings and margins or maybe use display: block or display: inline-block to position the arrow correctly.

You can do something like this:

Using a span to add the bg arrow below the nav/menu lis in the HTML:

<ul class="nav">
    <li>
        <a href="#">Menu 1</a>
        <span class="arrow">&nbsp;&nbsp;</span>
    </li>
    <li>
        <a href="#">Menu 2</a>
        <span class="arrow">&nbsp;&nbsp;</span>
    </li>
</ul>

The CSS:

.nav {
    font-size: anypx;
    list-style: none outside none;
    margin: 0;
    padding: 0;
    position: relative;
}

.nav li {
    background: #whatev;
    display: block;
    float: left;
    height: anypx;
    line-height: anypx;
    padding: 0;
    text-align: center;
}

.nav li a {
    color: #any;
    display: block;
    padding: any;
    position: relative;
    text-decoration: none;
    width: auto;
}
.arrow {
    background: url("images/arrow.png") no-repeat scroll 0 9px transparent;
    display: none;
    height: anypx;
    text-indent: -9999px;
    width: whatevs;
    z-index: 9999;
}

And Finally the JS/Jquery that makes it work:

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

function Your_menu(){

    $(".nav li").hover(function(){
            $(this).find('.arrow').css({visibility: "visible",display: "none"}).show();
        },function(){
            $(this).find('.arrow').css({visibility: "hidden"});
    });

}   

Here is a site that is showing this :)

http://www.drexelmedicine.org/

PURE CSS SOLUTION

Check this answer.

Is there any way to hover over one element and affect a different element?

So it might be:

#thething {
    margin: 0;
}
.classone:hover + #thething {
    margin-top: 10px;
    margin-left: 10px;
}

If they're adjacent siblings in a parent div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM