2:还有移动端的zepto库 也有手势插件,
3:另外还有个叫QuoJS的手势插件 这个插件不依赖任何的库,
4:早期的应该是用wml语言支持的WMLScript实现。
5:举例:使用iscroll.js实现
1)下载iScroll.js,百度搜索iScroll.js下载即可
2)引入iScroll.js,在要使用滑动效果的地方,引入iScroll.js文件
3)编写规范的html格式
只有如下格式才能实现滑动效果
<div id="wrapper">
<div class="scroll">
这个区域可以滑动
</div>
</div>
如下格式不能滑动
<div id="wrapper">
<div class="other">这个区域可以滑动</div>
<div class="scroll">
这个区域不可以滑动了
</div>
</div>
只有wrapper的第一个子元素才能实现滑动效果。
4)编写js调用代码
var Scroll = new iScroll('wrapper',{hScrollbar:false, vScrollbar:false})
第一参数必需是滑动元素的父元素的id。
主要参数一览:
hScroll: true, 左右滑动,默认为true
vScroll: true,上下滑动
hScrollbar: true, 是否显示y轴滚动条,默认为显示
vScrollbar: true,是否显示X轴滚动条,默认为显示
不是很简单一两句话能说清楚地,需要HTML, CSS和javascript的配合。我大概说下做法,你需要自己写写试试看首先把你的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)
}
}