Kinetik kaydırmayı (bu iyi bir fikir olsun ya da olmasın soru değil) uygulamaya çalışıyordum ve bazı "garip" davranışlar yaşadım.Javascript'te garip zamanlama davranışı
function scroll(timestamp){
var deltaTime = timestamp - this.scrollLastTime;
this.scrollLastTime = timestamp;
console.log(deltaTime);
var newPosition = this.scrollTop + this.scrollSpeed*deltaTime;
if(newPosition <= 0){
this.scrollTop = 0;
this.scrolling = false;
return;
}else if(newPosition > this.scrollHeight-this.clientHeight){
this.scrollTop = this.scrollHeight-this.clientHeight;
this.scrolling = false;
}else{
this.scrollTop = newPosition;
var newSpeed = this.scrollSpeed + Math.sign(this.scrollSpeed) * this.scrollAcceleration*deltaTime;
if(this.scrollSpeed < 0 && newSpeed >= 0){
this.scrolling = false;
}else if(this.scrollSpeed >0 && newSpeed <= 0){
this.scrolling = false;
}else{
this.scrollSpeed = newSpeed;
window.requestAnimationFrame(this.scrollCallback);
}
}
}
document.getElementById("0").addEventListener('wheel',
function(e){
this.scrollSpeed = e.wheelDelta/100;
if(!this.scrolling){
this.scrolling = true;
this.scrollLastTime = performance.now();
this.scrollAcceleration = -0.01;
if(!this.scrollCallback)this.scrollCallback = scroll.bind(this);
window.requestAnimationFrame(this.scrollCallback);
}
e.preventDefault();
});
sorun genellikle deltaTime negatif hale gelir, ne eksik?
Düzenleme: Bu yardımcı olursa Chromium 51.0.2704.79 Ubuntu 14.04 (64-bit) kullanıyorum. @Whothehellisthat zaten onun yorumunda işaret ettiği gibi
Temelde rAF zaman damgası çok güvenilir değildir. Kodun gerçekten çalıştırıldığı süreden ziyade en yakın kare süresine yapışmış gibi görünüyor. Asla altını almamıştım, ama aynı sorunu yaşıyorum (http://stackoverflow.com/questions/38360250/requestanimationframe-now-vs-performance-now-time-discrepancy). Sadece rAF zaman damgasına güvenmek yerine her frame'i 'performance.now()' kullanarak kullanmaya başvurdum. – Whothehellisthat