Hover map area sprite with jquery

﹥>﹥吖頭↗ 提交于 2019-12-02 03:35:12

I am also working on similar project. Here is my code with explanation.

HTML

<img src="images/car.jpg" alt="car" usemap="#carmap" width="1100px" height="700px" /> 
<map name="carmap">
    <area shape="rect" coords="0,0,200,150" alt="door" href="#" class="door1">
    <area shape="rect" coords="453,404,533,704" alt="door" href="#" class="door2">
</map>

<div class="door1"></div>

<div class="door2"></div>

CSS

img{
    position: relative;
}

div.door1{
    width: 200px;
    height: 150px;
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    background: blue;
    pointer-events:none;  
}

div.door2{
    width: 180px;
    height: 300px;
    position: absolute;
    left: 453px;
    top: 404px;                
    background: url("images/tire-modified.jpg") no-repeat left top;
    pointer-events:none;  
    border-radius: 50%;
}

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
    $(document).ready(function(){

        $("div.door1").hide();
        $("area.door1").mouseenter(function(){
            $("div.door1").show();
        });

        $("area.door1").mouseleave(function(){
            $("div.door1").hide();
        });

        $("area.door2").mouseenter(function(){
            $("div.door2").show();
        });

        $("area.door2").mouseleave(function(){
            $("div.door2").hide();
        });

    });
</script>

So, first I set the image with position relative and map with two areas on image. Now, on that map hover part I bind mouseenter and mouseleave events so that I can set my other divs(according to areas) I can set them with position absolute. Don't forget to add pointer-events:none; to your absolute elements so that none of the events applicable to those elements. Otherwise the blinking problem will be there. I hope it will be helpful to you.

Regular car-image. Regular car-image

On-hover of the car tier(car tier image with absolute position). On-hover of the car tier(car tier image with absolute position

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