在窗口调整大小时调整图像映射的大小

发布于 2024-08-13 04:18:42 字数 355 浏览 13 评论 0原文

我正在尝试在窗口调整大小事件中调整图像映射的大小。我得到的最接近的是使用鼠标单击事件,但它需要根据我正在做的事情调整窗口大小。我正在使用 Firefox 3.5.5

我正在使用jquery。这是我的示例 - 我想要在窗口调整大小时调整大小的区域按钮位于左上角(单击它可以调整地图和区域按钮的大小):

http://www.whitebrickstudios.com/foghornstour/imagemap3.html

任何帮助将不胜感激! 谢谢你, 富有的

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 using Firefox 3.5.5

I'm using jquery somewhat. Here's my example - the area button I want to resize on window resize is in the top left (click on it to resize map and area button):

http://www.whitebrickstudios.com/foghornstour/imagemap3.html

Any help would be appreciated!
Thank you,
Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

新人笑 2024-08-20 04:18:42

我编写了一些简单的函数来重建每个事件的所有地图点。试试这个

function mapRebuild(scaleValue) {
    var map = $("#imgmap"); // select your map or for all map you can choose $("map").each(function() { map = $(this);.....})
    map.find("area").each(function() { // select all areas
        var coords = $(this).attr("coords"); // extract coords
            coords = coords.split(","); // split to array
        var scaledCoords = "";
        for (var coord in coords) { // rebuild all coords with scaleValue
              scaledCoords += Math.floor(coords[coord] * scaleValue) + ",";
            }
        scaledCoords = scaledCoords.slice(0, -1); // last coma delete
        $(this).attr("coords", scaledCoords); // set new coords
        });
    }

scaleValue可以计算为oldWindowWidth/newWindowWidth。当然,您需要在窗口调整大小时保留 oldWindowWidth 的值。也许我的解决方案不及时,但我希望这对某人有用

I wrote some simple function to rebuild all map points on every event. Try this

function mapRebuild(scaleValue) {
    var map = $("#imgmap"); // select your map or for all map you can choose $("map").each(function() { map = $(this);.....})
    map.find("area").each(function() { // select all areas
        var coords = $(this).attr("coords"); // extract coords
            coords = coords.split(","); // split to array
        var scaledCoords = "";
        for (var coord in coords) { // rebuild all coords with scaleValue
              scaledCoords += Math.floor(coords[coord] * scaleValue) + ",";
            }
        scaledCoords = scaledCoords.slice(0, -1); // last coma delete
        $(this).attr("coords", scaledCoords); // set new coords
        });
    }

scaleValue can be calculated as oldWindowWidth/newWindowWidth. Of course you need to keep the value of oldWindowWidth on window resize. Maybe my solution not on time, but i hope this is useful to someone

尹雨沫 2024-08-20 04:18:42

我想你想要的是
http://home.comcast.net/~urbanjost/semaphore.html

其中我展示了如何在图像显示尺寸发生变化时使图像地图坐标发生变化的不同示例。

I think what you want is at
http://home.comcast.net/~urbanjost/semaphore.html

where I show different examples of how to make your image map coordinates change when your image display size changes.

心意如水 2024-08-20 04:18:42

这是一个旧线程,但对于任何寻找类似甚至相同问题解决方案的人来说,ImageMapster jQuery 插件似乎提供了最简单的解决方案。您可以使用其调整大小方法(如果需要,甚至可以动画调整大小!),如下所示来调整图像及其图像贴图的大小:

$('img').mapster( 'resize', newWidth, newHeight, resizeTime);

您可以在 ImageMapster 的演示 页面到 jsFiddle演示调整图像大小和大小它的地图响应浏览器窗口的变化。

This is an old thread but for anyone looking for a solution for a similar or even identical problem, the ImageMapster jQuery plugin appears to provide the easiest solution. You can use its resize method (which can even animate the resizing if desired!) as follows to resize an image along with its image map:

