麦创网

发表于 13/9/2 21:29:00 |显示全部楼层
jQuery简单的动画跳动菜单

HTML:
  1. <div id="lava">
  2. <ul>
  3. <li><a href="#">home</a></li>
  4. <li><a href="#">lava lamp menu</a></li>
  5. <li><a href="#">queness.com</a></li>
  6. <li class="selected"><a href="#">jQuery</a></li>
  7. </ul>
  8. <!-- If you want to make it even simpler, you can append these html using jQuery -->
  9. <div id="box"><div class="head"></div></div>
  10. </div>
复制代码
CSS:
  1. body {
  2. font-family:georgia;
  3. font-size:14px;
  4. }
  5. a {
  6. text-decoration:none;
  7. color:#333;
  8. }
  9. #lava {
  10. /* you must set it to relative, so that you can use absolute position for children elements */
  11. position:relative;
  12. text-align:center;
  13. width:583px;
  14. height:40px;
  15. }
  16. #lava ul {
  17. /* remove the list style and spaces*/
  18. margin:0;
  19. padding:0;
  20. list-style:none;
  21. display:inline;

  22. /* position absolute so that z-index can be defined */
  23. position:absolute;

  24. /* center the menu, depend on the width of you menu*/
  25. left:110px;
  26. top:0;

  27. /* should be higher than #box */
  28. z-index:100;
  29. }
  30. #lava ul li {

  31. /* give some spaces between the list items */
  32. margin:0 15px;

  33. /* display the list item in single row */
  34. float:left;
  35. }
  36. #lava #box {

  37. /* position absolute so that z-index can be defined and able to move this item using javascript */
  38. position:absolute;
  39. left:0;
  40. top:0;

  41. /* should be lower than the list menu */
  42. z-index:50;
  43. /* image of the right rounded corner */
  44. background:#ccc;
  45. height:20px;

  46. /* add padding 8px so that the tail would appear */
  47. padding-right:8px;

  48. /* self-adjust negative margin to make sure the box display in the center of the item */
  49. margin-left:-10px;
  50. }
  51. #lava #box .head {
  52. /* image of the left rounded corner */
  53. background:#eee;
  54. height:20px;
  55. /* self-adjust left padding to make sure the box display in the center of the item */
  56. padding-left:10px;
  57. }

  58. jQuery:
  59. $(document).ready(function () {
  60. //transitions
  61. //for more transition, goto http://gsgd.co.uk/sandbox/jquery/easing/
  62. var style = 'easeOutElastic';

  63. //Retrieve the selected item position and width
  64. var default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
  65. var default_width = $('#lava li.selected').width();
  66. //Set the floating bar position and width
  67. $('#box').css({left: default_left});
  68. $('#box .head').css({width: default_width});
  69. //if mouseover the menu item
  70. $('#lava li').hover(function () {

  71. //Get the position and width of the menu item
  72. left = Math.round($(this).offset().left - $('#lava').offset().left);
  73. width = $(this).width();
  74. //Set the floating bar position, width and transition
  75. $('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});
  76. $('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});

  77. //if user click on the menu
  78. }).click(function () {

  79. //reset the selected item
  80. $('#lava li').removeClass('selected');

  81. //select the current item
  82. $(this).addClass('selected');
  83. });

  84. //If the mouse leave the menu, reset the floating bar to the selected item
  85. $('#lava').mouseleave(function () {
  86. //Retrieve the selected item position and width
  87. default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left);
  88. default_width = $('#lava li.selected').width();

  89. //Set the floating bar position, width and transition
  90. $('#box').stop(false, true).animate({left: default_left},{duration:1500, easing: style});
  91. $('#box .head').stop(false, true).animate({width:default_width},{duration:1500, easing: style});

  92. });

  93. });
复制代码
在线演示http://www.aspxcs.net/index.php?m=content&c=index&a=down_file&id=525


本地下载http://www.aspxcs.net/index.php?m=content&c=down&a_k=f674A1ZSAgcHBQMABgBQVgFTBlcMCAtTBlJXUFZdCAYHAkRAW0RfCgYTVQ1eRBIRWR1LEUJHHFFCFBpWRBpbVkEYF0MKDVNTUVxfVRkCVlBSHVVXBwQdAgFVUwQGBgECBQVTA1FbAA4ZT1pAEFRbUEVfCwJQXFtUDFZEVlZAXFcIAA

打赏楼主

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

 
您需要登录后才可以回帖 登录 | 注册

本版积分规则

麦创网 ( 苏ICP备13053101号-1 )

@麦创网 禁止任何未经允许的修改,传播或使用。

Powered by Discuz!

©2012 @ iMac.vip. 用户言论不代表本站立场。

返回顶部