闲来无事写了一个文字无缝滚动,直接上代码,也可将文字替换成相应的图片。。
HTML代码:
<div class="box" id="box">
<div class="div1" id="div1">文本内容df文本内容re文本er内容文本内g容文本内容文本内容文本内容文本内容文本内vgg容文本内容文本内容文本内容文本内容文本内容文本dfgsd容文本内dfdf容文本内容文本内gf文本内容文本gd本df内容文本g内s容文本内容</div>
<div class="div1" id="div2"></div>
</div>
CSS代码:
.box{width:200px;height:24px;padding:5px;overflow:Hidden;white-space:nowrap;border:dashed 1px #eee;}
.div1{display:inline;height:24px;line-height:24px;white-space:nowrap;}
JS代码:
var box = document.getElementById('box');
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
div2.innerHTML = div1.innerHTML;
function mar(){
if(div2.offsetWidth - box.scrollLeft <= 0){
box.scrollLeft = 0;
}else {
box.scrollLeft++;
}
}
var mqar = setInterval(mar,30);
box.onmousemove = function(){
clearInterval(mqar);
}
box.onmouseout = function(){
mqar = setInterval(mar,30);
}