Is possible create map html area in percentage?

后端 未结 7 1448
孤街浪徒
孤街浪徒 2020-12-09 10:20

I need to create something like this:

http://www.mrporter.com/journal/journal_issue71/2#2

where every product in my big image is associated with a tooltip wh

7条回答
  •  执笔经年
    2020-12-09 11:06

    Because this can't be done with simple HTML/CSS manipulation, the only alternative is JavaScript to, effectively, recalculate the coordinates based on the resizing of the image. To this end I've put together a function (though there's two functions involved) that achieves this end:

    function findSizes(el, src) {
        if (!el || !src) {
            return false;
        }
        else {
            var wGCS = window.getComputedStyle,
                pI = parseInt,
                dimensions = {};
            dimensions.actualWidth = pI(wGCS(el, null).width.replace('px', ''), 10);
            var newImg = document.createElement('img');
            newImg.src = src;
            newImg.style.position = 'absolute';
            newImg.style.left = '-10000px';
            document.body.appendChild(newImg);
            dimensions.originalWidth = newImg.width;
            document.body.removeChild(newImg);
            return dimensions;
        }
    }
    
    function remap(imgElem) {
        if (!imgElem) {
            return false;
        }
        else {
            var mapName = imgElem
                .getAttribute('usemap')
                .substring(1),
                map = document.getElementsByName(mapName)[0],
                areas = map.getElementsByTagName('area'),
                imgSrc = imgElem.src,
                sizes = findSizes(imgElem, imgSrc),
                currentWidth = sizes.actualWidth,
                originalWidth = sizes.originalWidth,
                multiplier = currentWidth / originalWidth,
                newCoords;
    
            for (var i = 0, len = areas.length; i < len; i++) {
                newCoords = areas[i]
                    .getAttribute('coords')
                    .replace(/(\d+)/g,function(a){
                        return Math.round(a * multiplier);
                    });
                areas[i].setAttribute('coords',newCoords);
            }
        }
    }
    
    var imgElement = document.getElementsByTagName('img')[0];
    
    remap(imgElement);​
    

    JS Fiddle demo.

    Please note, though, that this requires a browser that implements window.getComputedStyle() (most current browsers, but only in IE from version 9, and above). Also, there are no sanity checks other than ensuring the required arguments are passed into the functions. These should, though, be a start if you want to experiment.

    References:

    • document.body.
    • document.createElement().
    • document.getElementsByName().
    • document.getElementsByTagName().
    • element.getAttribute().
    • element.setAttribute().
    • element.style.
    • Math.round().
    • node.appendChild().
    • node.removeChild().
    • parseInt().
    • string.replace().
    • string.substring().
    • window.getComputedStyle.

提交回复
热议问题