Adding hover effects for links in image map (area)

前端 未结 2 1700
太阳男子
太阳男子 2021-01-03 07:50

I\'m only a beginner in html and I would like to put hover effects for my image map. I have searched for lots of solutions but I only come up with solutions using script whi

2条回答
  •  执笔经年
    2021-01-03 08:12

    The following JS and CSS makes image(map)s responsive and adds a hover effect to the areas.

    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 += "";
        });
    });
    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);}
    
    
    
        Zirconia Abutment
        Gold Abutment
        CCM Abutment
        EZ Post Abutment
        Milling Abutment
        Angled Abutment
        Temporary Abutment [Metal]
        Fuse Abutment
        Lab Analog
        Transfer Impression Coping Driver
        Impression Coping [Transfer]
        Impression Coping [Pick-Up]
    

提交回复
热议问题