无缝滚动原生JS 版
* {
margin: 0;
padding: 0;
}
#box{
width: 300px;
height: 120px;
border: 1px solid red;
margin: 50px auto;
overflow: hidden;
}
.con {
height: 120px;
overflow: hidden; // 这句话至关重要--只有hidden后,才能滚动,子元素要高度 > 包裹元素
}
ul {
position: relative;
height: 240px;
list-style: none;
}
li {
height: 24px;
line-height: 24px;
width: 100%;
background-color: tomato;
}
li:nth-child(odd) {
background-color: #ccc;
}
<!-- 这个结构必须 是改变第二层 div的 scrollTop==卷去的是内容部分 -->
<div id="box">
<div id="container" class="con">
<ul id="wrap">
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
</div>
</div>
js 部分
<script>
var timer = null;
var speed = 60;
var wrap = document.getElementById("wrap");
var container = document.getElementById("container");
var box = document.getElementById("box");
var height = container.offsetHeight;
wrap.innerHTML += wrap.innerHTML;
function auto() {
if(container.scrollTop >= height){
container.scrollTop = 0;
}else{
container.scrollTop++;
}
}
timer = setInterval("auto()",speed);
box.onmouseover = function (){
clearInterval(timer);
};
box.onmouseout = function (){
timer = setInterval("auto()",speed);
};
</script>
无缝滚动 jquery 版
<script>
var timer = null;
var speed = 60;
var html = $("#wrap").html();
var container = $("#container");
var index = 0;
$("#wrap").html(html+html);
var height = $("#wrap").height()/2;
function auto() {
if(container.scrollTop() >= height){
index = 0;
container.scrollTop(index);
}else{
container.scrollTop(index++);
}
}
timer = setInterval("auto()",speed);
$("#box").hover(function(){
clearInterval(timer);
},function(){
timer = setInterval("auto()",speed);
})
</script>
无缝向左滚动
* {
margin: 0;
padding: 0;
}
#box{
width: 275px;
height: 120px;
border: 1px solid red;
margin: 50px auto;
overflow: hidden;
}
.con {
height: 120px;
overflow: hidden;
/*必须要-对于整个网页是不需要就会有滚动条,
而对于其他元素必须手动设置overflow:hidden,才能设置scrollTop,scollLeft 属性*/
}
ul {
position: relative;
list-style: none;
font-size: 0;
}
li {
display: inline-block;
padding: 0 10px;
font-size: 14px;
height: 24px;
line-height: 24px;
background-color: tomato;
}
li:nth-child(odd) {
background-color: #ccc;
}
js 部分
<script>
var timer = null;
var speed = 1000/40;
var html = $("#wrap").html();
var container = $("#container");
$("#wrap").html(html+html);
var len = $("#wrap").children('li').length;
// innerWidth() 是包含内边距的 不包括边框
var width = $("#wrap").children('li').eq(0).innerWidth();
var index = 0;
$("#wrap").width(len*width);
function auto() {
if(container.scrollLeft() >= $("#wrap").width()/2){
index = 0;
container.scrollLeft(index);
}else{
container.scrollLeft(index++);
}
}
timer = setInterval("auto()",speed);
/*$("#box").hover(function(){
clearInterval(timer);
},function(){
timer = setInterval("auto()",speed);
})*/
</script>
间歇性滚动 与 上面无缝滚动 html结构一样
<script>
var speed = 60;
var delay = 2000;
var wrap = document.getElementById("wrap");
var container = document.getElementById("container");
var box = document.getElementById("box");
var iHeight = 24*2;
var time;
wrap.innerHTML += wrap.innerHTML;
container.scrollTop = 0;
function move(){
container.scrollTop++;
time = setInterval("auto()",speed);
}
function auto() {
if(container.scrollTop % iHeight == 0){
clearInterval(time);
setTimeout("move()",delay);
}else{
container.scrollTop++;
if(container.scrollTop >= container.scrollHeight/2){
container.scrollTop = 0;
}
}
}
var timer = setTimeout("move()",0);
// 这个移入停止还没实现
</script>