【问题标题】:jQuery slidedown and then fadein contentjQuery 向下滑动然后淡入内容
【发布时间】:2026-02-15 16:20:03
【问题描述】:

我想知道是否有人可以提供帮助,我正在尝试复制客户喜欢的菜单(原则上):http://www.arup.com

到目前为止,我已经尝试查看该网站背后的代码/jquery,它似乎过于复杂(至少对我而言),我在 JSFiddle 上已经做到了这一点

$(document).ready(function () {
    $('#qc1,#qc2,#qc3').hover(function () {
        $(this).find('.answer').stop(true, true).fadeIn({
            duration: '3000',
            queue: true
        }).css('display', 'none').slideDown({
            duration: '1000',
            queue: false
        });
    }, function () {
        $(this).find('.answer').stop(true, true).fadeOut({
            duration: '3000',
            queue: true
        }).slideUp({
            duration: '2000',
            queue: false
        });
    });
});

理想情况下,我希望 div 向下滑动,然后让内容淡入 onmousein(然后反向 onmouseout),但作为 jQuery 新手已经尝试了几个小时。

【问题讨论】:

  • 考虑使用 css3 jsfiddle.net/3WfaK/2
  • 为什么要让人们悬停在空白处才能看到文字?这是可怕的用户体验,而且看起来很慢。你现在得到的现在看起来不错
  • @Huangism 我想你误解了 OP... 看看示例网站
  • 哦,我明白了,基本上是向下滑动容器然后淡入内容
  • 我们的任何解决方案是否有用,或者您正在寻找其他解决方案?我有兴趣看到您问题的最终解决方案。

标签: jquery fadein slide jquery-hover


【解决方案1】:

可能是like this 的东西?

$(document).ready(function() {
  $(".answer").each(function() {
      $(this).hide();
  });
  $('#qc1,#qc2,#qc3').hover(
    function() {
      $(this).find('.answer')
        .stop(1,1)
        .slideDown(500, function() {
          // note: i've wrapped the dropdown's content with an additional div.
          $(this).find("div").fadeIn(500); 
        });
    }, function() {
      $(this).find('.answer div')
        .stop(1,1)
        .fadeOut(500, function() {
          $(this).parent('.answer').stop().slideUp(500);
        });
      }
  );
});

基本上,一旦它的 parent 的 slideDown() 完成,我正在通过回调淡入()下拉列表的 content,反之亦然。

【讨论】:

  • 感谢您的帮助 Thykka,但我认为第一个几乎完成了。干杯马特
【解决方案2】:

我建议您参考 JQuery 文档,专门用于 slideUp()fadeIn() 函数。

可以选择在动画完成后执行“完成”功能。这就是您要在幻灯片之后执行淡入淡出的内容,反之亦然。

与 thykka 的解决方案一样,需要另一个 div 才能使淡入淡出和滑动分别发生。

<div class="answer" id="a1">
    <div class="answer_content">
        What makes us different
        <br />Vision & Values
        <br />Our Story
        <br />Our People
    </div>
</div>

这是DEMO

$(document).ready(function () {
    var qcContainer = $('#qc1,#qc2,#qc3');

    qcContainer.hover(
    // On mouse enter function
    function () {
        qcContainer.find('.answer').stop(true, true).slideDown(
            "slow", function () {
            // slideDown is complete, now for fadeIn
            qcContainer.find('.answer_content').stop(true, true).fadeIn('slow', function () {
                // Animation complete.
            });
        });

    },
    // On mouse leave function
    function () {
        qcContainer.find('.answer_content').stop(true, true).fadeOut(
            "slow", function () {
            // fadeOut complete, time for slideUp
            qcContainer.find('.answer').stop(true, true).slideUp("slow", function () {
                // Animation complete.
            });
        });
    });
});

【讨论】:

  • 谢谢乔纳森,这几乎是完美的,我需要做的就是按照下面 Huangism 的回答将它包装在一个容器中,这将是正确的!干杯马特
  • 太棒了!我很高兴能帮上忙!
【解决方案3】:

类似http://jsfiddle.net/P2ySU/3/ 这是我在示例网站上看到的。它所做的只是扩展容器并在菜单运行时保持这种状态

$('#container').on({
    mouseenter: function() {
        $(this).animate({height: '200px'});
    },
    mouseleave: function() {
        $(this).animate({height: '53px'});
    }
});

我加了

<div id="container">

作为整体包装。此外,您应该更改项目上的重复 ID 并改用类

【讨论】:

  • 感谢您的帮助 Huangism,我将借用您的建议将其包装在容器中,但使用上面 Jonathan 的代码!干杯马特
最近更新 更多