【问题标题】:use variable change on second click在第二次点击时使用变量更改
【发布时间】:2013-08-29 17:45:26
【问题描述】:

单击 .button 时,我想隐藏 4 个帖子,然后显示接下来的 4 个帖子,然后单击 .button 第二次应该隐藏现在可见的 4 个帖子(即帖子 5-8)并显示接下来的 4 个(发布 9-12)等等。

我的代码如下所示:

  var i = 0;
  function fadeOutFour(){
      $('.post').eq(i++).fadeOut(1000, function(){
        $('.post').eq(i++).fadeIn(1000);
        $('.post').eq(i++).fadeIn(1500);
        $('.post').eq(i++).fadeIn(2000);
        $('.post').eq(i++).fadeIn(2500);
      });
      $('.post').eq(i++).fadeOut(750);
      $('.post').eq(i++).fadeOut(500);
      $('.post').eq(i++).fadeOut(250);  
  }

  $('.button').click(function(){
        fadeOutFour(function(){
            i += 4;
        });
  });

现在点击工作后我无法获取 NEW 变量。所以首先,我做错了什么?另外,如果有更顺畅和更智能的方法来做到这一点,请告诉我:) 干杯!

【问题讨论】:

    标签: jquery variables button increment


    【解决方案1】:

    执行此操作的更简单紧凑的方法是这样

    看看

    http://jsfiddle.net/techsin/cfBeu/4/

    • 在前一个项目消失之前,不会预加载下一个项目。
    • 能够加载额外的项目,即使它们不完全是 n/4。
    • 代码更少。
    • 更流畅。

    代码如下:

    var n=v=4, time= 500, $p= $('.post'), t =true;
    
    
    $('.button').click(function(){
        if(t){
            t=false; if(n<$p.length+1) so();
        }});
    
    st();
    function st() {$p.slice(n-v,n-0).fadeIn(time,function(){t=true;});}
    function so() {$p.slice(n-v,n-0).fadeOut(time,function() {st();}); n+=v;}
    

    【讨论】:

    • 谢谢伙计,看起来棒极了!
    【解决方案2】:

    这就是你要找的东西吗?

    Working jsFiddle here

    var i = 0;
    $('.post').eq(i++).fadeIn(1000);
    $('.post').eq(i++).fadeIn(1500);
    $('.post').eq(i++).fadeIn(2000);
    $('.post').eq(i++).fadeIn(2500);
    
    function fadeOutFour() {
        firstHide(i);
        thenShow(i);
        i += 4;
    }
    
    function firstHide(i) {
        h = i - 4;
        $('.post').eq(h++).fadeOut(1000);
        $('.post').eq(h++).fadeOut(750);
        $('.post').eq(h++).fadeOut(500);
        $('.post').eq(h++).fadeOut(250);
    }
    
    function thenShow(i) {
        $('.post').eq(i++).fadeIn(1000);
        $('.post').eq(i++).fadeIn(1500);
        $('.post').eq(i++).fadeIn(2000);
        $('.post').eq(i++).fadeIn(2500);
    }
    
    $('.button').click(function () {
        fadeOutFour(function () {
            i += 4;
        });
    });
    

    【讨论】:

    • 我会这么说!非常感谢:)
    • 很高兴听到这个消息。请为任何对您有帮助的答案投票,并记得选择正确的答案来结束您的问题。
    • 很高兴为您提供帮助。祝愿您的项目顺利完成。
    • 嘿,看看我发现的 jquery 方法,我想你会喜欢的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多