jquery图像地图悬停效果口吃进出

发布于 2024-08-22 22:10:37 字数 5208 浏览 5 评论 0原文

我正在整理一张美国地图,其中包含我需要以某种方式触发的悬停状态。我构建它的方式(并且之前已经在较小规模上完成过,没有问题)是对美国的主要图像进行图像映射,给每个区域一个 id,然后使用 jQuery 显示/隐藏绝对定位的 div 上方地图。每个 div 都包含一个带有自己的地图的图像,当鼠标悬停在该图像上时,只会发送一个 show() 命令,以便它保持原状。

我还有一些 jquery 隐藏 mouseleave 上的所有 div。问题是,悬停效果在这种疯狂的口吃效果中不断打开和关闭。我做错了什么?正如您所看到的,我尝试了各种功能,包括 mouseover、mouseenter 和悬停。

<div id="mapContainer">
    <img name="CAS_hotspot_exporter_v2" border="0" src="CAS_hotspot_exporter_v2.jpg" id="CAS_hotspot_exporter_v2" usemap="#m_CAS_hotspot_exporter_v2" alt="" />
    <map name="m_CAS_hotspot_exporter_v2" id="m_CAS_hotspot_exporter_v">
        <area shape="poly" coords="76,138,110,147,101,182,138,248,133,258,132,265,105,260,101,248,97,241,87,235,81,233,70,174" alt="california" id="californiaHotspot"/>
        <area shape="poly" coords="76,137,94,91,100,98,105,102,127,104,149,112,139,124,140,129,133,153" alt="oregon"id="oregonHotspot" />
        <area shape="poly" coords="94,89,96,64,109,70,101,78,106,81,112,75,111,60,153,70,145,109,105,100" alt="washington" id="washingtonHotspot" />
        <area shape="poly" coords="112,149,103,182,136,243,158,161,112,149" href="#" id="nevadaHotspot"/>
        <area shape="poly" coords="133,153,181,162,186,132,172,131,161,113,165,101,157,86,159,73,154,72,146,110,152,112,141,125" href="#" id="idahoHotspot" />
    </map>

    <div id="washington">
        <img src="state_pngs/washington.png" border="0" usemap="#washingtonMap">
        <map name="washingtonMap" id="washingtonMap">
            <area id="washingtonActiveArea" shape="poly" coords="105,83,105,82,106,82,106,83,110,81,112,71,112,71,111,72,112,72,111,72,111,71,111,70,112,70,112,71,112,71,113,70,111,68,111,68,111,68,111,68,111,67,112,67,112,68,112,68,113,65,113,64,113,64,112,62,112,61,112,62,112,61,154,72,147,108,121,104,120,105,119,105,119,104,118,104,118,104,113,103,113,103,112,103,112,102,105,102,104,102,101,101,101,94,99,93,98,93,98,91,97,91,97,91,96,91,96,90,94,90,94,85,95,85,94,88,94,89,95,86,95,86,95,86,96,85,96,86,97,86,96,85,94,84,95,82,97,82,96,82,96,81,95,80,95,81,95,81,95,82,96,64,100,67,100,67,106,70,107,70,109,71,109,71,109,71,109,71,109,71,109,72,110,74,108,75,108,75,108,74,106,77,106,77,104,79,106,79,106,79,105,79,104,79,108,76,108,76,110,74,110,74,110,76,109,77,109,78,108,77,108,77,108,78,109,78,108,81,108,81,107,81,108,80,108,80,106,81,107,81,106,82,106,82,106,82,106,81,106,81,107,80,106,80,103,82,104,82,105,83" />
        </map>
    </div>

    <div id="oregon">
        <img src="state_pngs/oregon.png" border="0" usemap="#oregonMap">
        <map name="oregonMap" id="oregonMap">
            <area id="oregonArea" shape="poly" coords="75,139,76,127,76,127,80,122,81,123,81,122,79,122,90,99,91,99,93,91,95,92,96,91,96,92,98,93,98,93,100,94,100,101,111,102,111,103,112,103,112,103,117,104,117,104,118,104,118,105,146,108,146,110,147,110,148,113,139,128,140,128,141,130,134,155,134,155,75,139" />
        </map>
    </div>

    <div id="idaho">
        <img src="state_pngs/idaho.png" border="0" usemap="#idahoMap">
        <map name="idahoMap" id="idahoMap">
            <area id="idahoArea" shape="poly" coords="184,164,135,154,142,129,141,127,140,127,149,111,148,109,147,109,154,71,161,73,160,90,168,103,168,103,168,103,166,113,164,114,164,116,166,118,169,116,169,116,171,124,171,125,172,127,173,127,173,127,173,128,174,132,175,133,181,132,181,132,183,132,183,133,183,133,186,133,187,131,189,134,189,134,184,164" />
        </map>
    </div>

    <div id="california">
        <img src="state_pngs/california.png" border="0" usemap="#californiaMap">
        <map name="californiaMap" id="californiaMap">
            <area id="californiaActiveArea" shape="poly" coords="24,7,58,16,49,51,90,120,84,138,57,136,30,103,17,42,17,23" />
        </map>
    </div>

    <div id="nevada">
        <img src="state_pngs/nevada.png" border="0" usemap="nevadaMap">
        <map name="nevadaMap" id="nevadaMap">
            <area id="nevadaArea" shape="poly" coords="148,219,144,230,144,230,143,229,140,228,138,240,138,240,137,242,101,183,110,148,160,159,148,219" />
        </map>
    </div>
