CSS关于图片缩放和截取问题

html-css09

CSS关于图片缩放和截取问题,第1张

JS代码(随便放哪里):

<script language="javascript">

<!--

var flag=false

function DrawImage(ImgD){

var image=new Image()

image.src=ImgD.src

if(image.width>0 &&image.height>0){

flag=true

if(image.width/image.height>= 180/110){

if(image.width>180){

ImgD.width=180

ImgD.height=(image.height*110)/image.width

}else{

ImgD.width=image.width

ImgD.height=image.height

}

/*ImgD.alt="bigpic" */

}

else{

if(image.height>110){

ImgD.height=110

ImgD.width=(image.width*110)/image.height

}else{

ImgD.width=image.width

ImgD.height=image.height

}

/*ImgD.alt="bigpic" */

}

}

}

//-->

</script>

图片使用的地方:

<img src="图片" border=0 width="180" height="110" onload="javascriptrawImage(this)">

width="180" height="110" 注意这里最好限定,如果不限定加载图时会成原大,然后再缩小,这个过程如果图大了很难看的.这里是宽度和高度,在前面的JS里改,这里也作相应的改.

图不会变形,只会按比列缩 alt 解决不了.

background:url(pic.jpg) no-repeat left top

后面两个参数就是移动图片位置的,第一个是水平位置,如left或者right或者center,第2个是垂直位置,如top或者bottom或者center,也可以是数字,如

background:url(pic.jpg) no-repeat -10px -20px

具体看你要截取的位置

background-position属性分为x轴和y轴,也就是横坐标和纵坐标两个值:background-positionx和background-positiony。

对于一整张大图只需要截取其中一部分,你需要知道两点:

1.需要的那一部分图片的在整个大图中的位置 2.它的大小

然后就好办了,我举个小例子:

一张300px*300px的大图,我需要的部分大小是宽20px高50px,在整个图片的位置离上边是20px;左边是80px,然后可以写样式了:

div{

background-image:url(bjimg.jpg)/*背景图片的路径*/

background-position:-80px -20px/*背景图片的位置是距左80px;距上20像素*/

width:20px/*div的宽度*/

height:50px/*div的高度*/

}

希望对你有所帮助。