How to do zoom on an image while a mouseover when HTML & CSS are in the same editor page

后端 未结 1 1401
清酒与你
清酒与你 2021-01-22 10:40

I am working on a HTLM editor who combine HTML & CSS in the same page. I want that the users have a zoom when mouseover on a picture in front page



        
相关标签:
1条回答
  • 2021-01-22 11:39

    Here a full example working with css :hover. No need JS.

    You need to set the style element into your head element out of body element

    <html>
      <head>
        <style>
        .zoom {
          transition: transform .2s; /* Animation */
        }
    
        .zoom:hover {
          transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large, it will go outside of the viewport) */
          position:absolute;
          right:0;
        }
        </style>
      </head>
      <body>
        <p>&nbsp;</p>
        <hr />
        <h4 class="zoom"><img style="border: 1px solid black; align: right;" title="" src="https://cdn.pixabay.com/photo/2020/09/09/13/03/bike-riding-5557589_1280.png" alt="" width="204" align="right" border="1" hspace="" vspace="" /></h4>
        <h4><strong>4. Entrez votre mot de passe.</strong></h4>
        <ul style="list-style-type: disc; list-style-position: inside;">
        <li>Puis, cliquez sur "Suivant".</li>
        </ul>
        <p>&nbsp;</p>
      </body>
    </html>

    Here is a solution with JS Adapt from answer here : Javascript: Zoom in on mouseover WITHOUT Jquery or plugins

    <html>
      <head>
        <style>
        </style>
      </head>
      
      <body>
        <p>&nbsp;</p>
          <hr />
          <h4>
            <img id="imgZoom" style="border: 1px solid black; align: right;" width="200px" height="200px" align="right" onmousemove="zoomIn(event)" onmouseout="zoomOut()" src="https://cdn.pixabay.com/photo/2020/09/09/13/03/bike-riding-5557589_1280.png">
            <div style="border: 1px solid black;
                width: 200px;
                height: 200px;
                display: none;
                background-image: url('https://cdn.pixabay.com/photo/2020/09/09/13/03/bike-riding-5557589_1280.png');
                background-repeat: no-repeat;"
                id="overlay"
                onmousemove="zoomIn(event)"></div>
            </h4>
          <h4><strong>4. Entrez votre mot de passe.</strong></h4>
          <ul style="list-style-type: disc; list-style-position: inside;">
          <li>Puis, cliquez sur "Suivant".</li>
          </ul>
        <p>&nbsp;</p>
        
        
        <script>
          function zoomIn(event) {
            var element = document.getElementById("overlay");
            element.style.display = "inline-block";
            var img = document.getElementById("imgZoom");
            var posX = event.offsetX ? (event.offsetX) : event.pageX - img.offsetLeft;
            var posY = event.offsetY ? (event.offsetY) : event.pageY - img.offsetTop;
            element.style.backgroundPosition = (-posX * 4) + "px " + (-posY * 4) + "px";
    
          }
    
          function zoomOut() {
            var element = document.getElementById("overlay");
            element.style.display = "none";
          }
        </script>
      </body>
    </html>

    Last example with style directly in body: It is accepted normaly based https://www.w3.org/Submission/1996/1/WD-jsss-960822

    <html>
    
      <body>
        <style>
          .zoom {
            transition: transform .2s; /* Animation */
          }
    
          .zoom:hover {
            transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large, it will go outside of the viewport) */
            position:absolute;
            right:0;
          }
          </style>
        <p>&nbsp;</p>
        <hr />
        <h4 class="zoom"><img style="border: 1px solid black; align: right;" title="" src="https://cdn.pixabay.com/photo/2020/09/09/13/03/bike-riding-5557589_1280.png" alt="" width="204" align="right" border="1" hspace="" vspace="" /></h4>
        <h4><strong>4. Entrez votre mot de passe.</strong></h4>
        <ul style="list-style-type: disc; list-style-position: inside;">
        <li>Puis, cliquez sur "Suivant".</li>
        </ul>
        <p>&nbsp;</p>
      </body>
    </html>

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