【问题标题】:jQuery plugin imagemapster isn't doing anythingjQuery插件imagemapster没有做任何事情
【发布时间】:2012-11-05 17:05:00
【问题描述】:

我对 javascript 和 jQuery 还比较陌生,所以请记住,这可能是我看不到的明显解决方案。

我引用了 jQuery,它适用于其他事情。也许 imagemapster 参考有问题?我下载了它here 并像这样引用它。我也放了“imagemapster.min.js”,但是两个都放的时候不行,自己用的时候也不行。

<script type="text/javascript" src="../SiteAssets/css/jquery.imagemapster.js"></script>
<script type="text/javascript" src="../SiteAssets/css/jquery.imagemapster.min.js"></script>

我阅读了文档并几乎完全遵循了它(见下文),但它实际上对突出显示没有任何作用。相反,它会稍微弄乱他们的定位,从而破坏页面,这是没有意义的,因为 mapster 应该与定位无关。可点击区域也已向上移动并移出位置。它甚至没有给出任何错误;代码只是通过,除了我说的什么都没有发生。是的,我要强调的图像地图功能齐全,#downstairs、#upstairs 和 #offices 确实是图像标签的 ID,就像它们应该是的那样。

$(document).ready(function()
{
    $('#downstairs').mapster({
        fillOpacity: 0.5,
        mapKey: 'alt',
        isSelectable: false,
        render_highlight:
        {
            fillColor: '2aff00'
        }
    });

    $('#upstairs').mapster({
        fillOpacity: 0.5,
        mapKey: 'alt',
        isSelectable: false,
        render_highlight:
        {
            fillColor: '2aff00'
        }
    });

    $('#offices').mapster({
        fillOpacity: 0.5,
        mapKey: 'alt',
        isSelectable: false,
        render_highlight:
        {
        fillColor: '2aff00'
        }
    });
});

这是图像和地图的 html。他们三个都使用这种格式。

<div style="position:static;left:0px;top:0px">
<img id="downstairs" alt="downstairs" class="map" usemap="#downmap" style="visibility:visible;z-index:3; left: 10px; top: 54px;"
    src="sourceHere" width="1000" />
<map name="downmap" id="downmap">
    <!-- There is a large list of areas here that follow this format-->
    <area title="Portable 1" onclick="somefunction" coords="198,81,223,117" />
</map>
</div>

【问题讨论】:

  • 您应该使用jquery.imagemapster.jsjquery.imagemapster.min.js,但不能同时使用。第二个是相同代码的“缩小”版本,用于生产,而不是用于开发和测试。话虽如此,将 JS 代码放在 CSS 目录中有点不合逻辑。
  • 我怀疑这一点,但即使我只使用其中一个也不起作用。我不知道为什么,但我学校的每个人都出于某种原因将他们的 javascript 和 css 内容放在这个文件夹中。
  • 如果您的 JavaScript 控制台中没有错误,那么我可以看到两种可能性:选择器不起作用,或者插件不起作用。由于您使用的是$(document).ready,您确定具有这些 ID 的元素存在并且类型正确(img 而不是map)?
  • 插件应该可以工作;我得到它的地方有很多示范。插件有什么我不明白的地方,还是我只需要下载它然后参考它?是的,我确定我引用的是 img 的 id 而不是地图的 id。但是没有错误所以我不知道出了什么问题。
  • 我刚刚意识到它确实做了一些事情。由于某种原因,它改变了他们的定位风格,并稍微弄乱了页面。但是 mapster 根本不应该搞乱定位。它应该突出显示。那么为什么它对突出显示完全没有做任何事情却做一些完全不相关的事情呢?我将把它添加到上面的主要问题中。

标签: jquery image imagemap imagemapster


【解决方案1】:

检查您的地图区域是否定义了 href。例如:href="#"

【讨论】:

  • 非常感谢您的回答。你让我免于失去另一天并发疯。
【解决方案2】:

在将脚本移到地图下方之前,我遇到了类似的问题。我在文档中找不到它,但我记得读过要使插件工作,必须先加载图像。您仍然需要在头部调用插件,但您需要将 javascript 放在图像映射之后。

