Problem with hover event used on <a> and <li> tags [solved]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Problem with hover event used on <a> and <li> tags [solved]

Post by Ark »

Hey guys ive got a strange issue with css and some jquery script(maybe it's just the html structure)...
this is what's happening:

When I hover the <a href="#"><li></li></a> shiit happens:

Code: Select all

<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
Image

And this is what should happen:
Image

It does works like that when i'm hovering the

Code: Select all

<li><li>
<li><li>
<li><li>
<li><li>
<li><li>
this is the hover function in jquery:

Code: Select all

$("#inner-navbar ul li").hover(function(){
                        var $index=$(this).index();
                        var $width=99*$index;
                        var $height=75;
                        $(this).css("background","url(images/navbar-sprites.png) -"+$width+"px -"+$height+"px");
                    },function(){
                    var $index=$(this).index();                        
                        var $width=99*$index;
                        var $height=0;
                    $(this).css("background","url(images/navbar-sprites.png) -"+$width+"px -"+$height+"px");
                    });
IDK WTH is happening so could someone please guide me the correct way



Is there like a css rule that should be applied for not happening this?
Thanks for reading my whining!
Last edited by Ark on Tue Feb 28, 2012 1:39 am, edited 1 time in total.
Orgullo Catracho
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Problem with hover event used on <a> and <li> tags

Post by Winawer »

In your hover function, the index never changes because <li> is always the first (and only) child of <a>.
Ark wrote:

Code: Select all

<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
<a href="#"><li></li></a>
I don't think that's allowed. It should be <li><a href="#"></a></li>.
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Problem with hover event used on <a> and <li> tags

Post by Ark »

OOOooohh yea that explains that.

The problem nesting the <a> inside <li> is that clicking it doesn't happens nething, it's like clicking just the <li> el. :x

Do you got another alternative?

Thanks by the way!
Orgullo Catracho
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Problem with hover event used on <a> and <li> tags

Post by Chris »

There is no other alternative. The way Winawer showed you is the way it is done and always has been done. Placing the anchor tag around a list item is not only incorrect markup but to my mind is unlogical. You have a list of links. Not a link of lists.

Is the whole navigation stored on one image?

Here's some really simple css:

Code: Select all

ul li {
    float: left;
    list-style: none;
}

ul li a {
    display: block;
    width: 100px;
    height: 75px;
}

ul li a:hover {
    background-position: 0px -75px;
}
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Problem with hover event used on <a> and <li> tags

Post by Ark »

Yes it's just one image. Thanks chris now it's working as it's supposed to, didn't knew I had to set the dimensions of the <a> also. :)
Orgullo Catracho
Post Reply

Return to “Advanced Help and Support”