简体   繁体   English

Javascript代码在IE中不起作用

[英]Javascript code doesnt work in IE

Here is jsfiddle: http://jsfiddle.net/vZMyS/ EDIT: thanks to Buck Doyle i made an appropriate if else with different versions of the script for chrome and firefox/opera. 这是jsfiddle: http : //jsfiddle.net/vZMyS/编辑:感谢Buck Doyle,如果使用其他版本的chrome和firefox / opera脚本,我做了一个适当的建议。 Now it just doesnt work with IE. 现在它不适用于IE。

Script is supossed to make user able to scroll down/up to scrollpoints that are specified in the "var scrollKeys", using up/down keyboard keys, it does work in chrome, but it doesnt in other browsers. 可以通过脚本来使用户能够使用上/下键盘键向下/向上滚动到“ var scrollKeys”中指定的滚动点,它确实适用于chrome,但不适用于其他浏览器。

Any idea whats wrong here? 知道这里有什么问题吗? How could i fix this code in order to make it work in firefox/opera/ie8+? 我如何解决此代码,以使其在firefox / opera / ie8 +中起作用?

html 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 的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 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;
            }
        }
    }
});​

Here is a fork of your fiddle that works in Firefox. 是在Firefox中工作的小提琴。 But now it doesn't work in Chrome. 但是现在它在Chrome中不起作用。 The important thing is that I changed it to scroll html instead of body . 重要的是我将其更改为滚动html而不是body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM