Is possible create map html area in percentage?

后端 未结 7 1442
孤街浪徒
孤街浪徒 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 10:57

    There is a jQuery plugin for this jQuery RWD Image Maps.

    You might want to integrate my pending pull request (manually) to support "width=100%": https://github.com/stowball/jQuery-rwdImageMaps/pull/10

    0 讨论(0)
  • 2020-12-09 11:04

    Percentages in image maps are not an option. You might want to get some scripting involved (JS) that recalculates the exact position on images resize. Of course, in that script you can work with percentages if you want.

    0 讨论(0)
  • 2020-12-09 11:05

    Consider using the Raphaël JavaScript Library with some CSS. See http://raphaeljs.com/ and Drawing over an image using Raphael.js.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-09 11:22

    Alternative solution using links:

    CSS:

    .image{
      position: relative;
    }
    .image a{
      display: block;      
      position: absolute;
    }
    

    HTML:

    <div class="image">
      <img src="image.jpg" alt="image" />
      <a href="http://www.example.cz/1.html" style="top: 10%; left: 10%; width: 15%; height: 15%;"></a>
      <a href="http://www.example.cz/2.html" style="top: 20%; left: 50%; width: 15%; height: 15%;"></a>
      <a href="http://www.example.cz/3.html" style="top: 50%; left: 80%; width: 15%; height: 15%;"></a>
    </div>
    

    Percentage dimensions can be detected in graphic editors

    0 讨论(0)
提交回复
热议问题