图片热区自适应屏幕大小 通用代码

<img class="img-link" src="https://jdxl-img.jdxlt.com/uploads/40e44122152f4d89bf17308777d3c09d.gif" border="0" usemap="#map7591222054" alt="" />
<map name="map7591222054" id="map7591222054"><area shape="rect" coords="1,167,214,261" href="https://op.jiandanxinli.com/daily/820qx/index.htm " target="_blank" title="" alt=""></area><area shape="rect" coords="1,309,214,402" href="https://op.jiandanxinli.com/adv/lexiangqixi_cp1/index.htm?vision=2" target="_blank" title="" alt=""></area><area shape="rect" coords="1,453,215,544" href="https://op.jiandanxinli.com/daily/qixi_promotion_dazhong/index.htm" target="_blank" title="" alt=""></area><area shape="rect" coords="6,604,224,810" href="https://op.jiandanxinli.com/daily/0825zhishipai/index.htm" target="_blank" title="" alt=""></area><area shape="rect" coords="1,874,291,972" href="https://op.jiandanxinli.com/daily/0825caidan/index.htm" target="_blank" title="" alt=""></area><area shape="rect" coords="430,835,749,983" href="https://op.jiandanxinli.com/daily/0825liaoliao/index.htm" target="_blank" title="" alt=""></area></map>
.img-link{display:block;width: 100%;border: 0;}

热区按照多少比例写的。初始化为多少就好了·~ 其他不用改

var initwidth=null,//初始图片宽度
area=document.getElementsByTagName('area'),
initarea=null;//初始数据保存
console.log(area);
function init(){//初始化
        initwidth=750; // 热区按照多少比例写的。这里初始化为多少就好了·~
        initarea=new Array(area.length-1);
        for(var i=0;i<area.length;i++){
                initarea[i]=area[i].getAttribute("coords");
        }
}

function setCoords(){//根据分辨率自适应热区坐标
    var width=document.body.offsetWidth,
        percent=width/initwidth;
    for(var i=0;i<area.length;i++){
        var coords=initarea[i],
                arr=coords.split(",");
        for(var j=0;j<arr.length;j++){
            arr[j] *= percent;
        }
        area[i].setAttribute("coords",arr.join(","));
        console.log(area[i]);
    }
}
//使用
init();
setCoords();
window.onload = function () {
    setCoords();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左钦杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值