jQuery 延迟淡入效果

发布于 2024-10-06 19:51:19 字数 398 浏览 4 评论 0原文

我只是想对此片段做两个简单的调整:

$(document).ready(function() {
   $("span#poweroff a").click(function() {
      $("body").append('<div class="overlay"></div>');
   });
 });

首先,我希望 body.append 操作随着时间的推移而发生。这只是一个遮光叠加,但我想淡入?

其次,当有人悬停“span#poweroff a”时,我希望在一段时间后显示“span#poweroff a .message”,也可以淡入。

任何大师都愿意拯救我,我能在很长一段时间内得到什么时间让我不断尝试和犯错并让我恢复正常?非常感谢!

I'm just trying to do two simple adjutments to this snippet:

$(document).ready(function() {
   $("span#poweroff a").click(function() {
      $("body").append('<div class="overlay"></div>');
   });
 });

First I would like the body.append action to happen over time. It's just a blackout overlay, but I would like to fade in?

Second when someone hovers "span#poweroff a" I would like, after a certain amount of time to show "span#poweroff a .message", also by fadeing in.

Any guru's out there willing to save me what could me quite a long time putzing with trial and error and set me straight? Would very much appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2024-10-13 19:51:20

尝试.delay()。这可能就是您正在寻找的。至于您的代码,以下是您可能正在寻找的功能:

$(document).ready(function()
{
  $('span#poweroff').click(function()
  {
    $('body').append('<div class="overlay"></div>'); /* Make sure it's hidden via CSS, and has *some* uniqueness. */
    $('.overlay:eq(0)').fadeIn(); /* ':eq(0)' selects the first .overlay element. If you want all of them to animate, remove ':eq(0)'. I would use an ID for this, though, so if you decide to, replace the selector with '#your_id'. It's not too hard */
  });

  $('span#poweroff a').hover(function()
  {
    $('span#poweroff a .message').delay(1000)fadeIn(); /* You can set your speeds here too. If you won't use the delay, just omit the function. */
  }, function() {
    $('span#poweroff a .message').fadeOut(); /* If you want to, it can fade out. Otherwise, omit this function. */
  });  
});

这将作为一个粗略的框架(没有保证,因为我因犯小错误而臭名昭著)。

祝你好运!

Try .delay(). It's probably what you're looking for. As for your code, here are the functions you're probably looking for:

$(document).ready(function()
{
  $('span#poweroff').click(function()
  {
    $('body').append('<div class="overlay"></div>'); /* Make sure it's hidden via CSS, and has *some* uniqueness. */
    $('.overlay:eq(0)').fadeIn(); /* ':eq(0)' selects the first .overlay element. If you want all of them to animate, remove ':eq(0)'. I would use an ID for this, though, so if you decide to, replace the selector with '#your_id'. It's not too hard */
  });

  $('span#poweroff a').hover(function()
  {
    $('span#poweroff a .message').delay(1000)fadeIn(); /* You can set your speeds here too. If you won't use the delay, just omit the function. */
  }, function() {
    $('span#poweroff a .message').fadeOut(); /* If you want to, it can fade out. Otherwise, omit this function. */
  });  
});

This would work as a rough framework (no guarantees, as I am notorious for making small mistakes).

Good luck!

清风挽心 2024-10-13 19:51:20

您需要像这样对覆盖 div 进行硬编码:

<div class="overlay" style="display:none;"></div>

然后像这样使用 jQuery:

    $(document).ready(function() {
   $("span#poweroff a").click(function() {
      $('.overlay').fadeIn('slow');
   });
 });

如果我理解正确的话,当有人单击 span#poweroff 时,它会慢慢显示覆盖 div。

我的问题是会发生什么当您将鼠标悬停在 span#poweroff a 上然后单击它并显示 .overlay 时?如果您单击它,它将激活延迟显示,因为您必须将鼠标悬停在它上面才能单击它。

如果我们需要等待覆盖层可见,这里是没有处理的 jQuery:

    $(document).ready(function() {
       $("span#poweroff a").mouseenter(function() {
          $('.message').fadeIn('slow');
       });
       $("span#poweroff a").mouseleave(function() {
          $('.message').fadeOut('slow');
       });
     });

You need to hard code the overlay div like this:

<div class="overlay" style="display:none;"></div>

Then the jQuery like so:

    $(document).ready(function() {
   $("span#poweroff a").click(function() {
      $('.overlay').fadeIn('slow');
   });
 });

If I'm understanding you correctly, when someone clicks on span#poweroff it will slowly show the overlay div.

My question to you is what happens when you mouse over the span#poweroff a before clicking on it and showing the .overlay? If you go to click on it its going to activate the delayed show because you have to hover over it to click on it.

Here is the jQuery without handling if we need to wait for the overlay to be visible:

    $(document).ready(function() {
       $("span#poweroff a").mouseenter(function() {
          $('.message').fadeIn('slow');
       });
       $("span#poweroff a").mouseleave(function() {
          $('.message').fadeOut('slow');
       });
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文