开发者

window.parent jQuery selector not working in IE8

开发者 https://www.devze.com 2022-12-10 11:46 出处:网络
This code below works fine $(\'html\').bind(\'mousewheel\', function(event, delta) { window.parent.scrollBy(-120 * delta,0);

This code below works fine

$('html').bind('mousewheel', function(event, delta) {
    window.parent.scrollBy(-120 * delta,0);
    return false;
});

but this one doesn't, can anyone tell me why. I'd love to know.

$(window.parent).bind('mousewheel', function(event, delta) {
    window.parent.scrollBy(-120 * delta,0);
    return false;
});

I'd like to clarify that the window selector doesn't w开发者_Python百科ork in this case either.


The problem may be that jQuery's event handler wrapper must use window.event to retrieve the current event in IE. If you set a handler from window A on an event in window B, the script in window A will be looking at window A's window.event, whilst the event is actually occurring on window B.

But there may be more issues than that, too. Cross-window/frame scripting is fraught with difficulties and jQuery is not particularly designed to work around them. To make jQuery work properly cross-frame you will generally need an instance of jQuery in both windows, and you should only use the corresponding instance of jQuery ($) to interact with each window.

eta re comment:

OK, having looked into mousewheel further, I don't know how your code can be working in Firefox (it certainly doesn't for me). Firefox doesn't support mousewheel events at all; instead it supports DOMMouseScroll events. Also for the other browsers that support mousewheel, it should be bound to a DOM Node rather than the window. So I guess what you're looking for is:

if ('MouseScrollEvent' in window) {
    $(document).bind('DOMMouseScroll', function(event) {
        return scroll(event.detail*-40);
    });
} else {
    $(document).bind('mousewheel', function(event) {
        return scroll(event.wheelDelta);
    });
}

function scroll(d) {
    window.scrollBy(-d, 0);
    return false;
};

(However in WebKit this will stop scrolling when the mouse moves out of the horizontal area corresponding to the viewport width. You may prefer to bind the events to the wider element like the div, if it fills the browser.)

0

精彩评论

暂无评论...
验证码 换一张
取 消