我正在开发一个利用视差滚动的网站。它在 chrome 中看起来很漂亮,但我想绘制到画布上的图像都没有在 Firefox 中呈现。我很困惑。
这是HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf8'>
<link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<canvas id='canvas' width="400" height="200"></canvas>
<img id='dog' src='dog.gif'>
<script src='script.js'></script>
</body>
</html>
和 Javascript:
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
(function(win, d){
var $ = d.querySelector.bind(d);
var w = window.innerWidth;
var h = d.height;
var canvas = $('#canvas');
var ctx = canvas.getContext('2d');
var dog = $('#dog');
var ticking = false;
var relativeY = win.scrollY / h;
function updateElements(){
ctx.drawImage(dog, 10, pos(50, 0, relativeY, 0));
};
function paint(){
requestAnimFrame(updateElements)
};
function onScroll(){
if(!ticking){
ticking = true;
requestAnimFrame(updateElements);
var lastScrollY = win.scrollY;
}
}
function pos(base, range, relY, offset) {
return base + limit(0, 1, relY - offset) * range;
}
function limit(min, max, value) {
return Math.max(min, Math.min(max, value));
}
win.addEventListener('load', paint, false);
win.addEventListener('scroll', onScroll, false);
})(window, document);
谢谢!