这里是 jsfiddle: http: //jsfiddle.net/vZMyS/ 编辑:感谢 Buck Doyle,我用不同版本的 chrome 和 firefox/opera 脚本做了一个适当的 if else。现在它只是不适用于 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;
}
}
}
});
</p>