Highlighting an area of an image map based on sear

2019-09-20 06:49发布

问题:

This is a new question feeding from another question that was just answered here.

I am working to highlight a <div> based on search text. We've accomplished that, thanks to Alex.

Now, I'm trying to apply the same concept to mapped coordinates on an image map.

There's a jsfiddle here.

Here's the JS(jQuery 1.10.2)...

function doSearch(text) {
    $('#content div').removeClass('highlight');
    $('#content div:contains(' + text + ')').addClass('highlight');
}

回答1:

If you want a method without SVG, you can use the Maphilight jQuery plugin (GitHub).

I have updated your jsFiddle.

function doSearch(text) {
    $('#content div').removeClass('highlight');
    $('#content div:contains(' + text + ')').addClass('highlight');

    $('#Map area').mouseout(); 
    $('#Map area[data-text*="' + text + '"]').mouseover(); 
}
$(function() {
    $('#imgmap').maphilight({ stroke: false, fillColor: "ffff00", fillOpacity: 0.6 });
});

Note: For a better result just use a bigger image, because your bunny.jpg is too small and you have forced its size with height/width attributes.



回答2:

It is not possible with image-maps and area elements, because those are non visible elements, that cannot have child elements, nor styles. You would have to do it a lot more complicated like described here

But it is possible using modern embeded SVGs - Almost every browser does support it nowadays. Even IE.

I tested it with Chromium and Firefox.

It cannot be done with the help of jQuery as far as I know but with usual Javascript. The key is:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="663px" height="663px">
        <image xlink:href="http://webfro.gs/south/kb2/images/bunny.jpg" x="0" y="0" width="663" height="663" />
        <circle class="office" cx="504" cy="124" r="94" />
        <circle class="fire-exit" cx="168" cy="150" r="97" />
        <circle class="main-exit" cx="378" cy="589" r="48" />
</svg>

_

var svgns="http://www.w3.org/2000/svg";
var areas = document.getElementsByTagNameNS(svgns, 'circle');
$(areas).each(function(elem) {
    if(areas[elem].className.baseVal === text) {
        areas[elem].className.baseVal += ' highlightsvg';
    } else {
        areas[elem].className.baseVal = areas[elem].className.baseVal.replace(' highlightsvg', '');
    }

});

See here in the JSFiddle. Is that the way you want it?