Resize image map on window resize

前端 未结 7 2030
情话喂你
情话喂你 2020-12-20 18:59

I\'m trying to resize an image map on window resize event. The closest I\'ve gotten is to use a mouseclick event, but it needs to be window resize for what I\'m doing. I\'m

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 19:29

    Here is a solution that does not use jQuery.

    First, build a library function:

    var ImageMap = {
        resize: function(coords, mapWidth) {
            var areas = document.getElementsByTagName('area'),
                imageWidth = document.querySelector("#map").clientWidth,
                resize = imageWidth / mapWidth;
    
            for (var i=0; iMath.round(x*resize));
                areas[i].coords = temp.join(',');
            }
        },
        getCoords: function(){
            var areas = document.getElementsByTagName('area'),
                array = [];
            for (var i=0; i+x));
            }
            return array;
        }
    };
    

    Then, call the resize function when the page is initially loaded, and when it is resized:

    var coords = ImageMap.getCoords();
    window.onload = function () {
        ImageMap.resize(coords, 500);
    }
    window.onresize = function () {
        ImageMap.resize(coords, 500);
    }
    

    Replace 500 with whatever your default map size is

提交回复
热议问题