Zooming in & out an image by clicking zoom buttons (Javascript)

前端 未结 3 674
离开以前
离开以前 2021-02-04 17:30

I\'m trying to get an image zoomed in & out by two zoom buttons (+) & (-). The problem is that \"zooming in\" stops when the image is full screen size (width 100%). I ne

相关标签:
3条回答
  • 2021-02-04 17:49

    Remove max-width: 100%; from .main img css

    0 讨论(0)
  • 2021-02-04 18:10

    As the previous answers stated your code is absolutely fine with the exception of, you have limited the max-width of your image with max-width: 100%. If you remove that, it will give you the desired output.

    function zoomin() {
      var myImg = document.getElementById("map");
      var currWidth = myImg.clientWidth;
      if (currWidth == 2500) return false;
      else {
        myImg.style.width = (currWidth + 100) + "px";
      }
    }
    
    function zoomout() {
      var myImg = document.getElementById("map");
      var currWidth = myImg.clientWidth;
      if (currWidth == 100) return false;
      else {
        myImg.style.width = (currWidth - 100) + "px";
      }
    }
    *body {
      margin: 0;
    }
    
    #navbar {
      overflow: hidden;
      background-color: #099;
      position: fixed;
      top: 0;
      width: 100%;
      padding-top: 3px;
      padding-bottom: 3px;
      padding-left: 20px;
    }
    
    #navbar a {
      float: left;
      display: block;
      color: #666;
      text-align: center;
      padding-right: 20px;
      text-decoration: none;
      font-size: 17px;
    }
    
    #navbar a:hover {
      background-color: #ffffd;
      color: black;
    }
    
    #navbar a.active {
      background-color: #4CAF50;
      color: white;
    }
    
    .main {
      padding: 16px;
      margin-top: 30px;
      width: 100%;
      height: 100vh;
      overflow: auto;
      cursor: grab;
      cursor: -o-grab;
      cursor: -moz-grab;
      cursor: -webkit-grab;
    }
    
    .main img {
      height: auto;
      width: 100%;
    }
    
    .button {
      width: 300px;
      height: 60px;
    }
    <script type="text/javascript" src="https://cdn.rawgit.com/asvd/dragscroll/master/dragscroll.js"></script>
    
    <body>
    
      <div id="navbar">
        <button type="button" onclick="zoomin()"> Zoom In</button>
        <button type="button" onclick="zoomout()"> Zoom Out</button>
      </div>
    
      <div class="main dragscroll">
        <img id="map" src="http://www.worldatlas.com/webimage/countrys/europelargesm.jpg" />
      </div>

    Edits:

    • Since you wanted the initial width to be 100% (full size), instead of max-width, just set width to 100% like done in the last edit.
    • For implementing the drag feature to scroll the image, another CSS class has been added to the main container called dragscroll and it's js is imported as needed.
    0 讨论(0)
  • 2021-02-04 18:11

    Your zoom method is working good just remove the css max width, and change your js validation to allow the image, get bigger.

    function zoomin(){
            var myImg = document.getElementById("map");
            var currWidth = myImg.clientWidth;
            //if(currWidth == 2500) return false;
            // else{
            //    myImg.style.width = (currWidth + 100) + "px";
            //} 
            myImg.style.width = (currWidth + 100) + "px";
        }
        function zoomout(){
            var myImg = document.getElementById("map");
            var currWidth = myImg.clientWidth;
            if(currWidth == 100) return false;
    		 else{
                myImg.style.width = (currWidth - 100) + "px";
            }
        }
    *body {
      margin: 0;
    }
    #navbar {
    	overflow: hidden;
    	background-color: #099;
    	position: fixed;
    	top: 0;
    	width: 100%;
    	padding-top: 3px;
    	padding-bottom: 3px;
    	padding-left: 20px;
    }
    #navbar a {
      float: left;
      display: block;
      color: #666;
      text-align: center;
      padding-right: 20px;
      text-decoration: none;
      font-size: 17px;
    }
    #navbar a:hover {
      background-color: #ffffd;
      color: black;
    }
    #navbar a.active {
      background-color: #4CAF50;
      color: white;
    }
    .main {
      padding: 16px;
      margin-top: 30px;
    }
    .main img {
    	/* max-width: 100%; */
    	height: auto;
    }
    .button {
      width: 300px;
      height: 60px;
    }
    </head>
    <body>
    
    <div id="navbar">
    <button type="button" onclick="zoomin()"> Zoom In</button>
    <button type="button" onclick="zoomout()"> Zoom Out</button>
    </div>
    
    <div class="main">
    <img id="map" src="http://www.worldatlas.com/webimage/countrys/europelargesm.jpg" />
    </div>

    On the other hand I would recommend magic zoom as a zoom library

    0 讨论(0)
提交回复
热议问题