【发布时间】:2014-08-30 12:58:39
【问题描述】:
我在 jquery 中更改 src 属性时遇到问题。
这是一些拇指:
<a href='Imagini/someimage.jpg' class='popup' data-id='n' >
<img src='Imagini-min/someimage.jpg' />
</a>
这里,弹出图像的容器:
<div class="gallery" align="center">
<p id="close"><img src="Resurse/close.png" /></p>
<p id="prev"><img src="Resurse/left.png" /></p>
<img id="change" src="" data-id=""/>
<p id="next"><img src="Resurse/right.png" /></p>
</div>
还有jquery代码:
$(".popup").click(function(e){
e.preventDefault();
var img = $(this).attr("href");
var id = $(this).attr("data-id");
$(".gallery").show();
if(id != $(".gallery").find("#change").attr("data-id")){
$(".gallery").find("#change").attr("src", img).hide().fadeIn(100);
$(".gallery").find("#change").attr("data-id", id);
SetPosition();
}
});
function SetPosition(){
var width = $(".gallery").find("#change").width();
var height = $(".gallery").find("#change").height();
var button_h = $(".gallery #prev img").height();
$(".gallery #prev").css("top",(height/2 - button_h + 10 ));
$(".gallery #next").css("top",(height/2 - button_h + 8 ));
$(".gallery").css("width",(width+50));
$(".gallery").css("height",(height+10));
}
问题是当我打开浏览器并单击我得到的任何图像时: http://oi60.tinypic.com/2mq5gmg.jpg
如果按一次,就正常了: http://oi59.tinypic.com/qpqdk8.jpg
有人遇到过这样的问题吗?
【问题讨论】:
-
您的代码是否包含在文档就绪事件处理程序中?
-
“打开浏览器并单击任意图像”和“如果按一次,则正常”是什么意思???
-
使用您的代码创建一个 jsFiddle 并将链接发布到您的问题中。
-
看起来您正试图在浏览器完成下载之前读取图像的尺寸......
-
sitefortest.url.ph/articole.php?art=53 多试几次打开图片,你就明白是什么问题了。