Gar*_*ton 4 javascript jquery zoom
我是一个jQuery新手,所以答案可能很简单:
我有一个图像,我想用它做几件事.当用户点击"缩放"图标时,我正在运行"imagetool"插件(http://code.google.com/p/jquery-imagetool/)来加载更大版本的图片.该插件在图像周围创建一个新div,允许用户平移.
当用户点击替代图像时,我将删除旧图像并加载新图像.
问题出现在用户单击替代图像,然后单击缩放按钮时 - imagetool插件创建新div,但图像出现在...之后...
代码如下:
// Product Zoom (jQuery)
$(document).ready(function(){
$("#productZoom").click(function() {
// Set new image src
var imageSrc = $("#productZoom").attr("href");
$("#productImage").attr('src', imageSrc);
// Run the imagetool plugin on the image
$(function() {
$("#productImage").imagetool({
viewportWidth: 300,
viewportHeight: 300,
topX: 150,
topY: 150,
bottomX: 450,
bottomY: 450
});
});
return false;
});
});
// Alternative product photos (jQuery)
$(document).ready(function(){
$(".altPhoto").click(function() {
$('#productImageDiv div.viewport').remove();
$('#productImage').remove();
// Set new image src
var altImageSrc = $(this).attr("href");
$("#productZoom").attr('href', altImageSrc);
var img = new Image();
$(img).load(function () {
$(this).hide();
$('#productImageDiv').append(this);
$(this).fadeIn();
}).error(function () {
// notify the user that the image could not be loaded
}).attr({
src: altImageSrc,
id: "productImage"
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
在我看来,一旦将imagetool插件替换为新图像,它就再也看不到#productImage图像......所以我认为这与绑定有关?因为在页面加载后将新图像添加到dom中,iamgetool插件无法再正确使用它...这是正确的吗?如果是这样,任何想法如何处理它?
Wehey!我自己整理了一下......
事实证明,如果我完全删除包含div,然后用.html重写它,imagetool插件会再次识别它.
任何感兴趣的人修改后的代码:
$(document).ready(function(){
// Product Zoom (jQuery)
$("#productZoom").click(function() {
$('#productImage').remove();
$('#productImageDiv').html('<img src="" id="productImage">');
// Set new image src
var imageSrc = $("#productZoom").attr("href");
$("#productImage").attr('src', imageSrc);
// Run the imagetool plugin on the image
$(function() {
$("#productImage").imagetool({
viewportWidth: 300,
viewportHeight: 300,
topX: 150,
topY: 150,
bottomX: 450,
bottomY: 450
});
});
return false;
});
// Alternative product photos (jQuery)
$(".altPhoto").click(function() {
$('#productImageDiv div.viewport').remove();
$('#productImage').remove();
// Set new image src
var altImageSrc = $(this).attr("href");
// Set new image Zoom link (from the ID... is that messy?)
var altZoomLink = $(this).attr("id");
$("#productZoom").attr('href', altZoomLink);
var img = new Image();
$(img).load(function () {
$(this).hide();
$('#productImageDiv').append(this);
$(this).fadeIn();
}).error(function () {
// notify the user that the image could not be loaded
}).attr({
src: altImageSrc,
id: "productImage"
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9158 次 |
| 最近记录: |