【问题标题】:How to do zoom on an image while a mouseover when HTML & CSS are in the same editor page当 HTML 和 CSS 在同一个编辑器页面中时,如何在鼠标悬停时放大图像
【发布时间】:2021-01-10 22:28:50
【问题描述】:

我正在开发一个在同一页面中结合 HTML 和 CSS 的 HTLM 编辑器。我希望用户在将鼠标悬停在首页图片上时进行缩放

<h4><img style="border: 1px solid black; align: right;" title="" src="sys_attachment.do?sys_id=00ee33cbdb1b9c507261e03cd396190b" 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>

我搜索了解决方案,但解决方案总是将 HTML 和 CSS 分开 如果有人有任何建议或解决方案,请帮助我!

对不起我的英语,我还在学习!

【问题讨论】:

  • 我建议阅读 CSS。单独文件中的任何内容都可以内联。您也可以将该文件的内容粘贴到标题中的
  • 顺便说一句,如果不使用 &lt;style&gt; 或单独的文件(或 JS),您将无法获得悬停效果
  • 您知道您可以将样式元素包含到您的 html 文件中,您将在其中设置所有 css。如果这是你想要的?
  • 可以先添加 CSS 吗?
  • @MaxiGui 这就是我想要的(我想!)但我不知道我必须把它放在哪里

标签: html css image zooming mouseover


【解决方案1】:

这里有一个使用 css :hover 的完整示例。不需要JS。

您需要将style 元素设置为head 元素中的body 元素

<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>

这是 JS Adapt 的解决方案,来自此处的答案: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>

style 直接在正文中的最后一个示例: 它被正常接受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>

【讨论】:

  • 我在编辑器上没有标题,你可以查看我在这里看到的所有内容:pastebin.com/FiScLYJa
  • header 元素和head 完全不同。另外,您提供的代码用于 sur 到 body 元素
  • 这就是我可以修改的所有内容是否仍然可以包含
  • @SRP 好的,在这种情况下,你可以检查我添加的第二个 sn-p 它是用 js 的,它都转到了 body 元素
  • 我从第一个

    到最后一个

猜你喜欢
  • 1970-01-01
  • 2015-01-28
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 2011-07-25
相关资源
最近更新 更多