js以鼠标位置为中心的滑轮放大功能

3 篇文章 0 订阅

代码来自网上,小小改动了下,可以用其他的东西替换页面中aaa
在这里插入图片描述

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>以鼠标位置为中心的滑轮放大功能demo</title>
		<style type="text/css">
			html,
			body {
				height: 100%;
				overflow: hidden;
			}
			
			body {
				margin: 0;
				padding: 0;
			}
			
			#oImg {
				position: absolute;
				left: 50px;
				top: 50px;
				z-index: 1;
				width: 600px;
				height: 600px;
			}
		</style>
		<script type="text/javascript">
			/*绑定事件*/
			function addEvent(obj, sType, fn) {
				if(obj.addEventListener) {
					obj.addEventListener(sType, fn, false);
				} else {
					obj.attachEvent('on' + sType, fn);
				}
			};

			function removeEvent(obj, sType, fn) {
				if(obj.removeEventListener) {
					obj.removeEventListener(sType, fn, false);
				} else {
					obj.detachEvent('on' + sType, fn);
				}
			};

			function prEvent(ev) {
				var oEvent = ev || window.event;
				if(oEvent.preventDefault) {
					oEvent.preventDefault();
				}
				return oEvent;
			}
			/*添加滑轮事件*/
			function addWheelEvent(obj, callback) {
				if(window.navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
					addEvent(obj, 'DOMMouseScroll', wheel);
				} else {
					addEvent(obj, 'mousewheel', wheel);
				}

				function wheel(ev) {
					console.log(ev)
					var oEvent = prEvent(ev),
						delta = oEvent.detail ? oEvent.detail > 0 : oEvent.wheelDelta < 0;
					callback && callback.call(oEvent, delta);
					return false;
				}
			};
			/*页面载入后*/
			window.onload = function() {
				var oImg = document.getElementById('oImg');
				/*拖拽功能*/
				(function() {
					addEvent(oImg, 'mousedown', function(ev) {
						var oEvent = prEvent(ev),
							oParent = oImg.parentNode,
							disX = oEvent.clientX - oImg.offsetLeft,
							disY = oEvent.clientY - oImg.offsetTop,
							startMove = function(ev) {
								if(oParent.setCapture) {
									oParent.setCapture();
								}
								var oEvent = ev || window.event,
									L = oEvent.clientX - disX,
									T = oEvent.clientY - disY;
								oImg.style.left = L + 'px';
								oImg.style.top = T + 'px';
								oParent.onselectstart = function() {
									return false;
								}
							},
							endMove = function(ev) {
								if(oParent.releaseCapture) {
									oParent.releaseCapture();
								}
								oParent.onselectstart = null;
								removeEvent(oParent, 'mousemove', startMove);
								removeEvent(oParent, 'mouseup', endMove);
							};
						addEvent(oParent, 'mousemove', startMove);
						addEvent(oParent, 'mouseup', endMove);
						return false;
					});
				})();
				/*以鼠标位置为中心的滑轮放大功能*/
				(function() {
					addWheelEvent(oImg, function(delta) {
						var ratioL = (this.clientX - oImg.offsetLeft) / oImg.offsetWidth,
							ratioT = (this.clientY - oImg.offsetTop) / oImg.offsetHeight,
							ratioDelta = !delta ? 1 + 0.1 : 1 - 0.1,
							w = parseInt(oImg.offsetWidth * ratioDelta),
							h = parseInt(oImg.offsetHeight * ratioDelta),
							l = Math.round(this.clientX - (w * ratioL)),
							t = Math.round(this.clientY - (h * ratioT));
						with(oImg.style) {
							width = w + 'px';
							height = h + 'px';
							left = l + 'px';
							top = t + 'px';
						}
					});
				})();
			};
		</script>
	</head>

	<body>
		<div id="oImg">
			<div class="aaa">
				这里可以用svg替换aaa,放大厂区图功能
			</div>
		</div>
	</body>
<style type="text/css">
	.aaa{width: 100%;height: 100%;background: greenyellow;}
</style>
</html>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值