目标:
- 鼠标悬停(或 :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/