请教一个JS的问题。 我的这个网站用到了jquery的延时加载和等比例缩放图片,但是第一次打开页面的时候,等比例缩放好象不行,刷新下才可以执行,不知道是哪里出了问题,求高手指教。网址的链接:http://www.uotoc.com/product/jindan/ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片加载时需要时间的,如果已经打开过会被浏览器缓存jQuery.fn.ImageAutoSize = function(width,height) { $("img",this).each(function() { var image = $(this); if(image.width()>image.height())。这里第一次由于图片未加载完毕,所以width是不能获取到的。可以监听img的load事件,再进行缩放操作。 jQuery.fn.ImageAutoSize = function(width,height) { $("img",this).each(function() { var image = $(this); if(image.width()>image.height()) { if(image.width()>width) { image.width(width); image.height(width/image.width()*image.height()); } if(image.height()>height) { image.height(height); image.width(height/image.height()*image.width()); } } else { if(image.height()>height) { image.height(height); image.width(height/image.height()*image.width()); } if(image.width()>width) { image.width(width); image.height(width/image.width()*image.height()); } } }); } $(document).ready(function(){ //$("#contant2").ImageAutoSize(166,166); //$(".list_x").ImageAutoSize(166,166); $("img").lazyload({ //placeholder : "jd_pic/grey.gif", effect:"leiresizeimg" }); });function leiresizeimg(){ $("#contant2").ImageAutoSize(166,166); $(".list_x").ImageAutoSize(166,166);}现在图片有些加载不了。 <html><head> <title></title> <script type="text/javascript" src="jquery-1.4.js"></script> <script type="text/javascript"> initialMediaShow = function (w, h, currentImage) { var imgSize = getImageListThumbSizeAfterLoading(w, h, currentImage); currentImage.css({ width: imgSize.width + 'px', height: imgSize.height + 'px', top: (imgSize.displayH - imgSize.height) / 2 + 'px', left: (imgSize.displayW - imgSize.width) / 2 + 'px' }); }; getImageListThumbSizeAfterLoading = function (w, h, currentImage) { var size = { width: 0, height: 0, displayW: 0, displayH: 0, flag: 'none' }; var imageObj = new Image(); imageObj.src = currentImage.attr('src'); if (imageObj.width / imageObj.height < w / h) { size.basis = 'height'; size.height = h; size.width = (h / imageObj.height) * imageObj.width; size.displayW = w; size.displayH = h; } else { size.basis = 'width'; size.width = w; size.height = (w / imageObj.width) * imageObj.height; size.displayW = w; size.displayH = h; } return size; }; </script></head><body> <img class="list-img list-img-over" src="a.gif" onload="var currentImage = $(this);initialMediaShow(150,160,currentImage); this.nextSibling.style.display=\'none\';this.style.display =\'\';" /> <div style="position: relative;"> <img style="position: relative; left: 59px; top: 64px" src="loading.gif"> </div> '</body></html>这里可能会由些语法问题因为这是以前写的东西了提几点,1initialMediaShow 这个函数的 前2个参数代表你需要放置图片的容器大小,也就是我们要对图片按这个比例放大缩小。 这个东西的效果就是当图片未加载完成的时候先显示loading这个图片,然后当图片加载结束后loading这个图片标签被隐藏了。 如果是动态添加图片的话,那么请按照这个样子拼接好HTML就可以了 能不能把一个网面中两个不同ID的内容放到一个JS文件中进行输出? 隐藏与显示的问题,在线等 求身份证正则 JavaScript中的字符串操作 关于动态添加删除事件的问题~~~~急~~~~ 如何让每一个被选择的单选按钮保持被选中的状态? vml绘制的统计图如何转换成其他格式(如jpg、gif等) 求救:如何在表格中鼠标放上时更改表格的背景图?在线等! 求阿信的XP MENU源代码! 同名的复选框的判断,该怎么做呢? 请大家帮忙写个正则 求一个渐出的效果,可以使用jquery
{
$("img",this).each(function()
{
var image = $(this);
if(image.width()>image.height())
。这里第一次由于图片未加载完毕,所以width是不能获取到的。
可以监听img的load事件,再进行缩放操作。
{
$("img",this).each(function()
{
var image = $(this);
if(image.width()>image.height())
{
if(image.width()>width)
{
image.width(width);
image.height(width/image.width()*image.height());
}
if(image.height()>height)
{
image.height(height);
image.width(height/image.height()*image.width());
}
}
else
{
if(image.height()>height)
{
image.height(height);
image.width(height/image.height()*image.width());
}
if(image.width()>width)
{
image.width(width);
image.height(width/image.width()*image.height());
}
}
});
}
$(document).ready(function(){
//$("#contant2").ImageAutoSize(166,166);
//$(".list_x").ImageAutoSize(166,166);
$("img").lazyload({
//placeholder : "jd_pic/grey.gif",
effect:"leiresizeimg"
});
});
function leiresizeimg()
{
$("#contant2").ImageAutoSize(166,166);
$(".list_x").ImageAutoSize(166,166);
}现在图片有些加载不了。
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript"> initialMediaShow = function (w, h, currentImage) {
var imgSize = getImageListThumbSizeAfterLoading(w, h, currentImage);
currentImage.css({ width: imgSize.width + 'px', height: imgSize.height + 'px', top: (imgSize.displayH - imgSize.height) / 2 + 'px', left: (imgSize.displayW - imgSize.width) / 2 + 'px' }); };
getImageListThumbSizeAfterLoading = function (w, h, currentImage) { var size = {
width: 0,
height: 0,
displayW: 0,
displayH: 0,
flag: 'none'
};
var imageObj = new Image();
imageObj.src = currentImage.attr('src');
if (imageObj.width / imageObj.height < w / h) {
size.basis = 'height';
size.height = h;
size.width = (h / imageObj.height) * imageObj.width;
size.displayW = w;
size.displayH = h; }
else {
size.basis = 'width';
size.width = w;
size.height = (w / imageObj.width) * imageObj.height;
size.displayW = w;
size.displayH = h; } return size; };
</script>
</head>
<body>
<img class="list-img list-img-over" src="a.gif" onload="var currentImage = $(this);initialMediaShow(150,160,currentImage); this.nextSibling.style.display=\'none\';this.style.display =\'\';" />
<div style="position: relative;">
<img style="position: relative; left: 59px; top: 64px" src="loading.gif">
</div>
'
</body>
</html>
这里可能会由些语法问题因为这是以前写的东西了
提几点,1initialMediaShow 这个函数的 前2个参数代表你需要放置图片的容器大小,也就是我们要对图片按这个比例放大缩小。
如果是动态添加图片的话,那么请按照这个样子拼接好HTML就可以了