我已經創建了一個視差滾動,它似乎在Firefox中正常工作,但是在Chrome瀏覽器中滾動時正文文本略有跳動。 click here scroll to the about section。我不知道這是一個CSS或JS問題..下面是我已經納入我的視差功能的片段視差滾動問題 - 在webkit瀏覽器中滾動時div元素跳動
有誰知道我該如何解決這個問題?
$(document).ready(function(){
// Cache the Window object
$window = $(window);
// Cache the Y offset and the speed of each sprite
$('[data-type]').each(function() {
$(this).data('offsetY', parseInt($(this).attr('data-offsetY')));
$(this).data('Xposition', $(this).attr('data-Xposition'));
$(this).data('speed', $(this).attr('data-speed'));
});
// For each element that has a data-type attribute
$('[data-type="background"]').each(function(){
// Store some variables based on where we are
var $self = $(this),
offsetCoords = $self.offset(),
topOffset = offsetCoords.top;
// When the window is scrolled...
$(window).scroll(function() {
// If this section is in view
if (($window.scrollTop() + $window.height()) > (topOffset) &&
((topOffset + $self.height()) > $window.scrollTop())) {
// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -($window.scrollTop()/$self.data('speed'));
// If this element has a Y offset then add it on
if ($self.data('offsetY')) {
yPos += $self.data('offsetY');
}
// Put together our final background position
var coords = '50% '+ yPos + 'px';
// Move the background
$self.css({ backgroundPosition: coords });
$('[data-type="scroll-text"]', $self).each(function() {
var $text= $(this);
var pos = ($window.scrollTop()/10) * $text.data('speed');
var curP = $text.css('margin-top');
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(is_chrome) {
$text.animate({
paddingTop: pos,
}, 200, 'linear', function() {
// Animation complete.
});
} else {
$text.css('padding-top', pos);
}
});
}; // in view
}); // window scroll
}); // each data-type
}); // document ready
您的代碼將受益於簡單的優化:1)調用'$(窗口).scrollTop()'只是一次並緩存值,2)查詢DOM的'[數據文本]'之外的元素的事件監聽者。 –
@IanKuca你可以詳細說明這個升技,甚至可以編輯github上的JS。我不是最大的Javascript – NewBoy
http://pastebin.com/JCaA7T6A –