【问题标题】:How to show live preview in a small popup of linked page on mouse over on link?鼠标悬停在链接上时,如何在链接页面的小弹出窗口中显示实时预览?
【发布时间】:2011-01-08 04:35:42
【问题描述】:

鼠标悬停在链接上时,如何在链接页面的小弹出窗口中显示实时预览?

喜欢这个

http://cssglobe.com/lab/tooltip/03/

但实时预览

【问题讨论】:

  • 实时预览是指视频吗?您可以将任何内容放入这样的内联弹出窗口中(对象、HTML5 视频标签、img 等)。您可能有一些服务器端的东西,它通过 GET 获取链接并返回该链接的缩略图,然后在鼠标悬停时包含该图像。
  • @JitendraVyas 链接现在好像坏了。

标签: javascript jquery css xhtml


【解决方案1】:

HTML 结构

<div id="app">
<div class="box">
    <div class="title">How to preview link with iframe and javascript?</div>
    <div class="note"><small>Note: Click to every link on content below to preview</small></div>
    <div id="content">
        We'll first attach all the events to all the links for which we want to <a href="https://htmlcssdownload.com/">preview</a> with the addEventListener method. In this method we will create elements including the floating frame containing the preview pane, the preview pane off button, the iframe button to load the preview content.
    </div>
    <h3>Preview the link</h3>
    <div id="result"></div>
</div>

我们将首先使用 addEventListener 方法将所有事件附加到我们想要预览的所有链接。在此方法中,我们将创建元素,包括包含预览窗格的浮动框架、预览窗格关闭按钮、加载预览内容的 iframe 按钮。

<script type="text/javascript">
(()=>{
    let content = document.getElementById('content');
    let links = content.getElementsByTagName('a');
    for (let index = 0; index < links.length; index++) {
        const element = links[index];
        element.addEventListener('click',(e)=>{
            e.preventDefault();
            openDemoLink(e.target.href);
        })
    }

    function openDemoLink(link){

        let div = document.createElement('div');
        div.classList.add('preview_frame');
        
        let frame = document.createElement('iframe');
        frame.src = link;
        
        let close = document.createElement('a');
        close.classList.add('close-btn');
        close.innerHTML = "Click here to close the example";
        close.addEventListener('click', function(e){
            div.remove();
        })
        
        div.appendChild(frame);
        div.appendChild(close);
        
        document.getElementById('result').appendChild(div);
    }
})()

查看详情How to live preview link

【讨论】:

  • 您的整个答案似乎是从链接的网站逐字复制的。请在答案开头明确指出。事实上,如果你也用你自己的话添加一个解释就好了。
【解决方案2】:

您可以使用 iframe 在鼠标悬停时显示页面预览:

.box{
    display: none;
    width: 100%;
}

a:hover + .box,.box:hover{
    display: block;
    position: relative;
    z-index: 100;
}
This live preview for <a href="https://en.wikipedia.org/">Wikipedia</a>
  <div class="box">
    <iframe src="https://en.wikipedia.org/" width = "500px" height = "500px">
    </iframe>
  </div> 
remains open on mouseover.

这是一个包含多个实时预览的示例:

.box{
    display: none;
    width: 100%;
}

a:hover + .box,.box:hover{
    display: block;
    position: relative;
    z-index: 100;
}
Live previews for <a href="https://en.wikipedia.org/">Wikipedia</a>
  <div class="box">
     <iframe src="https://en.wikipedia.org/" width = "500px" height = "500px">
     </iframe>
  </div> 
and <a href="https://www.jquery.com/">JQuery</a>
  <div class="box">
     <iframe src="https://www.jquery.com/" width = "500px" height = "500px">
     </iframe>
  </div> 
will appear when these links are moused over.

【讨论】:

  • 很好的解决方案。我的一个问题是;如果您将鼠标移到正在显示的页面上,您是否可以使当您将鼠标悬停在链接上时出现的 iframe 保持可见?在this jsfiddle 中,我已经修改了您的示例,并使用了我想要的布局方式,但是如果页面出现时,您可以将鼠标移到它上面并且它保持可见(这样您就可以选择文本,例如例如,无需导航到页面)。提前致谢!
  • @Luke 我已经解决了这个问题:查看我的答案的更新版本。
  • IE 有什么修复方法吗,我使用的是 IE 11。其他浏览器都可以。在 IE 中,.box div 不会在悬停时保持显示
  • 这是一个非常聪明的解决方案,赞。
  • 请注意,可以将网络服务器配置为拒绝 iframe 中的预览。如果服务器发送响应头 X-Frame-Options DENY 浏览器将不会在 iframe 中显示其他网站。例如,google.com
【解决方案3】:

您可以使用下面的代码使用 javascript 显示链接的实时预览。

<embed src="https://www.w3schools.com/html/default.asp" width="60" height="40" />
<p id="p1"><a href="http://cnet.com">Cnet</a></p>
<p id="p2"><a href="http://codegena.com">Codegena</a></p>
<p id="p3"><a href="http://apple.com">Apple</a></p>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
  <link href="http://codegena.com/assets/css/image-preview-for-link.css" rel="stylesheet">     
  <script type="text/javascript">
    $(function() {
                $('#p1 a').miniPreview({ prefetch: 'pageload' });
                $('#p2 a').miniPreview({ prefetch: 'parenthover' });
                $('#p3 a').miniPreview({ prefetch: 'none' });
            });
  </script> <script src="http://codegena.com/assets/js/image-preview-for-link.js"></script>

通过Codegena了解更多信息

id="p1" - Fetch image preview on page load.
id="p2" - Fetch preview on hover.
id="p3" - Fetch preview image each time you hover.

【讨论】:

  • 我在一个网站上实现了这个脚本。很好,但我在导入 FB 页面时遇到问题
  • 6 年后工作得很好
【解决方案4】:

我个人会避免使用 iframe 并使用嵌入标签在鼠标悬停框中创建视图。

<embed src="http://www.btf-internet.com" width="600" height="400" />

【讨论】:

  • 最新的常青浏览器不再支持此功能。
【解决方案5】:

另一种方法是使用网站缩略图/链接预览服务LinkPeek(甚至现在恰好显示 StackOverflow 的屏幕截图作为演示)、URL2PNGBrowshotWebsnapr 或 @987654325 @。

【讨论】:

    【解决方案6】:

    我做了一个小插件来显示一个 iframe 窗口来预览链接。 仍处于测试版。 也许它适合你的情况:https://github.com/Fischer-L/previewbox

    【讨论】:

      【解决方案7】:

      您可以执行以下操作:

      1. 创建(或查找)将 URL 呈现为预览图像的服务
      2. 在鼠标悬停时加载该图像并显示它
      3. 如果你对直播很着迷,那么使用 jQuery 的 Timer 插件在一段时间后重新加载图像

      当然,这实际上不是现场直播。

      更明智的是,您可以为某些 URL 生成预览图像,例如每天或每周并使用它们。我想您不想手动执行此操作,也不想向您的服务用户显示与网站当前外观完全不同的预览。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2016-05-25
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多