2017-06-12 67 views
1

我使用這個代碼:EXAMPLE慢速滾動風聲鶴唳

取決於如果「形象-UL」是完全上面的瀏覽器窗口的底部邊緣與否,都會使div的滾動速度不同,因爲它應該。但是我遇到的問題是,當慢速滾動div接近頁面頂部時,滾動不順暢。它們似乎暫時停頓,有時甚至會朝相反的方向滾動。

// 
// default speed ist the lowest valid scroll speed. 
// 
var default_speed = 1; 
// 
// speed increments defines the increase/decrease of the acceleration 
// between current scroll speed and data-scroll-speed 
// 
var speed_increment = 0.01; 
// 
// maximum scroll speed of the elements 
// 
var data_scroll_speed_a = 3; // #sloganenglish 
var data_scroll_speed_b = 5; // #image-ul 
// 
// 
// 
var increase_speed, decrease_speed, target_speed, current_speed, speed_increments; 
$(document).ready(function() { 
    $(window).on('load resize scroll', function() { 
     var WindowScrollTop = $(this).scrollTop(), 
      Div_one_top = $('#image-ul').offset().top, 
      Div_one_height = $('#image-ul').outerHeight(true), 
      Window_height = $(this).outerHeight(true); 
     if (WindowScrollTop + Window_height >= (Div_one_top + Div_one_height)) { 
      $('#sloganenglish').attr('data-scroll-speed', data_scroll_speed_a).attr('data-current-scroll-speed', default_speed).attr('data-speed-increments', data_scroll_speed_a * speed_increment); 
      $('#image-ul').attr('data-scroll-speed', data_scroll_speed_b).attr('data-current-scroll-speed', default_speed).attr('data-speed-increments', data_scroll_speed_b * speed_increment); 
      $('.slogan-a-line').css('color', 'yellow'); 
      increase_speed = true; 
      decrease_speed = false; 
     } else { 
      $('#sloganenglish').attr('data-scroll-speed', '1').attr('data-current-scroll-speed', default_speed); 
      $('#image-ul').attr('data-scroll-speed', '1').attr('data-current-scroll-speed', default_speed); 
      $('.slogan-a-line').css('color', 'red'); 
      decrease_speed = true; 
      increase_speed = false; 
     } 
    }).scroll(); 
}); 


// data-scroll-speed script 
$.fn.moveIt = function() { 
    var $window = $(window); 
    var instances = []; 

    $(this).each(function() { 
     instances.push(new moveItItem($(this))); 
    }); 

    window.onscroll = function() { 
     var scrollTop = $window.scrollTop(); 
     instances.forEach(function(inst) { 
      inst.update(scrollTop); 
     }); 
    } 
} 

var moveItItem = function(el) { 
    this.el = $(el); 
    this.speed = parseInt(this.el.attr('data-scroll-speed')); 
    this.current_speed = 1.0; 
}; 

moveItItem.prototype.update = function(scrollTop) { 

    target_speed = parseInt(this.el.attr('data-scroll-speed')); 
    current_speed = this.current_speed; 
    speed_increments = parseFloat(this.el.attr('data-speed-increments')); 

    if (increase_speed) { 
     if (current_speed < target_speed) { 
      current_speed += speed_increments; 
     } else { 
      current_speed = target_speed; 
     } 
    } else if (decrease_speed) { 
     if (current_speed > default_speed) { 
      current_speed -= speed_increments; 
     } 
     if ($(window).scrollTop() === 0) { 
      current_speed = default_speed; 
     } 
    } 
    this.current_speed = current_speed; 
    var pos = scrollTop/this.current_speed; 
    this.el.css('transform', 'translateY(' + -pos + 'px)'); 
}; 

// Initialization 
$(function() { 
    $('[data-scroll-speed]').moveIt(); 
}); 

回答

0

示例代碼對我來說並不慢,所以它可能是特定於您的機器或瀏覽器的。

然而,也有一些事情可以做:

  1. 不要使用jQuery,你不需要它。 jQuery是明顯比使用原生JS函數(例如document.getElementById)慢

  2. 請勿重複使用jQuery選擇器。每次使用jQuery選擇器時,都會遇到性能問題。所以例如,而不是這樣的:

    function(){ 
        var Div_one_top = $('#image-ul').offset().top, 
        Div_one_height = $('#image-ul').outerHeight(true); 
    } 
    

這樣做:

var imageUl = $('#image-ul'); 
function(){ 
    imageUl.offset().top, 
    imageUl.outerHeight(true); 
} 

這個例子應該提高性能相當多。每當頁面無故滾動時,您正在執行多個jQuery選擇器。

性能密集的最佳選擇是徹底刪除jQuery並手工完成。

+0

非常感謝@Christopher Schneider!但是,這看起來像是一個重新編碼的代碼,我不夠聰明。 – Eddy

+0

那麼,刪除jQuery是。但是,至少應該將jQuery選擇器移到該函數之外。 –

+0

好吧,我會看看我能否弄清楚。謝謝!! – Eddy