$('img').mapster( 'resize', newWidth, newHeight, resizeTime);

You can find a link on ImageMapster's demo page to a jsFiddle that demonstrates resizing an image & its map in response to changing the browser window.

倾城°AllureLove 2024-08-20 04:18:42

作为 Viktor 答案的修改版本,该版本可以处理多个调整大小。它存储初始值,以便与未来的任何调整大小进行比较。这也使用 waitForFinalEvent所以它不会在调整大小时一遍又一遍地运行。



    var mapImg = $('#mapImg');
    var naturalWidth = 1200; // set manually to avoid ie8 issues
    var baseAreas = new Array();
    var scaleValue = mapImg.width() / naturalWidth;

    $(window).resize( function() {
        waitForFinalEvent( function() {
            scaleValue = mapImg.width() / naturalWidth;
            mapRebuild( scaleValue );
        }, 500, 'resize-window');
    });

    function mapRebuild( scaleValue ) {
        var map = $("#imgMap");
        var mapareas = map.find( 'area' );
        if ( baseAreas.length == 0 ) {
            mapareas.each( function() {
                baseAreas.push( $(this).attr( 'coords' ) ); // set initial values
            });
        }
        mapareas.each( function( index ) {
            var coords = baseAreas[index]; // use the corresponding base coordinates        
            coords = coords.split( ',' );       
            var scaledCoords = '';
            for ( var coord in coords ) {
                scaledCoords += Math.floor( coords[coord] * scaleValue ) + ',';
            }
            scaledCoords = scaledCoords.slice( 0, -1 );
            $(this).attr( 'coords', scaledCoords );
        });
    }

    mapRebuild( scaleValue ); // initial scale

As a modified version of Viktor's answer, this version can handle multiple resizes. It stores initial values for comparison against any future resize. This also uses waitForFinalEvent so it doesn't run over and over on resize.



    var mapImg = $('#mapImg');
    var naturalWidth = 1200; // set manually to avoid ie8 issues
    var baseAreas = new Array();
    var scaleValue = mapImg.width() / naturalWidth;

    $(window).resize( function() {
        waitForFinalEvent( function() {
            scaleValue = mapImg.width() / naturalWidth;
            mapRebuild( scaleValue );
        }, 500, 'resize-window');
    });

    function mapRebuild( scaleValue ) {
        var map = $("#imgMap");
        var mapareas = map.find( 'area' );
        if ( baseAreas.length == 0 ) {
            mapareas.each( function() {
                baseAreas.push( $(this).attr( 'coords' ) ); // set initial values
            });
        }
        mapareas.each( function( index ) {
            var coords = baseAreas[index]; // use the corresponding base coordinates        
            coords = coords.split( ',' );       
            var scaledCoords = '';
            for ( var coord in coords ) {
                scaledCoords += Math.floor( coords[coord] * scaleValue ) + ',';
            }
            scaledCoords = scaledCoords.slice( 0, -1 );
            $(this).attr( 'coords', scaledCoords );
        });
    }

    mapRebuild( scaleValue ); // initial scale

浅听莫相离 2024-08-20 04:18:42

这是一个不使用 jQuery 的解决方案。

首先,构建一个库函数:

var ImageMap = {
    resize: function(coords, mapWidth) {
        var areas = document.getElementsByTagName('area'),
            imageWidth = document.querySelector("#map").clientWidth,
            resize = imageWidth / mapWidth;

        for (var i=0; i<coords.length; i++) {
            var temp = coords[i].map(x=>Math.round(x*resize));
            areas[i].coords = temp.join(',');
        }
    },
    getCoords: function(){
        var areas = document.getElementsByTagName('area'),
            array = [];
        for (var i=0; i<areas.length; i++) {
            array.push(areas[i].coords.split(',').map(x=>+x));
        }
        return array;
    }
};

然后,在页面初始加载时以及调整大小时调用调整大小函数:

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

