简体   繁体   English

Javascript:mousemove事件处理程序未被触发

[英]Javascript: mousemove event handler not being triggered

I'm working on a set of e-learning modules that must be accessible in IE8 (due to legacy hardware in the organisation). 我正在开发一套必须在IE8中访问的电子学习模块(由于组织中的传统硬件)。

Everything works fine in IE9>. 在IE9中一切正常>。 In IE8 the mouseover event handler is not firing. 在IE8中,mouseover事件处理程序未触发。 Here's the relevant js: 这是相关的js:

$(document).ready(function() {
    $imageOptions = $('.multi-choice-image-container');
    $imageOptions.each(function() {
        $(this).hover(addHover, removeHover);
    })
});

function addHover(ev) {
    ev = ev || window.event;
    // add conditional class
    var $image;
    if (ev.target.tagName=="IMG") {
        $image = $(ev.target).parent(); // if event has been triggered from image as opposed to the containing div, get the parent object
    } else {
        $image = $(ev.target);
    }
    if (!$image.hasClass('hover-magnification')) {
        $image.addClass('hover-magnification');
        $hoveredImageContainer = $image;
        hoveredImageRect = $hoveredImageContainer[0].getBoundingClientRect();
        var hoveredImageRectWidth = hoveredImageRect.right - hoveredImageRect.left;
        var hoveredImageRectHeight = hoveredImageRect.bottom - hoveredImageRect.top;
        hoveredXMultiplier = (400 - hoveredImageRectWidth) / hoveredImageRectWidth;
        hoveredYMultiplier = (800 - hoveredImageRectHeight) / hoveredImageRectHeight;
        $hoveredImage = $hoveredImageContainer.find('.multi-choice-image');
    }
    if (document.body.addEventListener) {
        document.body.addEventListener('mousemove', mouseMoveHandler);
    } else {
        document.body.attachEvent('mousemove', mouseMoveHandler);
    }
}

function removeHover(ev) {
    ev = ev || window.event;
    var $image;
    if (ev.target.tagName=="IMG") {
        $image = $(ev.target).parent();
    } else {
        $image = $(ev.target);
    }
    $image.removeClass('hover-magnification');
    if (document.body.removeEventListener) {
        document.body.removeEventListener('mousemove', mouseMoveHandler);
    } else {
        document.body.detachEvent('mousemove', mouseMoveHandler);
    }
    $hoveredImageContainer = null;
}

function mouseMoveHandler(ev) {
    console.log("this function is not firing");
    ev = ev || window.event;
    if ($hoveredImageContainer!=null) {
        var scroll = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
        var xPos = ev.pageX - hoveredImageRect.left;
        var yPos = ev.pageY - (hoveredImageRect.top+scroll);
        $hoveredImage.css('left', -((xPos) * hoveredXMultiplier) +'px').css('top', -((yPos) * hoveredYMultiplier) + 'px');
    }
}

And some VERY simplified html (with the image urls altered, obviously): 还有一些非常简化的html(显然图像网址被修改):

<div class="multi-choice-image-container" id="option1">
    <img class="multi-choice-image" src="url/to/image.png">
</div>
<div class="multi-choice-image-container" id="option2">
    <img class="multi-choice-image" src="url/to/image.png">
</div>
<div class="multi-choice-image-container" id="option3">
    <img class="multi-choice-image" src="url/to/image.png">
</div>
<div class="multi-choice-image-container" id="option4">
    <img class="multi-choice-image" src="url/to/image.png">
</div>

And finally the css that makes the image full size (there are also other rules that set overflow as hidden so the zoomed image doesn't cover any other elements): 最后是使图像全尺寸的css(还有其他规则将溢出设置为隐藏,因此缩放的图像不会覆盖任何其他元素):

.hover-magnification img {
    width: auto;
    height: 800px;
    position: absolute;
}

I've included the class-adding js lines because I can see that THOSE lines ARE being applied - the class of the contained images changes and it zooms to full size. 我已经包含了类添加js行,因为我可以看到应用了这些行 - 所包含图像的类改变并且它缩放到完整大小。 It's the mouseMoveHandler function that is NOT getting triggered so the images are not adjusting their position based on the mouse position. 这是不会被触发的mouseMoveHandler函数,因此图像不会根据鼠标位置调整其位置。

OK, so my simple question, after all of that is WHY is the mousemove handler not triggering in IE8? 好的,所以我的简单问题,毕竟这是为什么鼠标移动处理程序不会在IE8中触发? (It works fine in 9+ and other browsers). (它在9+和其他浏览器中工作正常)。

OK. 好。 For anyone having the same problem, it looks like, instead of triggering 'mousemove', IE8 needs to trigger 'onmousemove'. 对于任何有相同问题的人来说,看起来,IE8需要触发'onmousemove',而不是触发'mousemove'。 So: 所以:

if (document.body.addEventListener) {
    document.body.addEventListener('mousemove', mouseMoveHandler);
} else {
    document.body.attachEvent('onmousemove', mouseMoveHandler);
}

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

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