日期:2014-05-16  浏览次数:20444 次

js控制点击让div滚动,显示一个,隐藏一个
以下为js代码:
<script type="text/javascript">
     function getDown(){
    var gundong = document.getElementsByClassName("gundong");
var L = gundong.length;
var show = 1;
for(var i=0;i<L;i++){
if(gundong[i].style.display=='block'){
    show=i;
break;
}
}
if(show+4<L){
   gundong[show].style.display='none';
   gundong[show+4].style.display='block';
}

}

function getUp(){
    var gundong = document.getElementsByClassName("gundong");
var L = gundong.length;
var show = 1;
for(var i=0;i<L;i++){
if(gundong[i].style.display=='block'){
    show=i;
break;
}
}
if(show-1>=0&&show+4<=L){
   gundong[show-1].style.display='block';
   gundong[show+3].style.display='none';
}

}
  </script>

以下为html代码:
<div id="main">
    <div class="gundong" id="liudong" style="display:block">1</div>
<div class="gundong" id="liudong" style="display:block">2</div>
<div class="gundong" id="liudong" style="display:block">3</div>
<div class="gundong" id="liudong" style="display:block">4</div>
<div class="gundong" id="liudong" style="display:none">5</div>
<div class="gundong" id="liudong" style="display:none">6</div>
<div class="gundong" id="liudong" style="display:none">7</div>
</div>
<input type='button' onclick="getUp()" value="上一个"> <input type='button' onclick="getDown()" value="下一个">
以下效果图: