在布局页面时,有时会遇到大图片将页面容器“撑破”的情况,尤其是加载外链图片(通常是通过采集的外站的图片)。那么本文将为您讲述使用jQuery如何按比例缩放大图片,让大图片自适应页面布局。
通常我们处理缩略图是使用后台代码(PHP、.net、Java等)根据大图片生成一定尺寸的缩略图,来供前台页面调用,当然也有使用前台javascript脚本将加载后的大图强行缩放,变成所谓的缩略图,这种方法不可取。但是,针对网站内容页,如本站文章详情页,如果需要加载一张很大的图片时,为了防止“撑破”布局,我们使用jQuery来等比例缩放图片。我们分两种情况来讲述:
1.已知图片尺寸
- <img src="a.jpg" width="800" height="300" alt="图片">
- </div>
<div id="demo1">
<img src="a.jpg" width="800" height="300" alt="图片">
</div>
当页面加载的图片中含有属性width和height值,则可以使用几句简单的jQuery代码实现等比例缩放。
- var w = $("#demo1").width();//容器宽度
- $("#demo1 img").each(function(){//如果有很多图片,我们可以使用each()遍历
- var img_w = $(this).width();//图片宽度
- var img_h = $(this).height();//图片高度
- if(img_w>w){//如果图片宽度超出容器宽度--要撑破了
- var height = (w*img_h)/img_w; //高度等比缩放
- $(this).css({"width":w,"height":height});//设置缩放后的宽度和高度
- }
- });
- });
$(function(){
var w = $("#demo1").width();//容器宽度
$("#demo1 img").each(function(){//如果有很多图片,我们可以使用each()遍历
var img_w = $(this).width();//图片宽度
var img_h = $(this).height();//图片高度
if(img_w>w){//如果图片宽度超出容器宽度--要撑破了
var height = (w*img_h)/img_w; //高度等比缩放
$(this).css({"width":w,"height":height});//设置缩放后的宽度和高度
}
});
});
2.未知图片尺寸
当页面加载的图片尺寸未知的情况下,上述代码则不能进行有效的缩放,这种情况多出现在采集的外部链接图片。
- <img src="a.jpg" alt="图片">
- </div>
<div id="demo2">
<img src="a.jpg" alt="图片">
</div>
所幸的是,有好心朋友已经写出来专门的插件来处理,而且跨浏览器,解决了前端朋友们的一大难题。
下面隆重介绍下autoIMG。
autoIMG可以快速对文章图片进行尺寸自适应,它利用浏览器获取图片文件头尺寸数据,无需等待图片加载完成。
autoIMG兼容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | ...
调用autoIMG插件方法相当简单:
- $("#demo2").autoIMG();
- });
————————————————————————————————————————————————————————————————————————
在做页面时,用户要求,不同的分辨率,弹出窗口的位置不同
我想是不是先获得屏幕宽度,然后付值给变量,再在onclick中设置参数
- alert(screen.width+"*"+screen.height)
- </script>
<script>
alert(screen.width+"*"+screen.height)
</script>
- function centerWindow(url,w,h){
- l=(screen.width-w)/2
- t=(screen.height-h)/2
- window.open(url,’’,’left=’+l+’,top=’+t+’,width=’+w+’,height=’+h)
- }
- </script>
- <input type=button onclick="centerWindow(’about:blank’,200,200)">
- ---------------------------------------------------------------
- <body>
- <SCRIPT LANGUAGE="JavaScript">
- var s ="网页可见区域宽:"+ document.body.clientWidth;
- s+="\r\n网页可见区域高:"+ document.body.clientHeight;
- s += "\r\n网页正文全文宽:"+ document.body.scrollWidth;
- s += "\r\n网页正文全文高:"+ document.body.scrollHeight;
- s += "\r\n网页正文部分上:"+ window.screenTop;
- s += "\r\n网页正文部分左:"+ window.screenLeft;
- s += "\r\n屏幕分辨率的高:"+ window.screen.height;
- s += "\r\n屏幕分辨率的宽:"+ window.screen.width;
- s +="\r\n屏幕可用工作区高度:"+ window.screen.availHeight;
- s +="\r\n屏幕可用工作区宽度:"+ window.screen.availWidth;
- alert(s);
- </SCRIPT>
- ---------------------------------------------------------------
- <SCRIPT LANGUAGE="JavaScript">
- <!-- Begin
- function redirectPage() {
- /*var url640x480 = "http://www.yourweb.com/640x480.html";**记得改相应的页面*/
- var url800x600 = "index1.asp";
- var url1024x768 = "index2.asp";
- /*if ((screen.width == 640) && (screen.height == 480))
- window.location.href= url640x480;*/
- if (screen.width <= 800 )
- window.location.href= url800x600;
- else if ((screen.width >= 1024) )
- window.location.href= url1024x768;
- }
- // End -->
- </script>
<script>
function centerWindow(url,w,h){
l=(screen.width-w)/2
t=(screen.height-h)/2
window.open(url,’’,’left=’+l+’,top=’+t+’,width=’+w+’,height=’+h)
}
</script>
<input type=button onclick="centerWindow(’about:blank’,200,200)">
---------------------------------------------------------------
<body>
<SCRIPT LANGUAGE="JavaScript">
var s ="网页可见区域宽:"+ document.body.clientWidth;
s+="\r\n网页可见区域高:"+ document.body.clientHeight;
s += "\r\n网页正文全文宽:"+ document.body.scrollWidth;
s += "\r\n网页正文全文高:"+ document.body.scrollHeight;
s += "\r\n网页正文部分上:"+ window.screenTop;
s += "\r\n网页正文部分左:"+ window.screenLeft;
s += "\r\n屏幕分辨率的高:"+ window.screen.height;
s += "\r\n屏幕分辨率的宽:"+ window.screen.width;
s +="\r\n屏幕可用工作区高度:"+ window.screen.availHeight;
s +="\r\n屏幕可用工作区宽度:"+ window.screen.availWidth;
alert(s);
</SCRIPT>
---------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function redirectPage() {
/*var url640x480 = "http://www.yourweb.com/640x480.html";**记得改相应的页面*/
var url800x600 = "index1.asp";
var url1024x768 = "index2.asp";
/*if ((screen.width == 640) && (screen.height == 480))
window.location.href= url640x480;*/
if (screen.width <= 800 )
window.location.href= url800x600;
else if ((screen.width >= 1024) )
window.location.href= url1024x768;
}
// End -->
</script>
这段代码是根据不同的屏幕显示不同的页面
下面是传递这个参数的
- document.write("<a href=’WebStat/index.asp’>");
- document.write("<img src=’WebStat/count.asp?Referer=<%=refer%>
- &Width="+escape(screen.width)+"&Height="+escape(screen.height)+
- "’ border=0 width=1 height=1>");
- document.write("</a>");
- </script>