【讨论】:

    【解决方案3】:

    当我第一次尝试 ImageMapster 时,我没有正确设置路径。您的区域路径必须从上到下环绕对象,然后从左到右,然后从下到上,直到它在开始时再次相遇。一旦我这样做了,ImageMapster 就“做了一些事情”。在我找到正确的路径之前,它看起来就像什么都没有发生。

    您还必须告诉image 使用map

    <div id="mapblock">
        <img id="map_image" src="/images/map.png" usemap="#map_map"/>
        <map name="map_map">
            <area data-key="AZ" full="item" href="#" coords="... " shape="poly" />
        </map>
    </div>
    

    另外,您在代码中使用的 ID# 应该是 image 的 ID,而不是地图的 ID。

    另外,请确保您的地图区域在图像内(您可能必须设置图像的width,您知道)否则悬停效果将不会显示。当我第一次尝试时,在确定我的区域位于正确的位置之前,我什么也没看到。

    我写了一个 mini-tutorial here 用于使用 Photoshop 和 Illustrator 生成 image-map。这对我来说效果很好,image-map 就在完美的位置。

    [在 OP 发布 imagemap html 之后]

    &lt;area title="Portable 1" onclick="somefunction" coords="198,81,223,117" /&gt;

    啊,我想你的坐标很匹配。把坐标想象成一条线。您已经描述了行的开头加上另一点 - 您必须告诉它进行完整循环。试试这个,看看会发生什么:

    'coords="198,81, 223,117, 198,81"

    【讨论】:

    • 是的,我告诉图像使用地图,图像地图在没有 imagemapster 的情况下完美工作。你能澄清一下你所说的区域路径是什么意思吗,因为我根本不明白?
    • 我的意思是:第一个 x & y 坐标必须与最后一个相同。我也会在我的答案中添加更多细节。
    • 啊,你好像加了两次jquery。您只需要添加一个 - 对我来说只需 .min 版本就可以了。我认为添加两次可能会搞砸。
    • 问题不在于图像映射本身。这些区域都在图像内,我设置了图像的宽度,它本身就可以正常工作。所以我假设我正在正确地做那个区域路径的东西,不管它是什么。而且我一次只导入一种jquery。到目前为止,我一直在获取像素位置并将它们填充到 coords 属性中。您认为我应该通过进入 Photoshop 并剪切图像并按照您的教程来重做所有这些,还是您对我的做法有解决方案?请记住,有近 100 个不同的区域。
    • 也许您可以发布图像映射代码?如果没有看到它,我真的不能告诉你更多。我确实知道,当我尝试它时,除非我的 x-y 坐标正确,否则它不会起作用。当我在我的迷你教程中导出时,坐标与我的图像正确对齐 - 这是一个问题解决了,另一个是第一个坐标和最后一个坐标的方式,嗯,协调......
    【解决方案4】:

    对于 imagemapster 使用 jquery 2.1.3

    <!-- not this
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> -->
    
    <!-- use this one -->
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    

    用于图像映射器

    <script src="http://www.outsharked.com/scripts/jquery.imagemapster.js"></script>
    

    同样的情况发生在我的图像地图悬停在更改版本后最终无法显示或工作正常。

    参考下面的代码:

    <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> -->
    
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    
    <script src="http://www.outsharked.com/scripts/jquery.imagemapster.js"></script>
    
    <div style="clear: both; width: 500px; height: 50px; border: 1px solid black;" id="selections"></div>
    <img id="vegetables" src="http://www.outsharked.com/imagemapster/examples/images/vegetables.jpg" usemap="#veg">
    <map id="veg_map" name="veg">
        <area shape="poly" name="redpepper" coords="412,156, 427,161, 429,163, 444,153, 453,155, 457,159, 452,168, 459,174, 455,178, 460,179, 463,193, 460,203, 441,214, 436,217, 458,238, 469,257, 479,267, 478,269, 479,285, 458,309, 436,310, 414,305, 410,323, 397,334, 379,313, 389,316, 401,320, 399,305, 382,300, 371,290,
        367,296, 366,298,
        338,274, 332,272, 300,239, 316,238, 316,234, 313,230, 328,225, 333,213, 338,196, 333,181, 337,166, 345,145" href="#">
        <area shape="poly" name="celery" coords="147,131, 156,143, 163,146, 177,129, 175,138, 177,138, 182,144, 180,164, 148,169, 139,171, 144,180, 141,198, 139,208, 140,222, 127,237, 148,216, 163,212, 166,216, 160,223, 163,233, 153,281, 135,318, 129,313, 122,322, 117,320, 99,301, 98,293, 85,300, 80,303, 74,300,
        64,285, 76,272, 98,249,
        94,246, 72,261, 57,258, 62,251, 60,248, 47,228, 51,207, 71,195, 72,192, 38,202, 33,195, 30,173, 147,127" href="#">
        <area shape="poly" name="carrots" coords="175,74, 170,80, 154,85, 189,103, 190,111, 182,111, 179,98, 157,95, 142,111, 140,128, 38,169, 39,163, 74,143, 74,141, 56,135, 52,115, 79,111, 78,106, 63,98, 71,91, 81,88, 82,83, 91,75, 136,74, 174,70" href="#">
        <area shape="poly" name="asparagus" coords="224,25, 239,33, 244,36, 247,32, 327,23, 344,31, 343,36, 319,41, 315,42, 302,52, 272,61, 265,64, 270,73, 256,67, 248,60, 187,55, 192,58, 192,60, 184,64, 185,67, 176,67, 173,69, 142,67, 146,63, 135,59, 123,57, 142,44, 145,42, 143,39, 145,32, 207,38, 185,18, 212,19,
        220,22" href="#">
        <area shape="poly" name="squash" coords="388,26, 391,38, 394,43, 395,46, 425,58, 427,68, 428,92, 386,125, 371,132, 374,124, 377,118, 374,105, 371,105, 370,107, 364,102, 368,97, 356,87, 353,89, 348,86, 322,87, 314,100, 314,102, 282,85, 278,84, 273,74, 272,68, 319,46, 346,31" href="#">
        <area shape="poly" name="yellowpepper" coords="237,222, 246,254, 255,291, 262,323, 271,322, 285,350, 306,352, 307,365, 298,374, 285,366, 270,375, 249,378, 244,384, 231,389, 215,373, 210,363, 203,357, 199,368, 184,361, 175,349, 162,356, 140,340, 143,305, 161,252, 168,224, 188,240, 194,223, 198,232, 213,226, 224,224,
        229,218" href="#">
        <area shape="poly" name="broccoli" coords="341,89, 342,91, 360,95, 353,100, 360,104, 364,109, 368,115, 369,112, 367,110, 376,111, 373,123, 367,128, 362,128, 359,131, 348,134, 345,137, 340,138, 341,148, 334,167, 323,172, 315,165, 315,162, 312,165, 311,172, 293,167, 292,141, 298,136, 307,134, 322,129, 328,128,
        329,116, 319,109, 314,104,
        317,93, 332,84" href="#">
        <area shape="poly" name="broccoli" coords="328,178, 338,197, 337,202, 330,215, 328,217, 325,228, 307,234, 305,239, 287,225, 287,216, 286,212, 282,216, 277,212, 277,185, 284,179, 310,175" href="#">
        <area shape="poly" name="dip" coords="253,102, 277,100, 280,105, 290,107, 295,111, 304,130, 290,140, 287,147, 240,157, 238,159, 227,153, 203,146, 198,125, 200,116, 214,102, 231,102" href="#">
    
    </map>
    
    <script>
        // a cross reference of area names to text to be shown for each area
        var xref = {
            carrots: "<b>Carrots</b> are delicious and may turn your skin orange!",
            asparagus: "<b>Asparagus</b> is one of the first vegetables of the spring. " +
                "Being a dark green, it's great for you, and has interesting side effects.",
            squash: "<b>Squash</b> is a winter vegetable, and not eaten raw too much. Is that really squash?",
            redpepper: "<b>Red peppers</b> are actually the same as green peppers, they've just been left on " +
                "the vine longer. Delicious when fire-roasted.",
            yellowpepper: "Similar to red peppers, <b>yellow peppers</b> are sometimes sweeter.",
            celery: "<b>Celery</b> is a fascinating vegetable. Being mostly water, it actually takes your body " +
                "more calories to process it than it provides.",
            cucumbers: "<b>Cucumbers</b> are cool.",
            broccoli: "<b>Broccoli</b> is like a forest of goodness in your mouth. And very good for you. " +
                "Eat lots of broccoli!",
            dip: "Everything here is good for you but this one. <b>Don't be a dip!</b>"
        };
    
        var defaultDipTooltip = 'I know you want the dip. But it\'s loaded with saturated fat, just skip it ' +
            'and enjoy as many delicious, crisp vegetables as you can eat.';
    
        var image = $('#vegetables');
    
        image.mapster({
            fillOpacity: 0.4,
            fillColor: "d42e16",
            stroke: true,
            strokeColor: "3320FF",
            strokeOpacity: 0.8,
            strokeWidth: 4,
            singleSelect: true,
            mapKey: 'name',
            listKey: 'name',
            onClick: function(e) {
                var newToolTip = defaultDipTooltip;
    
                // update text depending on area selected
                $('#selections').html(xref[e.key]);
    
                // if Asparagus selected, change the tooltip
                if (e.key === 'asparagus') {
                    newToolTip = "OK. I know I have come down on the dip before, but let's be real. " +
                        "Raw asparagus without any of that delicious ranch and onion dressing " +
                        "slathered all over it is not so good.";
                }
                image.mapster('set_options', {
                    areas: [{
                        key: "dip",
                        toolTip: newToolTip
                    }]
                });
            },
            showToolTip: true,
            toolTipClose: ["tooltip-click", "area-click"],
            areas: [{
                key: "redpepper",
                fillColor: "ffffff"
            }, {
                key: "yellowpepper",
                fillColor: "000000"
            }, {
                key: "carrots",
                fillColor: "000000"
            }, {
                key: "dip",
                toolTip: defaultDipTooltip
            }, {
                key: "asparagus",
                strokeColor: "FFFFFF"
            }]
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2012-09-07
      • 2016-10-02
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多