javascript - jQuery 动画垂直菜单

标签 javascript jquery html css

我正在尝试创建一个包含一些动画的垂直菜单。

我希望用户将鼠标悬停在触发元素宽度扩展并显示一些文本的元素的一部分上。

过去几天我尝试过,但感觉我现在只会让事情变得更糟!

如有任何帮助或建议,我们将不胜感激。

这是我的 fiddle :http://jsfiddle.net/danieljoseph/NZ2QL/3/

这是 JQuery:

$("nav li").hover(function () {
  $("nav li").css('width', 'auto');
});

$("#about-btn").mouseover(function () {
  $("#about-btn .tab").animate({
    width: "190px"
}, 1000);
});
$("#about-btn").mouseout(function () {
  $("#about-btn .tab").animate({
    width: "0"
  }, 1000);
});

$("#services-btn").mouseover(function () {
  $("#services-btn .tab").animate({
    width: "190px"
  }, 1000);
});
$("#services-btn").mouseout(function () {
  $("#services-btn .tab").animate({
    width: "0"
  }, 1000);
});

$("#work-btn").mouseover(function () {
  $("#work-btn .tab").animate({
    width: "190px"
  }, 1000);
});
$("#work-btn").mouseout(function () {
  $("#work-btn .tab").animate({
    width: "0"
  }, 1000);
});

$("#contact-btn").mouseover(function () {
  $("#contact-btn .tab").animate({
    width: "190px"
  }, 1000);
});
$("#contact-btn").mouseout(function () {
  $("#contact-btn .tab").animate({
    width: "0"
  }, 1000);
});

我是 JQuery 的业余爱好者,所以如果可能的话,任何提示和解释都会很棒。

谢谢。

最佳答案

Demo Fiddle

jQuery 代码

$("nav li").hover(function () {
    console.log($(this).children().children('.tab'));
    $(".tab", this).stop().animate({
        width: "190px"
    }, 1000);
}, function () {
    $(".tab", this).stop().animate({
        width: "0"
    }, 1000);
});  

根据我的说法,<ul> 的直接子代只能是 <li>所以我什至改变了你给的 fiddle 中错误的位置......

注意:这个任务可以使用纯 css 来完成,这样会轻得多


更新不是问题的一部分:

The same above feature using plain css

关于javascript - jQuery 动画垂直菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961002/

相关文章:

javascript - HTML/Javascript 设置两个选择选项并在选择第一个选项后重定向

javascript - 判断浏览器是否为IE最简单、最好的方法是什么?

javascript - 如何在不使用 ID 的情况下在 Polymer 中选择节点?

javascript - 如何使用 jquery 添加对称值

jquery - JavaScript 运行时错误 : cannot call methods on jtable prior to initialization; attempted to call method 'load'

html - 如何同时加载2个悬停效果

javascript - 在另一个元素中显示来自 "input range"的值给出 "undefined"

Javascript promise 意外的执行流程

javascript - append 项目时,如何使轮播上的点击功能起作用?

javascript - 将下拉列表的值文本动态附加到另一个下拉列表