使用JS实现鼠标放上图片进行放大离开实现缩小功能

使用JS实现鼠标放上图片进行放大离开实现缩小功能,具体代码如下所示:

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<div id= 'div_jpg' style="width: 200px;height: 200px;">
		<img src="./128206.jpg" id="img" style="width: 100%;height: 100%;">
	</div>
	<script>
		var img = document.getElementById('img')
		var s1,s2
		console.log(img)
		// 图片放大效果
		i = 100;
		img.addEventListener('mouseover',function(){
			clearInterval(s1);
			s1 = setInterval(function(){
				i+=0.1;
				img.style.width = (i)+'%';
				img.style.height = (i)+'%';
				i = parseFloat(i);
				if(i>=120) clearInterval(s1);
			},1);
		})
		img.addEventListener('mouseout',function(){
			clearInterval(s2);
			s2 = setInterval(function(){
				i-=0.1;
				img.style.width = (i)+'%';
				img.style.height = (i)+'%';
				i = parseFloat(i);
				if(i<=100) clearInterval(s2); 
			})
		})

	</script>
</body>
</html>

分享源码,喜欢的朋友点击查看:

基于jQuery插件Pinchzoom.js实现手指触摸图片放大缩小特效源码

jquery鼠标悬停图片放大滑动显示标题特效