首先把你的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)
}
}
js特效里图片不会滚动的原因是没有使用js里的定时器。根据查询相关资料信息,js特效里让图片无缝滚动就是要让图片集在一定时间里自动切换,需要js里的定时器来控制时间,没有使用定时器功能就无法实现图片滚动。js指JavaScript,是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。JS特效就是网页中实现的特殊效果或者特殊的功能的一种技术,是用网页脚本(javascript)来编写制作动态特殊效果。
比如图片切换,渐变等等,它为网页活跃了网页的气氛,有时候会起到一定的亲切力。
JavaScript 是根据 "ECMAScript"标准制定的网页脚本语言。这个标准由 ECMA 组织发展和维护。ECMA-262 是正式的 JavaScript 标准。
扩展资料:
能够具有交互性,能够包含更多活跃的元素,就有必要在网页中嵌入其它的技术。如:Javascript、VBScript、Document Object Model(DOM,文档对象模型)、Layers和 Cascading Style Sheets(CSS,层叠样式表)。
JavaScript 使网页增加互动性。JavaScript 使有规律地重复的HTML文段简化,减少下载时间。JavaScript 能及时响应用户的操作,对提交表单做即时的检查,无需浪费时间交由 CGI 验证。JavaScript 的特点是无穷无尽的,只要你有创意。