</div>

还有我的 jQuery:

$(function() { // Equivalent to $(document).ready(function() 
  if (!$.browser.msie && !$.browser.version.substr(0,1)<7) {
   $("area").mouseleave(function(){
    $("#mapContainer div").animate({opacity: 1.0}, 100).fadeOut();
   });
  }

  $("#californiaHotspot, #californiaActiveArea").mouseover(function(){
   $("#california").show();
  });

  $("#idahoHotspot, #idahoActiveArea").mouseenter(function(){
   $("#idaho").fadeIn();
  });

  $("#nevadaHotspot, #nevadaActiveArea").mouseenter(function(){
   $("#nevada").fadeIn();
  });
  $("#nevadaActiveArea").mouseleave(function(){
   $("#nevada").hide();
  });

  $("#oregonHotspot, #oregonActiveArea").mouseover(function(){
   $("#oregon").fadeIn();
  });

  $("#washingtonHotspot, #washingtonActiveArea").mouseover(function(){
   $("#washington").fadeIn();
  });
});

I'm putting together a map of the US which has hover states that I need to trigger somehow. The way I've built it (and have done this before on a smaller scale with no problem) is to imagemap the main image of the USA, give each area an id, then use jQuery to show/hide an absolutely positioned div overtop of the map. Each div contains an image with its own map, which when moused over just sends a show() command so that it stays put.

I also have some jquery that hides all divs on mouseleave. Problem is, the hover effects keep turning on and off in this crazy stuttering effect. What am I doing wrong? As you can see I've experimented with various functions, including mouseover, mouseenter, and hover.

<div id="mapContainer">
    <img name="CAS_hotspot_exporter_v2" border="0" src="CAS_hotspot_exporter_v2.jpg" id="CAS_hotspot_exporter_v2" usemap="#m_CAS_hotspot_exporter_v2" alt="" />
    <map name="m_CAS_hotspot_exporter_v2" id="m_CAS_hotspot_exporter_v">
        <area shape="poly" coords="76,138,110,147,101,182,138,248,133,258,132,265,105,260,101,248,97,241,87,235,81,233,70,174" alt="california" id="californiaHotspot"/>
        <area shape="poly" coords="76,137,94,91,100,98,105,102,127,104,149,112,139,124,140,129,133,153" alt="oregon"id="oregonHotspot" />
        <area shape="poly" coords="94,89,96,64,109,70,101,78,106,81,112,75,111,60,153,70,145,109,105,100" alt="washington" id="washingtonHotspot" />
        <area shape="poly" coords="112,149,103,182,136,243,158,161,112,149" href="#" id="nevadaHotspot"/>
        <area shape="poly" coords="133,153,181,162,186,132,172,131,161,113,165,101,157,86,159,73,154,72,146,110,152,112,141,125" href="#" id="idahoHotspot" />
    </map>

    <div id="washington">
        <img src="state_pngs/washington.png" border="0" usemap="#washingtonMap">
        <map name="washingtonMap" id="washingtonMap">
            <area id="washingtonActiveArea" shape="poly" coords="105,83,105,82,106,82,106,83,110,81,112,71,112,71,111,72,112,72,111,72,111,71,111,70,112,70,112,71,112,71,113,70,111,68,111,68,111,68,111,68,111,67,112,67,112,68,112,68,113,65,113,64,113,64,112,62,112,61,112,62,112,61,154,72,147,108,121,104,120,105,119,105,119,104,118,104,118,104,113,103,113,103,112,103,112,102,105,102,104,102,101,101,101,94,99,93,98,93,98,91,97,91,97,91,96,91,96,90,94,90,94,85,95,85,94,88,94,89,95,86,95,86,95,86,96,85,96,86,97,86,96,85,94,84,95,82,97,82,96,82,96,81,95,80,95,81,95,81,95,82,96,64,100,67,100,67,106,70,107,70,109,71,109,71,109,71,109,71,109,71,109,72,110,74,108,75,108,75,108,74,106,77,106,77,104,79,106,79,106,79,105,79,104,79,108,76,108,76,110,74,110,74,110,76,109,77,109,78,108,77,108,77,108,78,109,78,108,81,108,81,107,81,108,80,108,80,106,81,107,81,106,82,106,82,106,82,106,81,106,81,107,80,106,80,103,82,104,82,105,83" />
        </map>
    </div>

    <div id="oregon">
        <img src="state_pngs/oregon.png" border="0" usemap="#oregonMap">
        <map name="oregonMap" id="oregonMap">
            <area id="oregonArea" shape="poly" coords="75,139,76,127,76,127,80,122,81,123,81,122,79,122,90,99,91,99,93,91,95,92,96,91,96,92,98,93,98,93,100,94,100,101,111,102,111,103,112,103,112,103,117,104,117,104,118,104,118,105,146,108,146,110,147,110,148,113,139,128,140,128,141,130,134,155,134,155,75,139" />
        </map>
    </div>

    <div id="idaho">
        <img src="state_pngs/idaho.png" border="0" usemap="#idahoMap">
        <map name="idahoMap" id="idahoMap">
            <area id="idahoArea" shape="poly" coords="184,164,135,154,142,129,141,127,140,127,149,111,148,109,147,109,154,71,161,73,160,90,168,103,168,103,168,103,166,113,164,114,164,116,166,118,169,116,169,116,171,124,171,125,172,127,173,127,173,127,173,128,174,132,175,133,181,132,181,132,183,132,183,133,183,133,186,133,187,131,189,134,189,134,184,164" />
        </map>
    </div>

    <div id="california">
        <img src="state_pngs/california.png" border="0" usemap="#californiaMap">
        <map name="californiaMap" id="californiaMap">
            <area id="californiaActiveArea" shape="poly" coords="24,7,58,16,49,51,90,120,84,138,57,136,30,103,17,42,17,23" />
        </map>
    </div>

    <div id="nevada">
        <img src="state_pngs/nevada.png" border="0" usemap="nevadaMap">
        <map name="nevadaMap" id="nevadaMap">
            <area id="nevadaArea" shape="poly" coords="148,219,144,230,144,230,143,229,140,228,138,240,138,240,137,242,101,183,110,148,160,159,148,219" />
        </map>
    </div>
