Javascript 溢出隐藏自动滚动

Javascript 溢出隐藏自动滚动,javascript,jquery,html,css,Javascript,Jquery,Html,Css,有人知道如何在div中用overflow:hidden;设置自动滚动(带循环)吗 范例 <div class="container" style="width:500px; max-width:500px; height:100px; max-height:100px; background:#F00; overflow:hidden;"> <div class="element_01" style="width:500px; height:100px; float:l

有人知道如何在div中用overflow:hidden;设置自动滚动(带循环)吗

范例

<div class="container" style="width:500px; max-width:500px; height:100px; max-height:100px; background:#F00; overflow:hidden;">
    <div class="element_01" style="width:500px; height:100px; float:left;"></div>
    <div class="element_02" style="width:500px; height:100px; float:left;"></div>
</div>

最终效果如何


显示元素\u 01->等待5秒->平滑滚动到元素\u 02->等待5秒//并重复

您可以使用scrollTo jQuery插件:

并使用
setTimeout(function(){$.scrollTo('#element');},5000)重复一个函数

<div class="container" style="width:500px; max-width:500px; height:100px; max-height:100px; background:#F00; overflow:hidden;">
    <div class="element_01" style="width:500px; height:100px; float:left;">aaa</div>
    <div class="element_02" style="width:500px; height:100px; float:left;">bbb</div>
</div>

<script>
var container=document.getElementsByClassName('container')[0];
var start = 0;
var smoothVal = 20;
var waitVal = 5000;
function smooth(){
    var interval=setInterval(function(){

          start++;
          container.scrollTop = start;
        if(start>100) {
              clearInterval(interval);
            wait();
        } 
      },smoothVal)
}

function wait(){
    start = 0;
    container.scrollTop = start;
       setTimeout(function(){


              smooth();

            },waitVal)
}
smooth();

</script>

aaa
bbb
var container=document.getElementsByClassName('container')[0];
var start=0;
var=20;
var waitVal=5000;
函数平滑(){
var interval=setInterval(函数(){
启动++;
container.scrollTop=开始;
如果(开始>100){
间隔时间;
等待();
} 
},smoothVal)
}
函数wait(){
开始=0;
container.scrollTop=开始;
setTimeout(函数(){
光滑的();
},waitVal)
}
光滑的();

此示例使用定位而不是滚动。 使用溢出隐藏元素进行滚动是可行的,但可能有问题

$(文档).ready(函数(){
var numSlides=$('ul.scroller').children().length,
计数器=0;
windowHeight=$('.window').height();
setInterval(函数(){
计数器++;
如果(计数器==numSlides){
计数器=0;
$('.scroller').css('top','0');
}否则{
变量toMove=(计数器*窗口高度);
$('.scroller').css('top','-'+toMove.toString()+'px');
}
}, 2000)
});
html{font-family:Helvetica,Arial}
.窗户{
宽度:300px;
高度:200px;
溢出:隐藏;
位置:相对位置;
边框:2件纯色天蓝色;
}
卷轴{
宽度:100%;
位置:绝对位置;
排名:0;
左:0;
列表样式类型:无;
填充:0;
保证金:0;
-webkit过渡:顶级。5s轻松;
过渡:顶级。5s轻松;
}
李先生{
宽度:100%;
高度:200px;
框大小:边框框;
填充:80px0;
文本对齐:居中;
字号:28px;
}
卷轴李:第n个子(2n+2){background:#f5}

  • 第一项
  • 第二项
  • 第三项
  • 第四项

这就是我想要的!谢谢