這裏是jsfiddle:http://jsfiddle.net/vZMyS/ 編輯:多謝巴克Doyle我做了一個合適的,如果其他與不同版本的腳本鉻和火狐/歌劇。現在它只是不適用於IE瀏覽器。Javascript代碼在IE中不工作
腳本使用戶能夠使用向上/向下鍵盤按鍵向下/向上滾動到「var scrollKeys」中指定的滾動點,它在chrome中工作,但在其他瀏覽器中不起作用。
任何想法是什麼錯在這裏?我該如何修復這段代碼才能使它在firefox/opera/ie8 +中工作?
HTML
<div class="big">
<div class="box1">
<input type="text" name="input1" class="input1" />
</div>
<div class="box2">
<input type="text" name="input2" class="input1" />
</div>
<div class="box1">
<input type="text" name="input3" class="input1" />
</div>
<div class="box2">
<input type="text" name="input4" class="input1" />
</div>
</div>
CSS
.big {width:400px; height:4000px; float:left;}
.box1 {width:400px; height:1000px; background-color:#ccc; float:left;}
.box2 {width:400px; height:1000px; background-color:#ddd; float:left;}
.input1 {width:120px; height:16px; float:left;}
的JavaScript
var scrollKeys = new Array('0', '1000', '2000', '3000');
$('body').on('keyup', function(event) {
var keypressed = (event.which) ? event.which : event.keyCode;
var curScroll = $('body').scrollTop();
var keys = scrollKeys.length;
var moved = false;
for (i = 0; i < keys; i++) {
//console.log(scrollKeys[i]);
if (moved == false) {
if (keypressed == 40 && i != (keys - 1) && parseInt(scrollKeys[i]) < curScroll && parseInt(scrollKeys[i + 1]) > curScroll) {
$('body').animate({
scrollTop : (parseInt(scrollKeys[i + 1]))
}, 'fast', function() {});
console.log('down');
moved = true;
} else if (keypressed == 38 && i != 0 && parseInt(scrollKeys[i]) > curScroll && parseInt(scrollKeys[i - 1]) < curScroll) {
$('body').animate({
scrollTop : (parseInt(scrollKeys[i - 1]))
}, 'fast', function(){});
console.log('up');
moved = true;
}
}
}
});
請在此處添加您的代碼,並解釋問題所在。什麼是「scrollpoints」?代碼是做什麼的?發生了什麼,你期望發生什麼? –
編輯,謝謝:) –