【问题标题】:Enabling Highlight On Click for jQuery Map Highlighting为 jQuery 地图突出显示启用单击时突出显示
【发布时间】:2011-06-11 09:54:11
【问题描述】:

我正在使用 jQuery Highlight 插件制作图表 (http://davidlynch.org/js/maphilight/docs/),我目前拥有可单击的图表,并根据您单击的区域(如简单的选项卡)加载内容。

但是,我需要地图在点击时突出显示并禁用任何其他突出显示的区域。现在,我可以使该区域在单击时突出显示,但不能禁用任何现有的突出显示。我也想让图表在悬停时切换内容,但这并不像点击时突出显示那么重要。

我已准备好当前版本的演示: http://jsfiddle.net/keith/PVpgK/

或全屏结果: http://jsfiddle.net/keith/PVpgK/embedded/result/

提前感谢您的帮助

【问题讨论】:

    标签: jquery plugins highlight


    【解决方案1】:

    您需要遍历其他区域并关闭 alwayson,以便在新点击时取消突出显示最后一次点击。试试这样的:

      //map clicks
       $(".tabs area").click(function(){
    
           //AREAS LOOP:
           $(".tabs area").each(function(){
               var d = $(this).data('maphilight') || {};
               if(d.alwaysOn == true){
                 d.alwaysOn = false;  
               }
             });
    
    //DISPLAY CURRENT LI FUNCTION:
    $('.tabs area').mouseover(function(){
    
         var thisTarget = $(this).attr("href");
    
        if(thisTarget){      
            $('#test').innerHTML = thisTarget;  
        }
         $(this).parents(".tabs").find('area.current').removeClass('current');
    
         $(this).addClass('current');
    
         $(this).parents(".tabs").nextAll(".tab-content").children(":visible").fadeOut(1, function() {
             $(thisTarget).fadeIn("fast");
         });
    
    });
       //This block is what creates highlighting by trigger the "alwaysOn",
       var data = $(this).data('maphilight') || {};
       data.alwaysOn = true;  //NOTICE I MADE THIS ALWAYS TRUE
       $(this).data('maphilight', data).trigger('alwaysOn.maphilight');
       //there is also "neverOn" in the docs, but not sure how to get it to work
    
    
       if ($(this).hasClass("current") == false)
       {
           var thisTarget = $(this).attr("href");
    
           $(this).parents(".tabs").find('area.current').removeClass('current');
    
           $(this).addClass('current');
    
           $(this).parents(".tabs").nextAll(".tab-content").children(":visible").fadeOut(1, function() {
               $(thisTarget).fadeIn("fast");
           });
    
       }
       return false;
      });
    

    【讨论】:

    • @wajiw 如果我想在鼠标悬停时显示内容,但在单击之前不让它“粘住”,我该如何将其添加到鼠标悬停功能中?
    • 如果您在单击每个区域时注意到,它会切换到与该区域关联的内容。我试图在悬停和单击时更改内容,以便用户可以看到不同的内容,然后单击以使其“粘贴”...这有意义吗?
    • 那么你想显示对应悬停元素的 li 吗?我不确定,让我看看。
    • 对,显示与悬停元素相对应的 li,但如果您单击它,它会停留在该内容上(就像在上面的代码中所做的那样......再次感谢)
    • 您必须在代码中添加另一个鼠标悬停,以分别更新 li 元素。我更新了我的代码以显示一个在“显示当前 LI 函数”下执行此操作的函数
    【解决方案2】:

    一种更有效的方法是在单击时添加一个类,并在单击下一个位置时将其删除。添加类后,您可以操作。这样,如果您有成百上千个区域(就像我的地图那样),页面在尝试循环浏览每个区域时不会锁定。

    以下链接将带您到解释这一点的答案。 https://stackoverflow.com/a/8397900/1101054

    【讨论】:

      【解决方案3】:

      HTML 代码:

      <img src="img.jpg" alt="img" usemap="#map">    
      
      <map name="map">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
       <area shape="poly" coords="[yourCoords]" alt="img" href="javascript:void(0);" class="punto">
      </map>
      

      JS 代码

      $(function(){
         $('.map').maphilight({
             fillColor: 'ff0000',
             fillOpacity:0.4,
             stroke:false,
             neverOn:true
         });
      
         $('.punto').click(function(){
             var data = $(this).data('maphilight') || {};
             data.alwaysOn=true;
             $(this).data('maphilight', data).trigger('alwaysOn.maphilight');;
         });
      })
      

      【讨论】:

      • 嗨 :) 感谢您提供超级保护程序代码。您能否添加一个代码,以便我们可以在地图区域之间切换。即当一个区域被选中时,另一个应该被取消选择
      猜你喜欢
      • 2020-11-12
      • 2013-05-20
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      相关资源
      最近更新 更多