V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zebulon
V2EX  ›  问与答

有没有办法能简化这段代码

  •  
  •   zebulon · Jan 20, 2016 · 2872 views
    This topic created in 3750 days ago, the information mentioned may be changed or developed.

    $('.tabs ul li').eq(0).click(function(){
    $('body,html').animate({scrollTop: $('#tab1').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(1).click(function(){
    $('body,html').animate({scrollTop: $('#tab2').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(2).click(function(){
    $('body,html').animate({scrollTop: $('#tab3').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(3).click(function(){
    $('body,html').animate({scrollTop: $('#tab4').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(4).click(function(){
    $('body,html').animate({scrollTop: $('#tab5').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(5).click(function(){
    $('body,html').animate({scrollTop: $('#tab6').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(6).click(function(){
    $('body,html').animate({scrollTop: $('#tab7').offset().top-30+'px'}, 800);
    });
    $('.tabs ul li').eq(7).click(function(){
    $('body,html').animate({scrollTop: $('#tab8').offset().top-30+'px'}, 800);
    });

    6 replies    2016-01-20 11:50:57 +08:00
    hippoboy
        1
    hippoboy  
       Jan 20, 2016
    确定长度之后递归或者遍历?
    wesley
        2
    wesley  
       Jan 20, 2016
    $('.tabs ul li').click(function(){
    $('body,html').animate({scrollTop: $('#tab'+($(this).index()+1)).offset().top-30+'px'}, 800);
    });
    sleepwater
        3
    sleepwater  
       Jan 20, 2016
    $('.tabs ul li').on('click', function() {
    $('body,html').animate({scrollTop: $('#tab' + ($(this).index() + 1)).offset().top - 30 + 'px'}, 800);
    });
    chairuosen
        4
    chairuosen  
       Jan 20, 2016
    这种东西应该把 target 写到 html 里,$('[click-target]').click(function(){ some code scroll to $( $(this).attr('click-target') ); })
    shiye515
        5
    shiye515  
       Jan 20, 2016 via Android
    如果维护这个项目的人连这个都不会优化了,那就放弃治疗吧,这肯定不是项目里最奇葩的代码
    Zzzzzzzzz
        6
    Zzzzzzzzz  
       Jan 20, 2016
    这段就应该直接写成事件委托的.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4715 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 10:06 · PVG 18:06 · LAX 03:06 · JFK 06:06
    ♥ Do have faith in what you're doing.