优化“滚动偏移位置”javascript片段

时间:2015-07-02 19:24:35

标签: javascript css optimization scroll position

我不知道如何优化这段代码。我意识到我可以把这些对象放到某种数组中,但我不知道该怎么做。任何指导将不胜感激。我还在学习,到目前为止stackoverflow对我帮助很大。谢谢!

$(document).ready(function () {
   var $stripe01 = $('.stripe-01');
   var $stripe02 = $('.stripe-02');  
   var $stripe03 = $('.stripe-03');  
   var $stripe04 = $('.stripe-04');  
   var $stripe05 = $('.stripe-05');    
   var $stripe06 = $('.stripe-06');      

$(window).scroll(function () {
    var s = $(this).scrollTop(),
        d = $(document).height(),
        c = $(this).height();

    scrollPercent = (s / (d - c) * 18);
    scrollPercent02 = (s / (d - c) * 14);      
    scrollPercent03 = (s / (d - c) * 30);            
    scrollPercent04 = (s / (d - c) * 4);            

    var position01 = -(scrollPercent * ($(document).width() - $stripe01.width()));
    var position02 = -(scrollPercent02 * ($(document).width() - $stripe01.width()));
    var position03 = -(scrollPercent03 * -($(document).width() - $stripe03.width()));      
    var position04 = (scrollPercent04 * -($(document).width() - $stripe04.width()));      

    $stripe01.css({
        'left': position01
    });
    $stripe02.css({
        'left': position02
    });
    $stripe03.css({
        'right': position03
    });      
    $stripe04.css({
        'right': position03
    });      
    $stripe05.css({
        'right': position03
    });      
    $stripe06.css({
        'left': position04
    });      
});
});

0 个答案:

没有答案