是否可以使用CSS在鼠标悬停在图像映射上?

fmz*_*fmz 47 html css

我在网页上有一个图像,也需要链接.我正在使用图像映射来创建链接,我想知道是否有一种方法可以在鼠标悬停时为区域形状设置样式以进行轻微的交互.这可能吗?

我尝试了这个没有成功:

HTML

<img src="{main_photo}" alt="locations map"  usemap="#location-map" />
<map name="location-map">
    <area shape="rect" coords="208,230,290,245" href="{site_url}locations/grand_bay_al" />
    <area shape="rect" coords="307,214,364,226" href="{site_url}locations/mobile_al" />
    <area shape="rect" coords="317,276,375,290" href="{site_url}locations/loxley_al" />
</map>
Run Code Online (Sandbox Code Playgroud)

CSS

area { border: 1px solid #d5d5d5; }
Run Code Online (Sandbox Code Playgroud)

有什么建议?

ptr*_*iek 42

仅限CSS:

在去超级市场的​​路上考虑它,你当然也可以跳过整个图像映射的想法,并利用:hover图像顶部的元素(将div更改为a-blocks).这让事情变得简单得多,不需要jQuery ......

简短说明:

  • 图像在底部
  • 带显示的2 xa:块和绝对定位+不透明度:0
  • 悬停时将不透明度设置为0.2

例:

.area {
    background:#fff;
    display:block;
    height:475px;
    opacity:0;
    position:absolute;
    width:320px;
}
#area2 {
    left:320px;
}
#area1:hover, #area2:hover {
    opacity:0.2;
}
Run Code Online (Sandbox Code Playgroud)
<a id="area1" class="area" href="#"></a>
<a id="area2" class="area" href="#"></a>
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Saimiri_sciureus-1_Luc_Viatour.jpg/640px-Saimiri_sciureus-1_Luc_Viatour.jpg" width="640" height="475" />
Run Code Online (Sandbox Code Playgroud)

使用jQuery的原始答案

我刚刚用jQuery创建了类似的东西,我不认为它只能用CSS来完成.

简短说明:

  • 图像在底部
  • 具有翻转(图像或颜色)的Div具有绝对定位+显示:无
  • 实际的透明gif #map位于顶部(绝对位置)(以防止mouseout在翻转出现时调用)
  • jQuery用于显示/隐藏div

    $(document).ready(function() {
        if($('#location-map')) {
            $('#location-map area').each(function() {
                var id = $(this).attr('id');
                $(this).mouseover(function() {
                    $('#overlay'+id).show();
                    
                });
                
                $(this).mouseout(function() {
                    var id = $(this).attr('id');
                    $('#overlay'+id).hide();
                });
            
            });
        }
    });
Run Code Online (Sandbox Code Playgroud)
body,html {
    margin:0;
}
#emptygif {
    position:absolute;
    z-index:200;
}
#overlayr1 {
    position:absolute;
    background:#fff;
    opacity:0.2;
    width:300px;
    height:160px;
    z-index:100;
    display:none;
}
#overlayr2 {
    position:absolute;
    background:#fff;
    opacity:0.2;
    width:300px;
    height:160px;
    top:160px;
    z-index:100;
    display:none;
}
Run Code Online (Sandbox Code Playgroud)
<img src="http://www.tfo.be/jobs/axa/premiumplus/img/empty.gif" width="300" height="350" border="0" usemap="#location-map" id="emptygif" />
<div id="overlayr1">&nbsp;</div>
<div id="overlayr2">&nbsp;</div>
<img src="http://2.bp.blogspot.com/_nP6ESfPiKIw/SlOGugKqaoI/AAAAAAAAACs/6jnPl85TYDg/s1600-R/monkey300.jpg" width="300" height="350" border="0" />
<map name="location-map" id="location-map">
  <area shape="rect" coords="0,0,300,160" href="#" id="r1" />
  <area shape="rect" coords="0,161,300,350" href="#" id="r2"/>
</map>
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..

  • 修理小提琴会很棒! (2认同)

小智 8

使用伪元素.

HTML:

<div class="image-map-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/8/83/FibonacciBlocks.png" alt="" usemap="#image-map" />
    <div class="map-selector"></div>
</div>

<map name="image-map" id="image-map">
    <area alt="" title="" href="#" shape="rect" coords="54,36,66,49" />
    <area alt="" title="" href="#" shape="rect" coords="72,38,83,48" />
    <area alt="" title="" href="#" shape="rect" coords="56,4,80,28" />
    <area alt="" title="" href="#" shape="rect" coords="7,7,45,46" />
    <area alt="" title="" href="#" shape="rect" coords="10,59,76,125" />
    <area alt="" title="" href="#" shape="rect" coords="93,9,199,122" />
</map>
Run Code Online (Sandbox Code Playgroud)

一些CSS:

.image-map-container {
    position: relative;
    display:inline-block;
}
.image-map-container img {
    display:block;
}
.image-map-container .map-selector {
    left:0;top:0;right:0;bottom:0;
    color:#546E7A00;
    transition-duration: .3s;
    transition-timing-function: ease-out;
    transition-property: top, left, right, bottom, color;
}
.image-map-container .map-selector.hover {
    color:#546E7A80;
}

.map-selector:after {
    content: '';
    position: absolute;
    top: inherit;right: inherit;bottom: inherit;left: inherit;
    background: currentColor;
    transition-duration: .3s;
    transition-timing-function: ease-out;
    transition-property: top, left, right, bottom, background;
    pointer-events: none;
}
Run Code Online (Sandbox Code Playgroud)

JS:

$('#image-map area').hover(
    function () { 
        var coords = $(this).attr('coords').split(','),
            width = $('.image-map-container').width(),
            height = $('.image-map-container').height();
        $('.image-map-container .map-selector').addClass('hover').css({
            'left': coords[0]+'px',
            'top': coords[1] + 'px',
            'right': width - coords[2],
            'bottom': height - coords[3]
        })
    },
    function () { 
        $('.image-map-container .map-selector').removeClass('hover').attr('style','');
    }
)
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/79ebt32x/1/


Spa*_*ers 5

我认为这不可能只使用CSS(至少不是跨浏览器),但jQuery插件ImageMapster会做你想要的.您可以在图像映射上为悬停/活动状态绘制轮廓,着色或使用替代图像.

http://www.outsharked.com/imagemapster/examples/usa.html


Jas*_*ban 5

这是一个纯 CSS,它使用+下一个同级选择器:hoverpointer-events。从技术上讲,它不使用图像映射,但这个rect概念完全延续下来:

.hotspot {
    position: absolute;
    border: 1px solid blue;
}
.hotspot + * {
    pointer-events: none;
    opacity: 0;
}
.hotspot:hover + * {
    opacity: 1.0;
}
.wash {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(255, 255, 255, 0.6);
}
Run Code Online (Sandbox Code Playgroud)
<div style="position: relative; height: 188px; width: 300px;">
    <img src="http://demo.cloudimg.io/s/width/300/sample.li/boat.jpg">
        
    <div class="hotspot" style="top: 50px; left: 50px; height: 30px; width: 30px;"></div>
    <div>
        <div class="wash"></div>
        <div style="position: absolute; top: 0; left: 0;">A</div>
    </div>
        
    <div class="hotspot" style="top: 100px; left: 120px; height: 30px; width: 30px;"></div>
    <div>
        <div class="wash"></div>
        <div style="position: absolute; top: 0; left: 0;">B</div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)