美文网首页
html 悬浮框demo, 触碰边缘图片会缩小, 文本会自动换行

html 悬浮框demo, 触碰边缘图片会缩小, 文本会自动换行

作者: littlesunn | 来源:发表于2025-08-07 17:24 被阅读0次
悬浮框.gif
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Image Preview on Hover</title>
  <style>
    .attachment-link {
      margin: 20px;
      float: right;
    }

    .file-link {
      color: #409EFF;
      text-decoration: none;
      cursor: pointer;
      font-size: 13px;
    }

    .content-preview {
      position: fixed;
      pointer-events: none;
      background: white;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      padding: 5px;
      max-width: 300px;
      z-index: 9999;
    }

    .preview-text {
      white-space: pre-wrap;
      word-wrap: break-word;
      padding: 5px;
    }
  </style>
</head>

<body>
  <div class="attachment-link">
    <a class="file-link" href="http://localhost:5173/cat.jpg"
      onmouseenter="handleMouseEnter(this, 'http://localhost:5173/cat.jpg', event)" onmouseleave="handleMouseLeave()"
      onmousemove="updatePreviewPosition(event)">
      📎 http://localhost:5173/cat.jpg
    </a>

    <a class="file-link" href="http://localhost:5173/test.txt"
      onmouseenter="handleMouseEnter(this, 'http://localhost:5173/test.txt', event)" onmouseleave="handleMouseLeave()"
      onmousemove="updatePreviewPosition(event)">
      📎 http://localhost:5173/test.txt
    </a>
  </div>

  <div id="imagePreview" class="content-preview">
    <img id="previewImage" class="preview-image" src="" alt="Preview" style="display: none;" width="100%" height="100%">
    <pre id="previewText" class="preview-text" style="display: none;"></pre>
  </div>

  <script>
    const imagePreview = document.getElementById('imagePreview');
    const previewImage = document.getElementById('previewImage');
    const previewText = document.getElementById('previewText');
    let isPreviewVisible = false;

    function isImageFile(url) {
      if (!url) return false;
      const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg'];
      return imageExtensions.some(ext => url.toLowerCase().endsWith(ext));
    }

    function isTextFile(url) {
      if (!url) return false;
      const textExtensions = ['.txt', '.log', '.csv', '.json', '.xml', '.html', '.js', '.css'];
      return textExtensions.some(ext => url.toLowerCase().endsWith(ext));
    }

    function handleMouseEnter(element, url, event) {
      if (isImageFile(url)) {
        previewImage.src = url;
        previewImage.style.display = 'block';
        previewText.style.display = 'none';
        imagePreview.style.display = 'block';
        isPreviewVisible = true;
        updatePreviewPosition(event);
      } else if (isTextFile(url)) {
        fetchTextContent(url).then(content => {
          previewText.textContent = content;
          previewImage.style.display = 'none';
          previewText.style.display = 'block';
          imagePreview.style.display = 'block';
          isPreviewVisible = true;
          updatePreviewPosition(event);
        });
      }
    }

    function fetchTextContent(url) {
      return Promise.resolve("This is a sample text file content.\n\nIt can contain multiple lines.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam auctor, nisl eget ultricies tincidunt, nisl nisl aliquam nisl, eget ultricies nisl nisl eget nisl.");
    }

    function handleMouseLeave() {
      imagePreview.style.display = 'none';
      isPreviewVisible = false;
    }

    function updatePreviewPosition(event) {
      if (isPreviewVisible) {
        const previewWidth = 300;
        const previewHeight = 300;
        const offset = 15;

        let x = event.clientX + offset;
        let y = event.clientY + offset;

        if (y + previewHeight > window.innerHeight) {
          y = window.innerHeight - previewHeight - 5;
        }

        // if (x + previewWidth > window.innerWidth) {
        //     x = window.innerWidth - previewWidth - 5;
        // }

        imagePreview.style.left = x + 'px';
        imagePreview.style.top = y + 'px';
      }
    }
  </script>
</body>

</html>

相关文章

  • HTML系列知识(二)

    HTML段落 标签即表示段落,是paragraph的缩写,自带换行效果 在p标签中的文本会自动换行,不在p标签中的...

  • html

    :p标签的文本会在一个段落中自动换行。div标签内的文本也会在一个段落中自动换行。 :换行标签,使自动换行。文本格...

  • 使用safari对webview进行调试

    使用safari对webview进行调试 WebView加载HTML图片大小自适应与文章自动换行

  • wkwebview加载HTML标签适配

    适配标椎: 可自动换行,自动匹配屏幕的宽度, 图片自适应适配代码如下 其实就是在html的头部加个适配head

  • iOS14 画中画

    画中画本质是添加了一个视频悬浮框,demo地址:点我:demo链接[https://github.com/howh...

  • Android 界面 是可以设置隐藏其他应用的悬浮框(第三方应用

    最近在做Q的升级项目,写了一个悬浮框的demo,在设置界面 悬浮框竟然消失了,离开设置界面又出现了。我觉得很奇怪,...

  • css中3种定位机制

    标准文档流;浮动;绝对定位;块级元素:从左到右撑满一行,触碰到页面边缘自动换行;行内元素:在同一行显示,不会改变h...

  • iOS自动换行的输入框

    由于在项目中使用自动换行输入框的场景比较多,所以就自己封了个自动换行的输入框。具体步骤,且由我一一道来。 在苹果本...

  • iOS图片剪裁

    项目中需要对图片进行裁剪操作,所以封装了一个简单的剪裁图片的控件,当时的设想是能够根据剪裁框的区域自动放大或缩小,...

  • 布局方法

    完成自动换行的方块列表布局,如下图1所示: 可以自动换行,并且响应式的布局。 HTML: Hello Use...

网友评论

      本文标题:html 悬浮框demo, 触碰边缘图片会缩小, 文本会自动换行

      本文链接:https://www.haomeiwen.com/subject/lezsojtx.html