发布于 2017-04-27 12:27:49 | 124 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Javascript教程,程序狗速度看过来!

JavaScript客户端脚本语言

Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。


非常不错的应用代码,方便我们处理一些图片效果

<html> 
<head> 
<title>Untitled</title> 
<style type="text/css"> 
.testCss{width:200px;height:300px;border:1px red solid;text-align:center;display:block;} 
.testCss1{width:300px;height:300px;border:1px red solid;text-align:center;display:block;} 
.testCss2{width:400px;height:300px;border:1px red solid;text-align:center;display:block;} 
</style> 
<script type="text/javascript"> 
function autoSizeImg(Contents,offsetWidth,offsetHeight,vlmiddle){ 
var o=Contents.getElementsByTagName("IMG"); 
var cwidth= window.getComputedStyle?window.getComputedStyle(Contents,null).width:Contents.currentStyle["width"]; 
var cheight=window.getComputedStyle?window.getComputedStyle(Contents,null).height:Contents.currentStyle["height"]; 
var ncwidth=parseInt(cwidth); 
var ncheight=parseInt(cheight); 
for(var i=0;i<o.length;i++){ 
var img=o[i]; 
var iw=img.width; 
var ih=img.height; 
if(img.width>ncwidth){ 
var nw=ncwidth-offsetWidth; 
img.width=nw 
img.height=(nw*ih)/iw; 
}else if(img.width<ncwidth&&img.height>ncheight){ 
var nh=ncheight-offsetHeight; 
img.height=nh; 
img.width=(nh*iw)/ih; 
} 
if(img.height>ncheight&&img.width<ncwidth){ 
var hh=ncheight-offsetHeight; 
img.height=nh; 
img.width=(nh*iw)/ih; 
} 
if(vlmiddle)img.style.marginTop=((ncheight-img.height)/2)+"px"; 
} 
} 
</script> 
</head> 

<body> 
<div class="testCss"> 
<img src="http://files.phperz.com/file_images/sucai/111131524.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/> 
</div> 
<div class="testCss1"> 
<img src="http://files.phperz.com/upload/20081010215957304.jpg" onload="autoSizeImg(this.parentNode,5,5,true)"/> 
</div> 
<div> </div> 
<div class="testCss2"> 
<img src="http://files.phperz.com/upload/20081010215958874.gif" onload="autoSizeImg(this.parentNode,5,5,true)"/> 
</div> 
</body> 
</html>

注释:
1、后面的testCss1和testCss2两个样式跟testCss是一样的。只是设置了大小。
2、红色部分的height和width必须定义,是设置存放img外面的父层容器的高和宽。
3、text-algin:为图片水平居中
4、display:block,必须设置。否则FF下启用自动缩放不起作用。

使用方法:
1、是需要在img的父层定义样式,包含上述注释里面提到的东西;
2、是img加上 


onload="autoSizeImg(this.parentNode,5,5,true)"

参数说明:
参数Contents一般采用this.parentNode,即使img的父容易
参数offsetWidth,offsetHeight分别是宽和高度居中的修正值。
参数vlmiddle设置为true则是垂直居中

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务