fed icon indicating copy to clipboard operation
fed copied to clipboard

【讨论帖】关于上课讲到的滚轮事件

Open wabxq opened this issue 7 years ago • 5 comments

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			div{
				width: 300px;
				height: 300px;
				background-color: skyblue;
			    margin-top: -320px;
				margin-left: 1200px;
				position: fixed;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<div >
			<h1 id="four" >滑动滚轮</h1>
		</div>	
		<script>
			var ofour=document.querySelector("#four");
			ofour.addEventListener('mousewheel',function(e){
			      var zoom=parseInt(ofour.style.zoom, 10)||100;
                              zoom+=event.wheelDelta/12;
                              console.log(zoom);
                              if (zoom>0) ofour.style.zoom=zoom+'%';
                              return false;
			});
		</script>
	</body>
</html>

wabxq avatar Oct 24 '17 02:10 wabxq

看不懂楼上的

SDQ997 avatar Oct 24 '17 02:10 SDQ997

滚轮事件的代码,将原先的four的id移到h1上,因为滚轮事件是发生在h1上的,而且div没有这个属性

wabxq avatar Oct 24 '17 02:10 wabxq

//可以使用event.target
var ofour = document.querySelector("#four");
ofour.addEventListener('mousewheel', function(e) {
	var zoom = parseInt(event.target.style.zoom, 10) || 100;
	zoom += event.wheelDelta / 12;
	if(zoom > 0) event.target.style.zoom = zoom + '%';
	return false;
});

//也可以使用event.currentTarget
var ofour = document.querySelector("#four");
ofour.addEventListener('mousewheel', function(e) {
	var zoom = parseInt(event.currentTarget.style.zoom, 10) || 100;
	zoom += event.wheelDelta / 12;
	if(zoom > 0) event.currentTarget.style.zoom = zoom + '%';
	return false;
});

wabxq avatar Oct 24 '17 03:10 wabxq

其实,你可以把教程做成一个网页^_^,再往这里发链接

RubinTry avatar Oct 24 '17 04:10 RubinTry

@wabxq 分享知识点很好很棒!! 如果能够层次更分明写、条理更清晰些就更棒了!!

zptcsoft avatar Oct 24 '17 10:10 zptcsoft