jquery 图像映射调整大小

标签 jquery html

我编写此函数是为了重新调整元素 onLoad 的位置以及用户是否应该调整其浏览器窗口的大小。它在加载时正常工作,但在调整窗口大小时无法正确重新计算。我做错了什么?

var orig_width = jQuery("#imageMaps").attr("width");    

        function sizing() { 

              var pic = jQuery('#imageMaps');

              var curr_width =  pic.width();      

              if (orig_width > curr_width) {    
                var width_ratio = orig_width / curr_width;
              }

              else if (orig_width < curr_width) {
                  var width_ratio = curr_width / orig_width;
              }

              var width_ratio_dec = parseFloat(width_ratio).toFixed(2);        alert(width_ratio_dec);

            jQuery("area").each(function() {
            var pairs = jQuery(this).attr("coords").split(', ');
            for(var i=0; i<pairs.length; i++) {
                var nums = pairs[i].split(','); 
                for(var j=0; j<nums.length; j++) { 
                    if (orig_width > curr_width) {
                        nums[j] = nums[j] / width_ratio_dec; 
                    }
                    else if (orig_width < curr_width) {
                        nums[j] = nums[j] * width_ratio_dec; 
                    }
                    else if (orig_width == curr_width) {
                        nums[j]
                    }

                }
                       pairs[i] = nums.join(',');
            }
            jQuery(this).attr("coords", pairs.join(', '));
            }); 
        }

        jQuery(document).ready(sizing);
        jQuery(window).resize(sizing);

这是 html:

    <img class="alignnone size-full wp-image-430" id="imageMaps" src="SItePlan.gif" width="1500" height="1230" alt="Toledo Beach Marina Map" usemap="#flushometer" border="0" />

<map name="flushometer" id="flushometer"> 

<area shape="circle" coords="515,313,11" href="#" alt="" />
<area shape="circle" coords="910,115,11" href="#" alt="" />
<area shape="circle" coords="948,130,11" href="#" alt="" />
<area shape="circle" coords="1013,126,11" href="#" alt="" />
<area shape="circle" coords="928,375,11" href="#" alt="" />
<area shape="circle" coords="1252,339,11" href="#" alt="" />
<area shape="circle" coords="434,638,11" href="#" alt="" />
<area shape="circle" coords="761,684,11" href="#" alt="" />
<area shape="circle" coords="462,744,11" href="#" alt="" />
<area shape="circle" coords="361,790,11" href="#" alt="" />
<area shape="circle" coords="341,802,11" href="#" alt="" />
<area shape="circle" coords="310,827,11" href="#" alt="" />
<area shape="circle" coords="721,774,11" href="#" alt="" />
<area shape="circle" coords="835,799,11" href="#" alt="" />
<area shape="circle" coords="784,813,11" href="#" alt="" />
<area shape="circle" coords="793,865,11" href="#" alt="" />
<area shape="circle" coords="880,864,11" href="#" alt="" />
<area shape="circle" coords="1033,872,11" href="#" alt="" />
<area shape="circle" coords="444,367,11" href="#" alt="" />

</map>

最佳答案

我在这里假设您在调整窗口大小时同时调整图像大小。如果不是,那么这是毫无意义的代码,但这是您发布的内容的整理和(我相信)工作版本......

var orig_width = 0;

function sizing() { 
    if (orig_width == 0) return;

    var pic = $('#imageMaps');
    var curr_width =  pic.width();      
    var ratio = curr_width / orig_width;

    $("area").each(function() {

        var pairs = $(this).attr("coords").split(', ');

        for(var i=0; i<pairs.length; i++) {

            var nums = pairs[i].split(','); 

            for(var j=0; j<nums.length; j++) { 
                nums[j] = nums[j] * ratio; 
            }

            pairs[i] = nums.join(',');
        }
        $(this).attr("coords", pairs.join(","));
    }); 
    orig_width = curr_width;
}

$("#imageMaps").one("load", function() {
    orig_width = $("#imageMaps").attr("width");    
    $(window).resize(sizing);
}).each(function() {
    if (this.complete) $(this).load();
});

请注意,我只使用 ratio,因为您不需要知道图像是比原来大还是小 - 它只是不同或相同(ratio == 1)。

唯一值得注意的是,它在运行后设置 orig_width = curr_width,以便在下次发生调整大小事件时根据图像的当前大小计算出比例,而不是原始大小。

关于jquery 图像映射调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117219/

相关文章:

javascript - AngularJS 中 header 和 body 部分具有不同依赖关系的单独模块

javascript - 如何调节 LI 的高度?

jquery - 如何在一个页面布局中添加和删除自动导航的事件类?

jquery - 如何为文本区域标签添加背景文本?

javascript - 在 jQuery 中使用 $() 快捷方式是不好的做法吗?

iphone html css 教程

javascript - 基于 Asp.net 中单选按钮列表的 Jquery 显示/隐藏

javascript - 如何使 div 出现在用户屏幕的右下角?

jquery - 隐藏页面部分然后在单击链接时显示它 - JQuery

javascript - 带有可滚动图像的 Div