It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming CSS Forum

Make a Div move on hover

Make a Div move on hover

Postby Tomi on Sun Dec 21, 2008 4:38 pm

How can I make a Div move on hover? I'm wanting to create a fancy navigation whereby when the user hovers over the link, the background and the text will move out of alignment with the other links. Example:

////[Link]
////[Link]
////////[Link] (hovered over)
////[Link]

/= Solid block of colour.


Thanks
User avatar
Tomi
500+ Club
 
Posts: 925
Joined: Mon Jun 26, 2006 6:51 pm
Location: Essex, England

Re: Make a Div move on hover

Postby Johnathan on Sun Dec 21, 2008 5:59 pm

I got something. Not sure if it's exactly what you want but here it is.

Here's the HTML
Code: Select all
<ul>
<li><a href="#">Link1</a></li>
<li><a href="#">Link2</a></li>
<li><a href="#">Link3</a></li>
<li><a href="#">Link4</a></li>
<li><a href="#">Link5</a></li>
</ul>


And your CSS
Code: Select all
ul {
   padding:0px;
   margin:0px;
   }
li {
   margin:0px;
   padding:0px;
   list-style: none;
   }
li a {
   color:white;
   background-color:black;
   display: block;
   width:100px;
   }
li a:hover {
   background-color:black;
   display: block;
   text-align:right;
   }


Here's a working example. http://filesharebox.co.uk/test/
Johnathan
1000+ Club
 
Posts: 1207
Joined: Thu May 31, 2007 3:28 pm
Location: Belfast, Northen Ireland

Re: Make a Div move on hover

Postby Johnathan on Sun Dec 21, 2008 6:13 pm

I changed it a little now so the bar that pops out is always the same length.

Code: Select all
ul {
   padding:0px;
   margin:0px;
   }
li {
   margin:0px;
   padding:0px;
   list-style: none;
   }
li a {
   color:white;
   background-color:black;
   display: block;
   width:50px;
   }
li a:hover {
   background-color:black;
   display: block;
   text-align:right;
   width:100px;
   }

Live at http://filesharebox.co.uk/test/
Johnathan
1000+ Club
 
Posts: 1207
Joined: Thu May 31, 2007 3:28 pm
Location: Belfast, Northen Ireland

Re: Make a Div move on hover

Postby Tomi on Sun Dec 21, 2008 6:40 pm

Spoke on msn, thanks :)
User avatar
Tomi
500+ Club
 
Posts: 925
Joined: Mon Jun 26, 2006 6:51 pm
Location: Essex, England

Re: Make a Div move on hover

Postby rangana on Mon Dec 22, 2008 5:53 am

Some fancy motion ;)
Code: Select all
<style>
ul {
   padding:0px;
   margin:0px;
   width:100px;
   }
li {
   margin:1px;
   padding:0px;
   list-style: none;
   }
li a {
   display: block;
   width:100px;
   background:#565656;
   height:30px;
   text-align:center;
   font-family:"Trebuchet MS",arial,tahoma;
   font-size:10pt;
   line-height:30px;
   text-decoration:none;
   color:#9c0;
   font-weight:bold;
   }
</style>
<script type="text/javascript" src="http://rangana.moonylist.com/rac/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
   {
   $('ul li a').hover(
      function()
         {
         $(this).animate({marginLeft:'30px',backgroundColor:'#565656'},'normal');
         },
      function()
         {
         $('ul li a').animate({marginLeft:'0px',backgroundColor:'#9c0'},'fast');
         }
      );
   });
</script>
<ul>
<li><a href="#">Link1</a></li>
<li><a href="#">Link2</a></li>
<li><a href="#">Link3</a></li>
<li><a href="#">Link4</a></li>
<li><a href="#">Link5</a></li>
</ul>


You need to download jQuery and replace my external call to what you have at your end.

Hope that helps.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Make a Div move on hover

Postby Tomi on Tue Dec 23, 2008 2:02 am

Wow that's great thanks! How do I make it so I can have two lists, one that has the effect and one that doesn't?
User avatar
Tomi
500+ Club
 
Posts: 925
Joined: Mon Jun 26, 2006 6:51 pm
Location: Essex, England

Re: Make a Div move on hover

Postby rangana on Tue Dec 23, 2008 3:58 am

The script actually acts as if your pointing to a CSS rule of an element. You can replace the part inside the $ function to target the element of your desire, including IDs.

Here's a basic example:
Code: Select all
<style>
ul {
   padding:0px;
   margin:0px;
   width:100px;
   }
li {
   margin:1px;
   padding:0px;
   list-style: none;
   }
li a {
   display: block;
   width:100px;
   background:#565656;
   height:30px;
   text-align:center;
   font-family:"Trebuchet MS",arial,tahoma;
   font-size:10pt;
   line-height:30px;
   text-decoration:none;
   color:#9c0;
   font-weight:bold;
   }
</style>
<script type="text/javascript" src="http://rangana.moonylist.com/rac/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
   {
   $('#myul a').hover(
      function()
         {
         $(this).animate({marginLeft:'30px',backgroundColor:'#565656'},'normal');
         },
      function()
         {
         $('#myul a').animate({marginLeft:'0px',backgroundColor:'#9c0'},'fast');
         }
      );
   });
</script>
<ul id="myul">
<li><a href="#">Link1</a></li>
<li><a href="#">Link2</a></li>
<li><a href="#">Link3</a></li>
<li><a href="#">Link4</a></li>
<li><a href="#">Link5</a></li>
</ul>

<h3>Another list</h3>
<ul>
<li><a href="#">Link1</a></li>
<li><a href="#">Link2</a></li>
<li><a href="#">Link3</a></li>
<li><a href="#">Link4</a></li>
<li><a href="#">Link5</a></li>
</ul>


Hope that helps.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Make a Div move on hover

Postby Tomi on Tue Dec 23, 2008 4:02 am

Ooh I get it, thanks :)
User avatar
Tomi
500+ Club
 
Posts: 925
Joined: Mon Jun 26, 2006 6:51 pm
Location: Essex, England

Re: Make a Div move on hover

Postby rangana on Tue Dec 23, 2008 4:03 am

No problem. Glad to help ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 0 guests