我创建了一个视差滚动,它在 firefox 中似乎工作正常,但在 chrome 浏览器中滚动时正文文本略有跳跃。 click here scroll to the about section .我不确定这是 css 还是 JS 问题..下面是我已合并到我的视差函数中的片段
有谁知道我该如何解决这个问题?
$(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.) 使用position: fixed
来避免任何抖动,因为您会将元素从文档流中移除。然后,您可以使用 z-index 对其进行定位。
2.) 尽可能多地缓存以减少处理时间。
3.) Math.round 可能不是必需的,但尝试将此 CSS 添加到您的移动区域:-webkit-transform: translate3d(0,0,0);
这将强制硬件加速在 Chrome 中,这可能会缓解一些抖动。 (当我用检查器添加它时,它在我的屏幕上看起来更流畅,但它并没有消除滚轮的跳跃。)注意:不要在你的整个文档(例如 body 标签)上这样做,因为它可能导致您当前的布局出现一些问题。 (例如,您的导航栏没有粘在窗口的顶部。)
4.) 如果您有任何动画作为视差逻辑的一部分运行(将边距调整到位或沿着这些线进行调整),请将其移除 - 这可能会导致您看到跳跃。
希望这对您有所帮助。祝你好运。
关于javascript - 视差滚动问题 - 在 webkit 浏览器中滚动时 div 元素抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440577/