js 实现图片缩放

js 实现图片缩放图标保持大小不变,位置相对于图片移动

发布时间:2018-08-13 19:01:18

html

<div name="images1" style="zoom: 1; transform: scale(1); width: 5146px; height: 4583px;" id="images1">
	<img id="floorPlanPicture" src="图片地址" alt="" />
	<div>
		<span class="siteicon" data-left="5278.89657634" data-top="3453.1816" style="left: 5278.89657634px;top: 3453.1816px;color:#ff0000;zoom:1;transform:scale(1);">
			<i class="fa fa-map-marker"> </i>//图标
			<i>1</i>//序号 
		</span>
		......*13
	</div>
</div>

js

var image1 = document.getElementBy('images1');//获取包含图片和图标的div
var scrollFunc = function(e) {
   e.stopPropagation();
   e = e || window.event;
   zoom = parseFloat(images1.style.zoom);
   zoomIcon = icon[0].style.zoom;
   //图标的放大缩小
   if (e.wheelDelta) { //IE/Opera/Chrome
       tZoom = zoom + (e.wheelDelta > 0 ? 0.05 : -0.05);
   } else if (e.detail) { //Firefox
       tZoom = zoom + (e.detail > 0 ? -0.05 : 0.05);
   }

   if (tZoom < 0.1) return true;
   sZoom = 1 / tZoom;//图标的放大缩小
   images1.style.zoom = tZoom;
   images1.style.transform = 'scale(' + tZoom + ')';
   for (var i = 0; i < icon.length; i++) {
       var style = icon[i].style;
       var left = icon[i].getAttribute('data-left') * tZoom;//改变图标的位置
       var top = icon[i].getAttribute('data-top') * tZoom;//改变图标的位置
       style.left = left + 'px';
       style.top = top + 'px';
       style.zoom = sZoom;
       style.transform = 'scale(' + sZoom + ')';
   }
}

images1.onmousewheel = scrollFunc;//绑定函数到鼠标滑动事件

原图

原图

缩小的图

缩小的图

放大的图

放大的图

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值