</div>

And my jQuery:

$(function() { // Equivalent to $(document).ready(function() 
  if (!$.browser.msie && !$.browser.version.substr(0,1)<7) {
   $("area").mouseleave(function(){
    $("#mapContainer div").animate({opacity: 1.0}, 100).fadeOut();
   });
  }

  $("#californiaHotspot, #californiaActiveArea").mouseover(function(){
   $("#california").show();
  });

  $("#idahoHotspot, #idahoActiveArea").mouseenter(function(){
   $("#idaho").fadeIn();
  });

  $("#nevadaHotspot, #nevadaActiveArea").mouseenter(function(){
   $("#nevada").fadeIn();
  });
  $("#nevadaActiveArea").mouseleave(function(){
   $("#nevada").hide();
  });

  $("#oregonHotspot, #oregonActiveArea").mouseover(function(){
   $("#oregon").fadeIn();
  });

  $("#washingtonHotspot, #washingtonActiveArea").mouseover(function(){
   $("#washington").fadeIn();
  });
});

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

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

发布评论

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

评论(3

新人笑 2024-08-29 22:10:37

我建议使用 jquery .hover() 方法而不是 .mouseover().mouseenter().mouseleave () 方法来控制图像的显示/隐藏。您可以更可靠地控制 fadein() 和 fadeout() 方法。

http://api.jquery.com/hover/

I would suggest using the jquery .hover() method instead of the .mouseover(), .mouseenter(), .mouseleave() methods to control the showing/hiding of the images. You can control the fadein() and fadeout() methods much more reliably.

http://api.jquery.com/hover/

唔猫 2024-08-29 22:10:37

同意 jQuery 悬停是一个好主意。

另外,我会考虑以下内容:

  • 将底图 (CAS_hotspot_exporter_v2.jpg) 设为 div#mapContainer 的背景图像。
  • 使用透明的 1px 图像作为图像贴图。
  • 将宽度和高度设置为与底图图像相同。
  • 为淡入的 div(例如#california)指定 z-index -1。

完成所有这些意味着顶部始终有一个透明表面供鼠标“悬停”在其上,而其他 div 则被引入该表面“下方”。这样,当新的 div 淡入时,浏览器就不会触发 mouseleave 事件。

Agreed jQuery hover is a good idea.

Also I'd consider the following:

  • Make the base map (CAS_hotspot_exporter_v2.jpg) a background image of div#mapContainer.
  • Use a transparent 1px image for the image map.
  • Set width and height to the same as the base map image.
  • Give the divs that fade in (e.g. #california) a z-index of -1.

Doing all that means there's a transparent surface always on top for the mouse to "hover" over and the other divs are brought in "underneath" that surface. That way the browser shouldn't trigger a mouseleave event when the new div fades in.

挖鼻大婶 2024-08-29 22:10:37

顶部有透明层的好主意。

让国家图像与国家图像大小相同,并且国家图像除了国家本身之外都是透明的,这不是最简单的方法吗?

只是为了和弦的重用,当然取决于项目的性质 - 图像的重量。

Great idea with a transparent layer on top.

Would it not be the easyest way to make the state images same size as the country image with the state images being transparent except for the state itself.

Just for the reuse of chords and of course depending on the nature of the project - how heavy the images become.

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