我只是想知道如何使用 jquery 放大图片,
类似这个网站的东西,
当您点击大图时,它会放大,您可以移动光标并在放大时查看图片的其他部分,
如果有人可以向我展示链接或将我引向正确的方向,我将不胜感激。
最佳答案
它们不会放大,真正发生的是,当您单击“缩放”文本时,div 内的图像会被该图像的较大版本替换。并将溢出设置为隐藏。
当您移动光标时,使用一些巧妙的 JavaScript 和 DOM 处理,图像会相应地相对移动,从而创建实际放大图片的效果。
我将尝试为您创建一个简单的示例。
编辑
抱歉花了一段时间,但这里是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var fullWidth = 864; // Width in pixels of full-sized image
var fullHeight = 648; // Height in pixels of full-sized image
var thumbnailWidth = 389; // Width in pixels of thumbnail image
var thumbnailHeight = 292; // Height in pixels of thumbnail image
// Set size of div
$('#picture').css({
'width': thumbnailWidth+'px',
'height': thumbnailHeight+'px'
});
// Hide the full-sized picture
$('#full').hide();
// Toggle pictures on click
$('#picture').click(function() {
$('#thumbnail').toggle();
$('#full').toggle();
});
// Do some calculations
$('#picture').mousemove(function(e) {
var mouseX = e.pageX - $(this).attr('offsetLeft');
var mouseY = e.pageY - $(this).attr('offsetTop');
var posX = (Math.round((mouseX/thumbnailWidth)*100)/100) * (fullWidth-thumbnailWidth);
var posY = (Math.round((mouseY/thumbnailHeight)*100)/100) * (fullHeight-thumbnailHeight);
$('#full').css({
'left': '-' + posX + 'px',
'top': '-' + posY + 'px'
});
});
});
</script>
<style type="text/css">
#picture {
overflow: hidden;
position: relative;
border: 1px solid #000000;
}
#thumbnail {
cursor: pointer;
}
#full {
position: relative;
cursor: crosshair;
}
</style>
</head>
<body>
<div id="picture">
<img alt="" id="thumbnail" src="http://img202.imageshack.us/img202/8403/93629325.jpg" />
<img alt="" id="full" src="http://img111.imageshack.us/img111/4734/tbmecsekyellowflower.jpg" />
</div>
</body>
</html>
您需要相应地调整缩略图和全尺寸图像的宽度和高度。但只需复制粘贴上面的内容即可查看 imageshack 上托管的图像示例。
关于jquery - 如何使用jquery放大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1581784/