【问题标题】:Adding hover effects for links in image map (area)为图像地图(区域)中的链接添加悬停效果
【发布时间】:2014-10-29 15:24:05
【问题描述】:

我只是 html 的初学者,我想为我的图像地图添加悬停效果。我已经搜索了很多解决方案,但我只提出了使用我不理解的脚本的解决方案。

这个脚本是如何工作的?我尝试使用

area:hover {
border: 1px solid white;
}

但它不起作用。

【问题讨论】:

  • 那是 CSS,不是脚本。它在您的样式表中。
  • 对不起,我不是故意添加“这个”这个词。哈哈对不起

标签: css hover imagemap


【解决方案1】:

你想要做的不是很简单,你必须结合 javascript 和 css 或 jquery 来给出你想要的效果。

这是因为area元素不直接接受hover,很遗憾...

但好消息是这里之前已经回答了:

How to apply Hovering on html area tag?

Visible Area tag?

【讨论】:

    【解决方案2】:

    以下 JS 和 CSS 使图像(地图)具有响应性并为区域添加悬停效果。

    var images = document.querySelectorAll('img[usemap]');
    images.forEach( function(image) {
        var mapid = image.getAttribute('usemap').substr(1);
        var imagewidth = image.getAttribute('width');
        var imageheight = image.getAttribute('height');
        var imagemap = document.querySelector('map[name="'+mapid+'"]');
        var areas = imagemap.querySelectorAll('area');
    
        image.removeAttribute('usemap');
        imagemap.remove();
    
        // create wrapper container
        var wrapper = document.createElement('div');
        wrapper.classList.add('imagemap');
        image.parentNode.insertBefore(wrapper, image);
        wrapper.appendChild(image);
    
        areas.forEach( function(area) {
            var coords = area.getAttribute('coords').split(',');
            var xcoords = [parseInt(coords[0]),parseInt(coords[2])];
            var ycoords = [parseInt(coords[1]),parseInt(coords[3])];
            xcoords = xcoords.sort(function(a, b){return a-b});
            ycoords = ycoords.sort(function(a, b){return a-b});
            wrapper.innerHTML += "<a href='"+area.getAttribute('href')+"' title='"+area.getAttribute('title')+"' class='area' style='left: "+((xcoords[0]/imagewidth)*100).toFixed(2)+"%; top: "+((ycoords[0]/imageheight)*100).toFixed(2)+"%; width: "+(((xcoords[1] - xcoords[0])/imagewidth)*100).toFixed(2)+"%; height: "+(((ycoords[1] - ycoords[0])/imageheight)*100).toFixed(2)+"%;'></a>";
        });
    });
    img {max-width: 100%; height: auto;}
    
    .imagemap {position: relative;}
    .imagemap img {display: block;}
    .imagemap .area {display: block; position: absolute; transition: box-shadow 0.15s ease-in-out;}
    .imagemap .area:hover {box-shadow: 0px 0px 1vw rgba(0,0,0,0.5);}
    <!-- Image Map Generated by http://www.image-map.net/ -->
    <img src="https://i.imgur.com/TwmCyCX.jpg" width="2000" height="2604" usemap="#image-map">
    <map name="image-map">
        <area target="" alt="Zirconia Abutment" title="Zirconia Abutment" href="/" coords="3,12,199,371" shape="rect">
        <area target="" alt="Gold Abutment" title="Gold Abutment" href="/" coords="245,12,522,371" shape="rect">
        <area target="" alt="CCM Abutment" title="CCM Abutment" href="/" coords="564,12,854,369" shape="rect">
        <area target="" alt="EZ Post Abutment" title="EZ Post Abutment" href="/" coords="1036,12,1360,369" shape="rect">
        <area target="" alt="Milling Abutment" title="Milling Abutment" href="/" coords="1390,12,1688,369" shape="rect">
        <area target="" alt="Angled Abutment" title="Angled Abutment" href="/" coords="1690,12,1996,371" shape="rect">
        <area target="" alt="Temporary Abutment [Metal]" title="Temporary Abutment [Metal]" href="/" coords="45,461,506,816" shape="rect">
        <area target="" alt="Fuse Abutment" title="Fuse Abutment" href="/" coords="1356,461,1821,816" shape="rect">
        <area target="" alt="Lab Analog" title="Lab Analog" href="/" coords="718,935,1119,1256" shape="rect">
        <area target="" alt="Transfer Impression Coping Driver" title="Transfer Impression Coping Driver" href="/" coords="8,1330,284,1731" shape="rect">
        <area target="" alt="Impression Coping [Transfer]" title="Impression Coping [Transfer]" href="/" coords="310,1330,697,1731" shape="rect">
        <area target="" alt="Impression Coping [Pick-Up]" title="Impression Coping [Pick-Up]" href="/" coords="1116,1330,1560,1733" shape="rect">
    </map>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 2013-01-30
      • 2017-07-04
      • 1970-01-01
      • 2015-01-12
      相关资源
      最近更新 更多