将 500 替换为默认地图大小

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; i<coords.length; i++) {
            var temp = coords[i].map(x=>Math.round(x*resize));
            areas[i].coords = temp.join(',');
        }
    },
    getCoords: function(){
        var areas = document.getElementsByTagName('area'),
            array = [];
        for (var i=0; i<areas.length; i++) {
            array.push(areas[i].coords.split(',').map(x=>+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

骑趴 2024-08-20 04:18:42

加载或调整窗口大小时重新计算图像地图坐标的具体示例:

此图像为 1930 * 3360 :

  <div>
    <img id = "alhambra" src="filename.png" usemap="#image-map" width=100%>

    <map name="image-map">
        <area target="" alt="home" id = "home" title="home" href="" coords="1905,307,35,12" shape="rect">
        <area target="" alt="vaciado" id = "vaciado" title="vaciado" href="" coords="141,367,1783,631" shape="rect">
        <area target="" alt="tienda" id = "tienda" title="tienda" href="" coords="282,1408,278" shape="circle">
        <area target="" alt="stocks" id = "stocks" title="stocks" href="" coords="1300,2968,722,2699" shape="rect">
        <area target="" alt="whatsapp" id = "whatsapp" title="whatsapp" href="" coords="506,2980,1788,3193" shape="rect">
        <area target="" alt="direccion" id = "direccion" title="direccion" href="" coords="43,3215,1883,3324" shape="rect">
    </map>
  </div>

并在正文后添加脚本:

</body>
<script>
  let text_home_coord = document.getElementById('home').coords;
  let text_vaciado_coord = document.getElementById('vaciado').coords;
  let text_tienda_coord = document.getElementById('tienda').coords;
  let text_stocks_coord = document.getElementById('stocks').coords;
  let text_whatsapp_coord = document.getElementById('whatsapp').coords;
  let text_direccion_coord = document.getElementById('direccion').coords;
  
  function img_map_response(){

    // get width and height in pixel
  var width_100_in_px = document.getElementById('alhambra').offsetWidth;
  var height_100_in_px = document.getElementById('alhambra').offsetHeight;

  // recalculate coords of image map
  function get_coor_resp(nombre_coords){

    // real width and height of image map
    var img_real_width="1930";
    var img_real_height="3360";

    // convert string coords to array
    text_array = nombre_coords.split(',');

    // rect
    if (text_array.length == 4) {
      // convert strig to integer
      x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
      x2 = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y2 = parseInt(parseInt(text_array[3])*parseInt(height_100_in_px)/parseInt(img_real_height));
      // result converted in array of strings
      array_txt =[x1.toString(), y1.toString(), x2.toString(), y2.toString()]
      console.log("array_txt",array_txt)
      return array_txt.join(',')

    // circle
    } else {
      // convert strig to integer
      x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
      r = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
      // result converted in array of strings
      array_txt =[x1.toString(), y1.toString(), r.toString()]
      return array_txt.join(',')        
    }
  }
  
 // set coords by recalculate coords (converted in string)
  document.getElementById('home').coords=get_coor_resp(text_home_coord);
  document.getElementById('vaciado').coords=get_coor_resp(text_vaciado_coord);
  document.getElementById('tienda').coords=get_coor_resp(text_tienda_coord);
  document.getElementById('stocks').coords=get_coor_resp(text_stocks_coord);
  document.getElementById('whatsapp').coords=get_coor_resp(text_whatsapp_coord);
  document.getElementById('direccion').coords=get_coor_resp(text_direccion_coord);
}
// add events load and resize for recalculate coords
window.addEventListener('load', img_map_response);
window.addEventListener('resize', img_map_response);
</script>

</html>

a concret sample to recalculate coords of image map when window is loaded or resized:

this image is 1930 * 3360 :

  <div>
    <img id = "alhambra" src="filename.png" usemap="#image-map" width=100%>

    <map name="image-map">
        <area target="" alt="home" id = "home" title="home" href="" coords="1905,307,35,12" shape="rect">
        <area target="" alt="vaciado" id = "vaciado" title="vaciado" href="" coords="141,367,1783,631" shape="rect">
        <area target="" alt="tienda" id = "tienda" title="tienda" href="" coords="282,1408,278" shape="circle">
        <area target="" alt="stocks" id = "stocks" title="stocks" href="" coords="1300,2968,722,2699" shape="rect">
        <area target="" alt="whatsapp" id = "whatsapp" title="whatsapp" href="" coords="506,2980,1788,3193" shape="rect">
        <area target="" alt="direccion" id = "direccion" title="direccion" href="" coords="43,3215,1883,3324" shape="rect">
    </map>
  </div>

and add the script after body as:

</body>
<script>
  let text_home_coord = document.getElementById('home').coords;
  let text_vaciado_coord = document.getElementById('vaciado').coords;
  let text_tienda_coord = document.getElementById('tienda').coords;
  let text_stocks_coord = document.getElementById('stocks').coords;
  let text_whatsapp_coord = document.getElementById('whatsapp').coords;
  let text_direccion_coord = document.getElementById('direccion').coords;
  
  function img_map_response(){

    // get width and height in pixel
  var width_100_in_px = document.getElementById('alhambra').offsetWidth;
  var height_100_in_px = document.getElementById('alhambra').offsetHeight;

  // recalculate coords of image map
  function get_coor_resp(nombre_coords){

    // real width and height of image map
    var img_real_width="1930";
    var img_real_height="3360";

    // convert string coords to array
    text_array = nombre_coords.split(',');

    // rect
    if (text_array.length == 4) {
      // convert strig to integer
      x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
      x2 = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y2 = parseInt(parseInt(text_array[3])*parseInt(height_100_in_px)/parseInt(img_real_height));
      // result converted in array of strings
      array_txt =[x1.toString(), y1.toString(), x2.toString(), y2.toString()]
      console.log("array_txt",array_txt)
      return array_txt.join(',')

    // circle
    } else {
      // convert strig to integer
      x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
      y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
      r = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
      // result converted in array of strings
      array_txt =[x1.toString(), y1.toString(), r.toString()]
      return array_txt.join(',')        
    }
  }
  
 // set coords by recalculate coords (converted in string)
  document.getElementById('home').coords=get_coor_resp(text_home_coord);
  document.getElementById('vaciado').coords=get_coor_resp(text_vaciado_coord);
  document.getElementById('tienda').coords=get_coor_resp(text_tienda_coord);
  document.getElementById('stocks').coords=get_coor_resp(text_stocks_coord);
  document.getElementById('whatsapp').coords=get_coor_resp(text_whatsapp_coord);
  document.getElementById('direccion').coords=get_coor_resp(text_direccion_coord);
}
// add events load and resize for recalculate coords
window.addEventListener('load', img_map_response);
window.addEventListener('resize', img_map_response);
</script>

</html>
中性美 2024-08-20 04:18:42

要在调整窗口大小时调用函数,请尝试以下操作:

$(window).bind('resize', function() {
    // resize the button here
});

此外,第 37 行缺少美元符号:

scaleXY('theMap',(window).width());

它应该是:

scaleXY('theMap',$(window).width());

To call a function when the window is resized, try the following:

$(window).bind('resize', function() {
    // resize the button here
});

Also, line 37 is missing a dollar sign:

scaleXY('theMap',(window).width());

It should be:

scaleXY('theMap',$(window).width());
时光瘦了 2024-08-20 04:18:42

如果您只需要调整图像大小,请使用以下技术:
http://www.cssplay.co.uk/layouts/background.html

感谢 CSSPlay。

If you only need to resize the image, use this technique:
http://www.cssplay.co.uk/layouts/background.html

Thanks to CSSPlay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文