Jquery:新的 FadeToggle,我可以在切换的第二个操作上运行代码吗?

发布于 2024-10-03 06:26:34 字数 278 浏览 0 评论 0原文

所以当我点击#mybutton时,它会淡入.mybox。是否可以采用下面的代码,并添加类似 .animate({'width':'toggle'}) 的内容或在第一次切换,然后第二次激活切换时不激活延迟? 我该怎么做?最简单/最简单的方法?

这是我要使用的代码:

$('#mybutton').click(function(){
  $('.mybox').fadeToggle(500)
})

so when i click #mybutton it will fade in .mybox. is it possible to take my code below, and add something like .animate({'width':'toggle'}) or add a .delay(2000) before the first time it's toggled, then not activate the delay when the toggle is activated the second time?
how would i do that? easiest/simplest way?

here's my code to work with:

$('#mybutton').click(function(){
  $('.mybox').fadeToggle(500)
})

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

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

发布评论

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

评论(3

向日葵 2024-10-10 06:26:34

使用 one() 绑定一次性事件处理程序,然后绑定“真正的”永久事件处理程序将在这里工作:

var box = $('#box');

$('a').one('click', function(){
    box.delay(2000).fadeToggle();
    $(this).click(function(){
        box.fadeToggle();
        return false;
    });
    return false;
});

请在此处查看这个简单的演示: http://jsfiddle.net/一江/2m3bb/

Using one() to bind a one-time event handler, then binding the "real" permanent event handler will would work here:

var box = $('#box');

$('a').one('click', function(){
    box.delay(2000).fadeToggle();
    $(this).click(function(){
        box.fadeToggle();
        return false;
    });
    return false;
});

See this simple demo here: http://jsfiddle.net/yijiang/2m3bb/

傻比既视感 2024-10-10 06:26:34

这是我的做法,使用 setTimeout() :

var firstClick = true; 

$('#mybutton').click(function(){
  if (firstClick) {
      setTimeout(function() {
          $('.mybox').fadeToggle(500);
          firstClick = false; // #mybutton has been clicked once, we no longer want to wait before toggling on subsequent clicks of #mybutton 
      });
  } else {
      $('.mybox').fadeToggle(500);
  }      
}) 

Here's how I'd do it, using setTimeout():

var firstClick = true; 

$('#mybutton').click(function(){
  if (firstClick) {
      setTimeout(function() {
          $('.mybox').fadeToggle(500);
          firstClick = false; // #mybutton has been clicked once, we no longer want to wait before toggling on subsequent clicks of #mybutton 
      });
  } else {
      $('.mybox').fadeToggle(500);
  }      
}) 
热血少△年 2024-10-10 06:26:34

您可以仅使用 toggle() 而不是 fadeToggle() 来获得更多控制。

$('#mybutton').click(function(){
var firstTime = true;
$('.mybox').toggle(
    function () {
        $(this).fadeIn(500);
    },
    function () {
        if (firstTime)
        { $(this).delay(2000); }
        $(this).animate({'width':'toggle'}).fadeOut(500);
    }
);
})

You can use just toggle() instead of fadeToggle() to get a bit more control.

$('#mybutton').click(function(){
var firstTime = true;
$('.mybox').toggle(
    function () {
        $(this).fadeIn(500);
    },
    function () {
        if (firstTime)
        { $(this).delay(2000); }
        $(this).animate({'width':'toggle'}).fadeOut(500);
    }
);
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文