首先把你的div设置style为overflow hidden
然后在div里面再添加一个div,假设叫做imgcontainer,在里面存放所有图片,图片以float: left方式显示,每个图片的宽度你是知道的,这样图片的个数*每个图片的宽度(必要的话再加上图片之间的间隔)设置为imgcontainer的总宽度。然后把div的宽度设置为显示3个图片的宽度。这样就可以保证只显示3个图片,多余的图片看不到。
然后在点击左右2个按钮的时候添加事件,每点击一次,让imgcontainer的marginLeft增加或减少一个图片的宽度(同样必要的时候加上图片之间的间隔距离),这样就使得图片列表在左右移动了。
下面是我的一个例子,跟你的需求可能不完全一样,大概做下参考:
HTML片段
<div id="pagecontent" class="pagecontent"><div id="nav_thumbs_prev" class="disabled" onclick="NavThumbsPrev()"></div>
<div id="photolist_container">
<ol id="photolist" class="photolist">
<li><figure onclick="ViewPhoto(this,'aaaaa')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">aaaaa</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'bbbbb')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">bbbbb</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'cccc')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">cccc</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'dddd')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">dddd</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'eeee')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">eeee</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'ffff')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">ffff</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'gggg')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">gggg</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'hhhh')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">hhhh</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'iiii')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">iiii</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'jjjj')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">jjjj</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'kkkk')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">kkkk</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'llll')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">llll</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'mmmm')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">mmmm</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'nnnn')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">nnnn</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'oooo')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">oooo</figcaption></figure></li>
<li><figure onclick="ViewPhoto(this,'pppp')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">pppp</figcaption></figure></li>
</ol>
</div>
<div id="nav_thumbs_next" onclick="NavThumbsNext()"></div>
</div>
CSS部分
.photolist_container {margin: 0
/*Firefox*/
width: -moz-calc(100% - 30px)
/*chrome safari*/
width: -webkit-calc(100% - 30px)
/*Standard */
width: calc(100% - 30px)
float: left
height: 140px
overflow: hidden
}
ol.photolist {
background-color: black
margin: 0
margin-left: 1px
padding: 0
list-style: none
height: auto
padding: 0
display: block
height: 125px
}
ol.photolist>li {
margin: 0
margin-left: -1px
padding: 4px
text-align:center
display:inline
float: left
border: 1px solid #FF6699
width: 100px
}
#photolist figure {
margin: 0
padding: 0
text-align:center
border: 1px solid #000000
cursor: pointer
}
.samplecontainer {
height: 100px
width: 100px
text-align: center
margin: 0
padding: 0
border: 0
display: table-cell
vertical-align: middle
}
.samplecontainer>img {
margin: 0
padding: 0
border: 0
vertical-align:middle
}
Javascript部分
function MovePhotoList(size) {var photolist = document.getElementById("photolist")
var marginLeft = 1
if (photolist.style.marginLeft != "")
marginLeft = parseInt(photolist.style.marginLeft)
marginLeft = marginLeft + size
if (marginLeft <(document.getElementById("photolist_container").offsetWidth - photolist.offsetWidth))
{
marginLeft = document.getElementById("photolist_container").offsetWidth - photolist.offsetWidth
document.getElementById("nav_thumbs_next").setAttribute("class", "disabled")
}
else
{
document.getElementById("nav_thumbs_next").setAttribute("class", "enable")
}
if (marginLeft >1)
{
marginLeft = 1
document.getElementById("nav_thumbs_prev").setAttribute("class", "disabled")
}
else
{
document.getElementById("nav_thumbs_prev").setAttribute("class", "enable")
}
photolist.style.marginLeft = marginLeft + "px"
}
function NavThumbsPrev() {
var nav_thumbs_prev = document.getElementById("nav_thumbs_prev")
if (nav_thumbs_prev.getAttribute("class") != "disabled") {
MovePhotoList(56)
}
}
function NavThumbsNext() {
var nav_thumbs_next = document.getElementById("nav_thumbs_next")
if (nav_thumbs_next.getAttribute("class") != "disabled") {
MovePhotoList(-56)
}
}