jQuery实战之仿Flash跳动的按钮效果

来源:互联网 发布:python中time.sleep 编辑:程序博客网 时间:2024/05/19 16:35



下面是JQ的代码部分:

  1. $(function(){   
  2.       //append img to LI   
  3.       $("#nav-shadow li").append('<img class="shadow" src="images/reflaction_pic.jpg" 
  4. width="60" height="32"  alt="" />');//这是阴影的图片   
  5.       //append hover event   
  6.       $("#nav-shadow li").hover(function(){   
  7.      //define e for tihs   
  8.           var $e = this;   
  9.          $($e).find("a").stop().animate({marginTop:'-14px'},250,function(){//回调函数控制   
  10.               $($e).find("a").animate({marginTop:'-10px'},250);  
  11.               
  12.          });  
  13.          $($e).find("img.shadow").stop().animate({width:"80%",opacity:"0.3",marginLeft:"8px"},250);                  
  14.            
  15.      },function(){  
  16.          var $e = this;  
  17.          $($e).find("a").stop().animate({marginTop:"4px"},250,function(){  
  18.              $($e).find("a").animate({marginTop:"0px"},250);  
  19.          });      
  20.          $($e).find("img.shadow").stop().animate({width:"100%",opacity:"1",marginLeft:"0px"},250);              
  21.         }  
  22.         )  
  23.      }) 

分析:

首先增加倒影:

  1. $("#nav-shadow li").append('<img class="shadow" src="images/reflaction_pic.jpg" 
  2. width="60" height="32"  alt="" />'

然后注册,hover事件,用回调函数控制弹回去时候的效果。阴影的距离感是通过透明度控制的。

下面是HTML代码:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">   
  2.   <html>   
  3.      <head>   
  4.          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   
  5.          <title>button_effect</title>   
  6.          <script type="text/javascript" src="jquery-1.4.2.min.js"></script>   
  7.          <script type="text/javascript" src="action.js"></script>   
  8.          <style type="text/css">   
  9.              *{ margin:0; padding:0;}  
  10.              div{ width:500px; height:500px; margin:100px 0 auto;}  
  11.              ul,ol{ list-style:none; list-style-type:none;}  
  12.              a,a:visited,a:hover{ display:block; text-decoration:none; color:#ccc; text-indent:-9999px; 
  13. outline: 0 none; width:61px; height:60px; z-index:2; overflow:hidden;  position:relative;}  
  14.              li{ float:left; width:61px; height:92px; margin-left:10px; position:relative;}      
  15.              #nav-shadow li.chang-one a{ background:url(images/button_pic.jpg) no-repeat left top;}  
  16.              #nav-shadow li.chang-two a{background:url(images/button_pic.jpg) no-repeat -60px top;}  
  17.              #nav-shadow li.chang-three a{background:url(images/button_pic.jpg) no-repeat -120px top;}  
  18.              #nav-shadow li.chang-four a{background:url(images/button_pic.jpg) no-repeat -180px top;}  
  19.              #nav-shadow li.chang-five a{background:url(images/button_pic.jpg) no-repeat -240px top;}  
  20.              #nav-shadow li img.shadow{margin:0 auto; position:absolute; bottom:0px; left:0px; z-index:1;}  
  21.          </style>  
  22.      </head>  
  23.      <body>  
  24.       <div id="content">  
  25.           <ul id="nav-shadow">  
  26.               <li class="chang-one"><a href="#" title="reflaction_one">click me</a></li>  
  27.              <li class="chang-two"><a href="#" title="reflaction_two">click me</a></li>  
  28.              <li class="chang-three"><a href="#" title="reflaction_three">click me</a></li>  
  29.              <li class="chang-four"><a href="#" title="reflaction_four">click me</a></li>  
  30.              <li class="chang-five"><a href="#" title="reflaction_five">click me</a></li>          
  31.           </ul>  
  32.       </div>  
  33.      </body>  
  34.  </html>