使用 jQuery 更改图像映射坐标值
我有一个非常复杂的图像映射,我想将其缩小一半。为此,需要将所有坐标值除以 2。由于有数千个坐标值,我想我可以使用 jQuery 遍历 DOM 来查找坐标值,然后将它们除以 2。当涉及到 JavaScript 和 jQuery 时,我非常业余,我编写了以下代码,但无法完成我的任务:
$(function(){
$('area').each(function(){
coord_vals= $('area'[coords]).split(',');
new_vals= coord_vals/2;
$('area'[coords]).val(new_vals + ',');
});
});
这是我试图遍历的 HTML 的前几行:
<div id="map">
<img class="map" src="images/us_map.jpg" width="960" height="593" usemap="#usa">
<map name="usa">
<area href="#" title="SC" shape="poly" coords="735,418, 734,419, 731,418, 731,416, 729,413, 727,411, 725,410, 723,405, 720,399, 716,398, 714,396, 713,393, 711,391, 709,390, 707,387, 704,385, 699,383, 699,382, 697,379, 696,378, 693,373, 690,373, 686,371, 684,369, 684,368, 685,366, 687,365, 687,363, 693,360, 701,356, 708,355, 724,355, 727,356, 728,360, 732,359, 745,358, 747,358, 760,366, 769,374, 764,379, 762,385, 761,391, 759,392, 758,394, 756,395, 754,398, 751,401, 749,404, 748,405, 744,408, 741,409, 742,412, 737,417, 735,418"></area>
<area href="#" title="HI" shape="poly" coords="225,521, 227,518, 229,517, 229,518, 227,521, 225,521"></area>
<area href="#" title="HI" shape="poly" coords="235,518, 241,520, 243,520, 244,516, 244,513, 240,512, 236,514, 235,518"></area>
I have a pretty complex image map that I would like to shrink by half. To do so would require dividing all the coord values by 2. Since there are thousands of coord values, I was thinking that I could use jQuery to traverse the DOM to find the coord values, and divide them by two. I'm pretty amateur when it comes to JavaScript and jQuery and I wrote the following code that doesn't work to accomplish my task:
$(function(){
$('area').each(function(){
coord_vals= $('area'[coords]).split(',');
new_vals= coord_vals/2;
$('area'[coords]).val(new_vals + ',');
});
});
Here's the first few lines of HTML I'm trying to traverse:
<div id="map">
<img class="map" src="images/us_map.jpg" width="960" height="593" usemap="#usa">
<map name="usa">
<area href="#" title="SC" shape="poly" coords="735,418, 734,419, 731,418, 731,416, 729,413, 727,411, 725,410, 723,405, 720,399, 716,398, 714,396, 713,393, 711,391, 709,390, 707,387, 704,385, 699,383, 699,382, 697,379, 696,378, 693,373, 690,373, 686,371, 684,369, 684,368, 685,366, 687,365, 687,363, 693,360, 701,356, 708,355, 724,355, 727,356, 728,360, 732,359, 745,358, 747,358, 760,366, 769,374, 764,379, 762,385, 761,391, 759,392, 758,394, 756,395, 754,398, 751,401, 749,404, 748,405, 744,408, 741,409, 742,412, 737,417, 735,418"></area>
<area href="#" title="HI" shape="poly" coords="225,521, 227,518, 229,517, 229,518, 227,521, 225,521"></area>
<area href="#" title="HI" shape="poly" coords="235,518, 241,520, 243,520, 244,516, 244,513, 240,512, 236,514, 235,518"></area>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
这会维护格式并仔细转换每个有效的,这是输出:
你可以给在这里尝试一下。
You can do it like this:
This maintains the formats and converts each valid carefully, here's the output:
You can give it a try here.
我知道您说过您只是将图像地图缩小了一半,但我想我应该给您一些代码来使您的地图具有响应能力。当使用地图的图像重新调整大小时,坐标也会相应变化。
它所做的第一件事是存储图像映射的原始坐标,然后每当页面调整大小时运行函数mapResize。
在这里您会看到我们抓取了使用 usemap 名称的图像。如果您还想使用该网站上的任何其他地图,请放在这里。
然后代码将图像的当前宽度除以其原始宽度。我们稍后使用该数字乘以坐标来给出坐标的新值。
是的,这可能不是您确切问题的答案,但我认为它回答了您的问题并且更进一步。
我也知道这是一个 2 年前的问题,但我正在寻找自己问题的答案,这是最有帮助的线程。只是想添加我的调整=)
I know you said you're just shrinking your image map by half, but I thought I'd give you a bit of code that would make your map responsive. As the image that is using the map is re-sized, the coords change appropriately.
First thing it does is store the original coordinates of the image map, and then runs the function mapResize whenever the page re-sizes.
Here you'll see that we grab the image that is using the usemap name. This is here in case you have any other maps on the site that you'd like to use as well.
Then the code divides the images current width by its original width. We later use that number multiplied by the coordinates to give the new value of the coords.
So yeah, that might not be the answer to your exact question, but I think it answers your question and goes a bit further.
I also know this is a 2 year old question, but I was searching for an answer to a question of my own and this was the most helpful thread. Just wanted to add in my adjustment =)
我认为最好编写一些代码来为您生成新代码。这样做将减少初始化时间并使您的用户更满意。我整理了一个演示,基本上它用转换后的代码填充了一个文本区域,您只需用它替换原始代码即可。
HTML
脚本
I think it might be best to write some code to produce a new code for you. Doing this will reduce the initialization time and make your users happier. I threw together a demo, basically it fills a textarea with the transformed code with which you just replace the original.
HTML
Script
您走在正确的轨道上,只是您没有在循环中使用 $(this) 。另外,要获取坐标,您可以使用 attr('coords')
固定版本
You were on the right track it's just you didn't use $(this) in your loop. Also, to get the coords, you can use attr('coords')
Fixed version