Native js realizes the effect of picture zoom in and zoom out timer

  • 2021-07-13 03:49:43
  • OfStack

Key points of knowledge

var fn=setInterval(function(){},1000)

Execute the function once every 1 second

clearInterval(fn)

Clear the timer

It is judged that when the picture is enlarged or reduced to a fixed size, the timer is cleared

Complete code


<!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>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
body,button,input,select,textarea{font:12px/1.5 tahoma,arial,\5b8b\4f53;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
small{font-size:12px;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:underline;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
</style> 
</head> 
<body>
<div style="width:400px;margin:0 auto;">
 <img src="http://img.mukewang.com/53577ee900016c2102080260.jpg" id="myImage" /><br>
 <input type="button" id="max" value=" Zoom in " />
 <input type="button" id="min" value=" Shrink " />
</div>
<script type="text/javascript">
function pic_max(){
 var maxBtn=document.getElementById("max");
 var minBtn=document.getElementById("min"); 
 maxBtn.onclick=function(){
 max();
 }
 var img=document.getElementById("myImage");
 var maxHeight=img.height*2;
 var maxWidth=img.width*2;
 function max(){
 var endHeight=img.height*1.3;
 var endWidth=img.width*1.3;
 var maxTime=setInterval(function(){
 if(img.height<endHeight&&img.width<endWidth){
  if(img.height<maxHeight&&img.width<maxWidth){
  img.height=img.height*1.05;
  img.width=img.width*1.05;
  }else{
  alert(" The picture is already the maximum value ")
  clearInterval(maxTime);
  }
 }else{
  clearInterval(maxTime);
 }
 },20);
 }
 minBtn.onclick=function(){
 min();
 }
 var img=document.getElementById("myImage");
 var minHeight=img.height*0.5;
 var minWidth=img.width*0.5;
 function min(){
 var overHeight=img.height*0.7;
 var overWidth=img.width*0.7;
 var minTime=setInterval(function(){
 if(img.height>overHeight&&img.width>overWidth){
  if(img.height>minHeight&&img.width>minWidth){
  img.height=img.height*0.95;
  img.width=img.width*0.95;
  }else{
  alert(" The picture is already the minimum ")
  clearInterval(minTime);
  }
 }else{
  clearInterval(minTime);
 }

 },20);
 }
}
window.onload=function(){
 pic_max();
}
</script> 
</body> 
</html> 


Related articles: