Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jquery animate background color on mouseover

Posted on 2012-03-22
8
Medium Priority
?
430 Views
Last Modified: 2012-04-08
Hello Experts,

I have a menu in my site using UL LI tags (horizontal).
In the CSS i am using the a:hover where the background of the menu item changes to green color.

I am trying to use Jquery animation to animate the effect so the green background starts from left to right.

I tried this script below but unfortunately it moves the entire menu item.
I would apprentice your direction or if you can point me to an online example.

Thank you.

$(document).ready(function() {
	$("#menu a").mouseover(function() {
	  $(this).animate({"left": "50px"}, 1000); 
   });	
});

Open in new window

0
Comment
Question by:Refael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37755983
$(this).animate({'background-position': '10px -15px'}, 1000);

assuming you use a new-ish jQuery
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 600 total points
ID: 37755985
You MAY still need a plugin

http://keith-wood.name/backgroundPos.html
0
 

Author Comment

by:Refael
ID: 37756222
Hi mplungjan,

I have tried it and it does not work. actually i am not sure this is related? i am dealing with a mouse-over menu link that changes the background color.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37756243
so try

$(this).animate({"background-color": "red"}, 1000);

but why not just toggle the class?
0
 
LVL 16

Accepted Solution

by:
Steve Krile earned 1400 total points
ID: 37756591
If you don't mind including Jquery UI in your project, then you can use the built-in class switching capability to achieve this effect.  Here is a sample.  The idea is to fix your anchor tag and another block element in the <li> tags.  The anchor stays the same on hover, but you change the <div> tag by assigning it a class with a width *different* than the default class of the div.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Color Back</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
    <script type="text/javascript">


        $(document).ready(function () {


                $(".menu li").hover(
                    function () { $(this).children("div").switchClass("hover_off", "hover_on", "normal") },
                    function () { $(this).children("div").switchClass("hover_on", "hover_off", "fast") });

        });


        

    </script>
    <style>
        ul.menu {width: 200px;border:solid 1px Gray; padding:4px;margin:0; list-style-type:none;position:relative;}
        .menu li {position:relative;height:24px;}
        .menu li a {display:block;position:absolute;height:100%;width:100%;top:0;}
        .hover_on {background: Red;width: 100%;position:absolute;height:100%;}
        .hover_off {background: Transparent;width: 0px;position:absolute;height:100%;}

    </style>
</head>
<body>

   <ul class="menu">
    <li>
        <div class="hover_off">
        </div>            
        <a href="#">Item 1</a>
    </li>
    <li>
        <div class="hover_off"></div>
        <a href="#">Item 2</a>
    </li>
    <li>
        <div class="hover_off"></div>
        <a href="#">Item 3</a>
    </li>
   </ul>
</body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37757029
A toggle would do the same. No need fo UI
0
 
LVL 16

Expert Comment

by:Steve Krile
ID: 37757291
A toggle does not animate - which is what the original poster asked for.

"I am trying to use Jquery animation to animate the effect so the green background starts from left to right."
0
 

Author Closing Comment

by:Refael
ID: 37821884
Thank you!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question