首页 > 语言 > JavaScript > 正文

javascript:文字不间断向左移动的实例代码

2024-05-06 14:37:00
字体:
来源:转载
供稿:网友
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"><!--
//主要的滚动函数
//如果滚动的效果正好是需要的效果
//那么你只需要直接调用这个函数即可
var moveTag=function(obj){
var _this = this;
_this.speed = 10;//移动的速度
_this.space = 10 * _this.speed;//移动间隔的时间
obj.scrollLeft=0;//初始化滚动的位置(主要是考虑到需要兼容FF)
var divList = obj.getElementsByTagName("DIV");
var obj2 = new Object();//包含所有滚动列的div
for(var i=0;i<divList.length;i++){
if(divList[i].parentNode==obj){
obj2=divList[i];
break;
}
}
//得到需要滚动的所有列
//divList循环两次的目的是为了不让样式名称影响滚动代码
//也就是尽可能的减少滚动代码与样式名称的耦合
var cellCount = 0;
_this.cellList = new Array();
for(var i=0;i<divList.length;i++){
if(divList[i].parentNode==obj2){
cellCount++;
_this.cellList.push(divList[i]);//需要滚动的所有列
}
}
_this.resetCellList=function(){
//这个函数主要是为了让滚动不间断
//在每次滚动到终点的时候需要使用
//将已经滚动过的列移到当前列之后
for(var i=0;i<_this.cellList.length-1;i++){
obj2.removeChild(_this.cellList[i]);
obj2.appendChild(_this.cellList[i]);
}
//重新获取_this.cellList
_this.cellList = new Array();
for(var i=0;i<divList.length;i++){
if(divList[i].parentNode==obj2) _this.cellList.push(divList[i]);
}
//alert(_this.cellList.length);
}
_this.resetForMoveRight=function(){
//这个函数主要是为了让滚动不间断
//与resetCellList的区别是这个函数是为了向右不间断滚动使用的
//在每次滚动到起点的时候需要使用
//将当前列之后的所有列移动到当前列之前
if(_this.cellList.length>0){
for(var i=_this.cellList.length-1;i>0;i--){
obj2.removeChild(_this.cellList[i]);
obj2.insertBefore(_this.cellList[i],obj2.childNodes[0]);
}
}
//重新获取_this.cellList
_this.cellList = new Array();
for(var i=0;i<divList.length;i++){
if(divList[i].parentNode==obj2) _this.cellList.push(divList[i]);
}
//alert(_this.cellList.length);
}
//alert(_this.cellList.length);
obj2.style.width = parseInt(obj.offsetWidth * cellCount) + "px";
//
//alert(_this.endScroll);
var cellScroll = obj.offsetWidth;//每次滚动需要滚动多少距离
var endScroll = obj2.offsetWidth - cellScroll;//计算滚动条的终点位置
//alert(_this.cellScroll);
//
_this.moveLength = cellScroll;//初始化移动偏量,为0的时候,在页面加载完毕之后将会立即移动;等于_this.cellScroll表示延迟一会再开始执行
_this.scrollEnd = false;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选