Highlighting an area of an image map based on search text

北城以北 提交于 2020-01-25 10:33:21

问题


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?



来源:https://stackoverflow.com/questions/18499036/highlighting-an-area-of-an-image-map-based-on-search-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!