这是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;
}
}
}
});