Jquery函数.offset在IE中输出的问题

发布于 2024-10-12 00:17:34 字数 2450 浏览 3 评论 0原文

我对 jquery 和 javascript 以及网站整体开发都很陌生,并且我在 .offset 函数方面遇到了问题。我有以下代码在 chrome 和 FF 上工作正常,但在 IE 上不起作用:

$(document).keydown(function(k){  
    var keycode=k.which;  
    var posk=$('html').offset();  
    var centeryk=screen.availHeight*0.4;  
    var centerxk=screen.availWidth*0.4;  
    $("span").text(k.which+","+posk.top+","+posk.left);  
    if (keycode==37){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollLeft:-1*posk.left-centerxk})  
    };  
    if (keycode==38){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollTop:-1*posk.top-centeryk})  
    };  
    if (keycode==39){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollLeft:-1*posk.left+centerxk})   
    };
    if (keycode==40){
        k.preventDefault();
        $("html,body").stop().animate({scrollTop:-1*posk.top+centeryk})
    };  
});  

我想要它做的是使用箭头键将窗口滚动设定的百分比,所以我的想法是找到左上角的当前坐标并添加相对于用户屏幕的百分比并设置滚动动画,以便内容不会跳跃并且用户不会从原来的位置失去焦点。 $("span").text 只是为了让我知道发生了什么,并且在代码完成时将其转换为注释。

所以这就是发生的情况,在 Chrome 和 Firefox 上,位置变量的 $("span").text 的输出是正确的,从 0,0 开始,并且始终显示在坐标中滚动了多少内容,但在 IE 上它从 -2,-2 开始并且永远不会离开它,即使我手动滚动窗口直到它结束并尝试使用右箭头键它仍然会返回 -2,-2 的初始值并向后滚动到一开始。

我尝试用 document.body.scrollLetf 和scrollTop 的偏移量替换,但结果是相同的,只是这次坐标是0,0。我做错了什么吗?或者这是一些 IE 错误?有没有办法解决它或我可以使用其他一些函数并达到相同的结果?

另一方面,我在网站的这一部分为用户做了另外两个导航选项,一个是单击并拖动屏幕上的任意位置以移动它:

$("html").mousedown(function(e)
{
    var initx=e.pageX
    var inity=e.pageY
    $(document).mousemove(function(n)
    {   
        var x_inc= initx-n.pageX;
        var y_inc= inity-n.pageY;
        window.scrollBy(x_inc*0.7,y_inc*0.7);
        initx=n.pageX;
        inity=n.pageY
        //$("span").text(initx+ "," +inity+ "," +x_inc+ "," +y_inc+ "," +e.pageX+ "," +e.pageY+ "," +n.pageX+ "," +n.pageY);

    // cancel out any text selections
    document.body.focus();

    // prevent text selection in IE
    document.onselectstart = function () { return false; };
    // prevent IE from trying to drag an image
    document.ondragstart = function() { return false; };

    // prevent text selection (except IE)
    return false;
    });
});

$("html").mouseup(function()
{
    $(document).unbind('mousemove');
});

我没有编写的这段代码的唯一部分是防止文本选择行,这些是我在关于单击和拖动对象的教程中找到的,无论如何,这段代码在 Chrome、FireFox 和 IE 上运行良好,尽管在 Firefox 和 IE 上,拖动时更经常发生一些运动故障,有时看起来“滚动”是一个小锯齿状的,它只是一个视觉上的东西,并没有那么重要,但如果有办法阻止它,我想知道。

I'm new to jquery and javascript, and to web site developing overall, and I'm having a problem with the .offset function. I have the following code working fine on chrome and FF but not working on IE:

$(document).keydown(function(k){  
    var keycode=k.which;  
    var posk=$('html').offset();  
    var centeryk=screen.availHeight*0.4;  
    var centerxk=screen.availWidth*0.4;  
    $("span").text(k.which+","+posk.top+","+posk.left);  
    if (keycode==37){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollLeft:-1*posk.left-centerxk})  
    };  
    if (keycode==38){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollTop:-1*posk.top-centeryk})  
    };  
    if (keycode==39){  
        k.preventDefault();  
        $("html,body").stop().animate({scrollLeft:-1*posk.left+centerxk})   
    };
    if (keycode==40){
        k.preventDefault();
        $("html,body").stop().animate({scrollTop:-1*posk.top+centeryk})
    };  
});  

hat I want it to do is to scroll the window a set percentage using the arrow keys, so my thought was to find the current coordinates of the top left corner of the document and add a percentage relative to the user screen to it and animate the scroll so that the content don't jump and the user looses focus from where he was. The $("span").text are just so I know what's happening and will be turned into comments when the code is complete.

So here is what happens, on Chrome and Firefox the output of the $("span").text for the position variables is correct, starting at 0,0 and always showing how much of the content was scrolled in coordinates, but on IE it starts on -2,-2 and never gets out of it, even if I manually scroll the window until the end of it and try using the right arrow key it will still return the initial value of -2,-2 and scroll back to the beggining.

I tried substituting the offset for document.body.scrollLetf and scrollTop but the result is the same, only this time the coordinates are 0,0. Am I doing something wrong? Or is this some IE bug? Is there a way around it or some other function I can use and achieve the same results?

On another note, I did other two navigating options for the user in this section of the site, one is to click and drag anywhere on the screen to move it:

$("html").mousedown(function(e)
{
    var initx=e.pageX
    var inity=e.pageY
    $(document).mousemove(function(n)
    {   
        var x_inc= initx-n.pageX;
        var y_inc= inity-n.pageY;
        window.scrollBy(x_inc*0.7,y_inc*0.7);
        initx=n.pageX;
        inity=n.pageY
        //$("span").text(initx+ "," +inity+ "," +x_inc+ "," +y_inc+ "," +e.pageX+ "," +e.pageY+ "," +n.pageX+ "," +n.pageY);

    // cancel out any text selections
    document.body.focus();

    // prevent text selection in IE
    document.onselectstart = function () { return false; };
    // prevent IE from trying to drag an image
    document.ondragstart = function() { return false; };

    // prevent text selection (except IE)
    return false;
    });
});

$("html").mouseup(function()
{
    $(document).unbind('mousemove');
});

The only part of this code I didn't write was the preventing text selection lines, these ones I found in a tutorial about clicking and draging objects, anyway, this code works fine on Chrome, FireFox and IE, though on Firefox and IE it's more often to happen some moviment glitches while you drag, sometimes it seems the "scrolling" is a litlle jagged, it's only a visual thing and not that much significant but if there's a way to prevent it I would like to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-10-19 00:17:34

好吧,我刚刚解决了我的问题,将 posk 变量更改为

    var poskt=$(document).scrollTop(); 

并为scrollLeft 添加一个新的 var,这样代码在 Chrome、FF 和 Internet Explorer 上的行为相同

okay I just solved my problem changing the posk variable to

    var poskt=$(document).scrollTop(); 

and adding an new var for scrollLeft, this way the code behave the same on Chrome, FF and Internet Explorer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文