我有500w * 500h的畫布1,其中包含5000w * 500h的寬背景圖像。我通過設置overflow:hidden
來包裝具有div 500 * 500的畫布,以便首先加載背景,但僅顯示背景500w,其餘部分將通過使用scrollLeft屬性滾動viewPort部分來顯示.i添加了一個事件監聽器,用於檢查 - >或< - 按鍵並相應地滾動視口。我的問題是HTML 5畫布滾動事件偵聽器不工作
隨着家居裝和設置正確(背景圖像正確加載,並在畫布上繪製),當我使用下面的代碼
window.onload=function(){
//images are loaded and drawn
//necessary variables defined here
//view is the element id of the viewport div and is also properly accessed
window.addEventListener("keydown",function(e){
keyPressFlag = true;
if(e.keyCode == 39) //-> key
{view.scrollLeft+=3;}
});
};
獲得滾動效果。但是當我嘗試按照以下方式實現相同的效果時,我得不到相同的效果。我可以看到RIGHT MOVED登錄到控制檯,但滾動不會發生。
window.onload=function(){
//images are loaded and drawn
//necessary variables defined here
//view is the element id of the viewport div and is also properly accessed
//keyPressFlag = false; and keyPressed = 0 initially
function move()
{
if(keyPressed == 37)
{
console.log("LEFT MOVED");
view.scrollLeft -=3;
}
if(keyPressed == 39)
{
console.log("RIGHT MOVED");
view.scrollRight +=3;
}
}
window.addEventListener("keydown",function(e){
console.log("Key Pressed");
keyPressFlag = true;
keyPressed = e.keyCode;
move();
});
window.addEventListener("keyup",function(e){
console.log("key Released");
keyPressFlag = false;
});
};
我應該怎樣做,當事件被觸發時通過調用move函數來做背景滾動?
我喜歡你的想法!我會嘗試這種方式。儘管我在代碼中發現錯誤,但move()函數應該具有view.scrollLeft而不是view.scrollRight。我自己愚蠢的錯誤 – 2014-12-03 18:38:46