CSS 图片偏移

html-css09

CSS 图片偏移,第1张

首先你要知道要截取的图片在整张图片中的偏移量以及大小。然后:

<div style="width:wwwpxheight:hhhpxbackground:url(图片url) no-repeat -lllpx -tttpx"></div>

其中www是要截取的小图片的宽度,hhh则是高度;

lll是小图片的左边在大图片的偏移量,ttt则是上边的偏移量。

请注意负号不能省略!

给body设置:margin:0padding:0就行了。

body{margin:0padding:0}

在对css不是很熟悉的情况下,不建议*{margin:0padding:0}因为这样会把所有元素的内外边距都为0,,,

我这写了个简单的,你要根据你的需要改动。

我实现的是,有5张图,每次只显示一张图片,点按钮切换。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">

<style type="text/css">

body{ margin:0font-family : "Lucida Grande", Verdana, Lucida, Arial, Helvetica, 宋体,sans-serif}

ul{margin:0padding:0list-style:none}

#imgList{ width:100pxheight:100px/* 定义显示窗口的大小 */

border:1px solid #000overflow:hidden}

#imgList ul{ width:500px} /* 定义全部图片的宽度和,这里有5张,每张宽100px,总宽度就是500px */

#imgList li{ width:100pxheight:100pxfloat:left} /* 单张图片的所在位置的大小,宽度就是图片宽度加左右边距 */

#imgList img{ width:90pxheight:90pxmargin:5px} /* 单张图片的尺寸,及外边距 */

</style>

<script>

function move(to){

var imgList = document.getElementById("imgList")

if(to == "left") imgList.scrollLeft += 100//li的宽度

else imgList.scrollLeft -= 100//li的宽度

}

</script>

</head>

<body>

<!-- 请使用以下html结构 -->

<div id="imgList">

<ul>

<li><img src="Photoshop CS4.png">

<li><img src="Photoshop CS4.png">

<li><img src="Photoshop CS4.png">

<li><img src="Photoshop CS4.png">

<li><img src="Photoshop CS4.png">

</ul>

</div>

<!-- 这个可以自己定义,把事件copy过去就行了 -->

<input type="button" value="left" onClick="move('left')">

<input type="button" value="right" onClick="move('right')">

</body>

</html>