JS simple picture zooming in and out of two ways

  • 2020-03-27 00:00:28
  • OfStack

Take the upper left corner as the fixed point, zoom in and out, the position of the point is unchanged.

Method one:

The Html code

   <script type="text/javascript"> 
        //Compatible with Internet explorer and firefox & NBSP;   Zoom in, zoom in
        function ImageSuofang(args) { 
            var oImg = document.getElementById("oImg"); 
            if (args) { 
                oImgoImg.width = oImg.width * 1.1; 
                oImgoImg.height = oImg.height * 1.1; 
            } 
            else { 
                oImgoImg.width = oImg.width / 1.1; 
                oImgoImg.height = oImg.height / 1.1; 
            } 
        }     
     </script> 

<form id="form1"> 

     <div class="pancontainer" data-orient="center" style="width:320px; height:480px;margin: 5px 300px 0px 400px;border: 1px solid #000;"> 
<img id="oImg" src="/img/c.jpg" alt="pic"/> 
</div> 

             <input id="btn1" type="button" value=" amplification " onclick="ImageSuofang(true)" /> 
        <input id="btn2" type="button" value=" narrow " onclick="ImageSuofang(false)" /> 
         <!--            <input type="button" value="<-Rotate counterclockwise " name="RotateL" id="RotateL" onclick="rotateRight('oImg',90);">  --> 

             
</form> 

Method 2:

The CSS code is as follows:

Css code

#biankuang{height:480px;width:320px;margin: 30px auto;}//If you add a border, you can see that the fixed point is the upper left corner.

Below is the image zoom function JS code:

Js code

var zoomLevel = 0; 
var currentWidth = 0; 
var currentHeight = 0; 
var originalWidth = 0; 
var originalHeight = 0; 
function initial(){ 
    currentWidth = document.myImage.width; 
    currentHeight = document.myImage.height; 
    originalWidth = currentWidth; 
    originalHeight = currentHeight; 
    update(); 
} 
function zoomIn(){ 
    document.myImage.width = currentWidth*1.2; 
    document.myImage.height = currentHeight*1.2; 
    zoomLevel = zoomLevel + 1; 
    update(); 
} 
function zoomOut(){ 
    document.myImage.width = currentWidth/1.2; 
    document.myImage.height = currentHeight/1.2; 
    zoomLevel = zoomLevel - 1; 
    update(); 
} 
function resetImage(){ 
    document.myImage.width = originalWidth; 
    document.myImage.height = originalHeight; 
    zoomLevel = 0; 
    update(); 
} 
function update(){ 
    currentWidth = document.myImage.width; 
    currentHeight = document.myImage.height; 
    zoomsize.innerText = zoomLevel; 
    imgsize.innerText = currentWidth + "X" + currentHeight; 
} 

  The code in the body of HTML is as follows:

The Html code

<body onload="initial()"> 

<div id="biankuang" data-orient="center"> 
<img name="myImage" src="/img/c.jpg" alt="pic"/>     //Introduce local images
</div> 

<p> 
<input type="button" value=" Enlarge images " onclick="zoomIn()"> 
<input type="button" value=" Reduce image " onclick="zoomOut()"> 
<input type="button" value="重置图片" onclick="resetImage()"> 
<span id="zoomsize"></span> <span id="imgsize"></span></p> 
</body> 

Related articles: