Is possible create map html area in percentage?

后端 未结 7 1465
孤街浪徒
孤街浪徒 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:11

    I know this is an old question but maybe someone needs this at some point as I did. I modified @David Thomas' answer a bit to be have this little piece of JS be able to handle future recalculations:

    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++) {
                // Save original coordinates for future use
                var originalCoords = areas[i].getAttribute('data-original-coords');
                if (originalCoords == undefined) {
                    originalCoords = areas[i].getAttribute('coords');
                    areas[i].setAttribute('data-original-coords', originalCoords);
                }
    
                newCoords = originalCoords.replace(/(\d+)/g,function(a){
                    return Math.round(a * multiplier);
                });
                areas[i].setAttribute('coords',newCoords);
            }
        }
    }
    
    function remapImage() {
        var imgElement = document.getElementsByTagName('img')[0];
        remap(imgElement);
    }
    
    // Add a window resize event listener
    var addEvent = function(object, type, callback) {
        if (object == null || typeof(object) == 'undefined') return;
        if (object.addEventListener) {
            object.addEventListener(type, callback, false);
        } else if (object.attachEvent) {
            object.attachEvent("on" + type, callback);
        } else {
            object["on"+type] = callback;
        }
    };
    
    addEvent(window, "resize", remapImage);
    

提交回复
热议问题