jQuery简单的动画跳动菜单

网络编辑 13/9/2 3.2k 0

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

相关帖子
MemacX
final cut pro10.X 几款插件
MemacX最后由MemacX回复于 14/2/21
0 / 2.2k
花样年华
欣赏两张GiF动画照片 中秋小福利
花样年华最后由花样年华回复于 13/9/19
0 / 2.1k
花样年华
25个漂亮的CSS3 突变和动画效果教程
花样年华最后由花样年华回复于 13/8/25
0 / 3.3k
admin
3 / 8.8k
admin
分享一款-微软新Logo开机动画
admin最后由584842381an回复于 16/6/25
12 / 7.5k
花样年华
4 / 3.9k
花样年华
12 / 7.4k
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
您需要登录后才可以回帖 登录 | 注册
本版积分规则