javascript - 使用最少的 JQuery 显示放大图像的最佳方式是什么?

标签 javascript jquery

目标:

  • 鼠标悬停(或 :hover)时,将预览图像放大约 400% 并将其显示在页面中央
  • 移除鼠标离开时的预览

问题:

  • 像 FancyBox 这样的解决方案过于臃肿
    • 在 FancyBox 的例子中,它忽略了图像元素的宽度和高度,这使得它毫无用处
  • 这些“灯箱”中的大多数在调用时会抢走焦点

真的,我只是在寻找一个简单、高效的解决方案。

最佳答案

尝试这样的事情。诀窍是 position 你可以把 div 放在任何你想要的地方。 读点东西here .你可以阅读悬停 here

这是一个 html 示例。 (将其复制到文本文件并使用浏览器打开)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>Test</title>
      <script text="text/javascript" 
          src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js">
      </script>
  </head>
  <body>
    <ul>
      <li class="">Lynx</li>
      <li>Jaguar</li>
    </ul>
    <div id="picture" style="position:absolute; top:0px; right:0px;">
    </div>
  <script type="text/javascript">
    var animal = {
        "Lynx": 
          "http://wnbaoutsiders.files.wordpress.com/2009/06/lynx21.jpg",
        "Jaguar" : 
          "http://www.tropical-rainforest-animals.com/image-files/jaguar.jpg"
      }
    var hovered = function(e) {
      //you can get what to show from the elemnt, instead of the content
      // you could use an id.
      var name = $(e.target).html()
      $('#picture').append("<img src='" + animal[name] +"'/>")
    }
    var unhovered = function() {
      $('#picture').empty();
    }
    //here you bind mouseenter and mouseleave
    $('li').hover(hovered, unhovered);
  </script>
  </body>

关于javascript - 使用最少的 JQuery 显示放大图像的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2798600/

相关文章:

jquery - 有没有办法将所有值发布到多选列表中,而不仅仅是选定的值?

javascript - 创建 key :value pairs for an object while looping and setting variable for each

javascript - 收到的 promise 待定

javascript - 如何在点击次数后隐藏元素?

java - GWT自动刷新代码跳转至首页

javascript - 更改隐藏/显示方法

javascript - javascript 上的 setTimeout() 用于基于表单提交的自定义 URL

javascript - 带有链接 .png 的 SVG 不呈现

javascript - 在 Twitter Bootstrap 模态中输入按键时触发 onClick 事件

jquery - 使用 jCarousel for jQuery 隐藏 onLoad 内容