【问题标题】:How can I check if my mouse x position is in my image?如何检查我的鼠标 x 位置是否在我的图像中?
【发布时间】:2016-02-05 10:42:33
【问题描述】:

我有一个 onmouseenter 事件,其中我通过 jquery 选择器 $('.imageTile') 获得了一组图像。在 Chrome 中,我可以这样做来获取我真正感兴趣的元素的索引:

for(var i = 0; i < $('.imageTile').length; i++)
{
   if(e.y >= $('.imageTile')[i].y && e.y <= $('.imageTile')[i].y + $('.imageTile')[i].height) {
       index = i;
       break;
   }
}

但在 IE 中,这不起作用。我需要使用一些奇怪的技巧吗?

【问题讨论】:

  • 你到底想做什么?可能有一种更简单的方法来做到这一点。您是否尝试获取被悬停的图像的索引?
  • $('.imageTile').on('hover', function() { console.log($(this).index(); });
  • 我也想把 X 作为某个点,因为我想在我悬停的图像旁边设置一个图像,最好将顶部设置为一样。

标签: javascript jquery asp.net internet-explorer mouse-position


【解决方案1】:

我认为这会起作用,但我还没有测试过。

var index = -1;
$(function(){
    $('.imageTile img').hover(function(){
        index = $('.imageTile').index($(this));
    },
    function(){ 
        index = -1; // reset index on mouseout
    });
});

你一定喜欢 jQuery,https://api.jquery.com/index/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2015-02-04
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多