简体   繁体   English

在区域悬停时更改img src

[英]Change img src on area hover

<img src="img/clear-map-bg.png" alt="" usemap="my-map" />
<map name="my-map">
    <area shape="rect" coords="24,39,108,187,25,186,24,39" alt="" class="img1" />
    <area shape="rect" coords="173,117,230,116,238,276,176,289" alt="" class="img2" />
    <area shape="rect" coords="226,109,287,104,291,256,231,270" alt="" class="img3" />
    <area shape="rect" coords="283,102,340,97,346,269,286,252" alt="" class="img4" />
</map>

I want to change the path of the image on mouseover the area. 我想在鼠标悬停在该区域上时更改图像的路径。 I have 4 different images 我有4张不同的图片

HTML: HTML:

<img src="https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg"/>

  <div id="wrapper">
    <area id="img1" />
    <area id="img2" />
    <area id="img3" />
    <area id="img4" />
  </div>

CSS: CSS:

area {
  width: 50px;
  height: 50px;
  display: inline-block;
  padding: 0px;
}

#img1 {
  background-color: red;
}

#img2 {
  background-color: yellow;
}

#img3 {
  background-color: green;
}

#img4 {
  background-color: orange;
}

JS: JS:

$(function(){

  $("#img1").hover(function(){
    $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/sauro/128.jpg');
  }, function(){                      $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg');
  });

  $("#img2").hover(function(){
    $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/zack415/128.jpg');
  }, function(){                      $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg');
  });

  $("#img3").hover(function(){
    $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/abinav_t/128.jpg');
  }, function(){                      $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg');
  });

  $("#img4").hover(function(){
    $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/shalt0ni/128.jpg');
  }, function(){                      $("img").eq(0).attr('src','https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg');
  });

});

RESULT - http://jsbin.com/zefuxanoco/edit?html,css,js,output 结果-http: //jsbin.com/zefuxanoco/edit? html, css,js,输出

Try somthing like this: 尝试这样的事情:

<img src="img/clear-map-bg.png" alt="" usemap="my-map" />
<map name="my-map">
    <area shape="rect" coords="24,39,108,187,25,186,24,39" alt="" class="img1" />
    <area shape="rect" coords="173,117,230,116,238,276,176,289" alt="" class="img2" />
    <area shape="rect" coords="226,109,287,104,291,256,231,270" alt="" class="img3" />
    <area shape="rect" coords="283,102,340,97,346,269,286,252" alt="" class="img4" />
</map>

<script>
    $( "map[value='my-map'] area" ).each(function(){
        $(this).bind('hover',function(){
            var url = 'xy.jpg';
            $("img[usemap='my-map']").attr('src', url);
        });
    });
</script>

or with IDs and data attributes: 或具有ID和数据属性:

<img id="myimage" src="img/clear-map-bg.png" alt="" usemap="my-map" />
<map id="mymap" name="my-map">
    <area shape="rect" coords="24,39,108,187,25,186,24,39" alt="" data-img="image_1.jpg" />
    <area shape="rect" coords="173,117,230,116,238,276,176,289" alt="" data-img="image_2.jpg" />
    <area shape="rect" coords="226,109,287,104,291,256,231,270" alt="" data-img="image_3.jpg" />
    <area shape="rect" coords="283,102,340,97,346,269,286,252" alt="" data-img="image_4.jpg" />
</map>

<script>
    $("#mymap area").each(function(){
        $(this).bind('hover',function(){
            var url = $(this).attr('data-img');
            $('#myimage').attr('src', url);
        });
    });
</script>

Use classes if you want to use more tham one instance. 如果要使用多个实例,请使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM