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


Posted in Javascript onAugust 08, 2013
<!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; 
_this.scrollTimes = 0; 
//休息一会儿 
_this.sleep=function(){ 
_this.scrollTimes++; 
if(_this.scrollTimes>=_this.space){ 
_this.scrollTimes=0; 
_this.moveLength=0; 
} 
}; 
_this.moveStart = true;//是否开始移动 
_this.isMoveLeft = true;//是否向左移动 
_this.move=function(){ 
obj.onmouseover=function(){ 
_this.moveStart=false; 
}; 
obj.onmouseout=function(){ 
_this.moveStart=true; 
}; 
//重新设定cellList 
if(obj.scrollLeft>=endScroll && _this.isMoveLeft){//向左移动,并且已经移动到尽头 
if(_this.moveLength > 0) _this.moveLength = cellScroll;//调整 
if(_this.cellList.length>0){ 
_this.resetCellList();//更新cellList 
obj.scrollLeft=0; 
}else{ 
_this.scrollEnd = true; 
} 
}else if(obj.scrollLeft<=0 && !_this.isMoveLeft){//向右移动,并且已经移动到尽头 
if(_this.moveLength > 0) _this.moveLength = cellScroll;//调整 
if(_this.cellList.length>0){ 
_this.resetForMoveRight();//更新cellList 
obj.scrollLeft=endScroll; 
}else{ 
_this.scrollEnd = false; 
} 
} 
// 
if(_this.scrollEnd){//向左移动 
if(_this.moveLength<cellScroll && _this.moveStart){ 
obj.scrollLeft--; 
_this.moveLength++; 
}else if(_this.moveLength>=cellScroll){ 
_this.sleep(); 
} 
} 
else{//向右移动 
if(_this.moveLength<cellScroll && _this.moveStart){ 
obj.scrollLeft++; 
_this.moveLength++; 
}else if(_this.moveLength>=cellScroll){ 
_this.sleep(); 
} 
} 
}; 
//自动 
_this.start=function(){ 
setInterval(_this.move,_this.speed); 
}; 
//右移动 
_this.moveRight=function(){ 
_this.scrollEnd = true;//已经滚动到尽头 
_this.isMoveLeft = false;//向右滚动 
_this.scrollTimes=0; 
}; 
//左移动 
_this.moveLeft=function(){ 
_this.scrollEnd = false;//没有滚动到尽头 
_this.isMoveLeft = true;//向左滚动 
_this.scrollTimes=0; 
}; 
}; 
// --></script> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<style><!-- 
#list{border:#ccc 1px solid;} 
#list div div{line-height:30px;text-align:center;border-right:#ccc 1px solid;} 
#list{width:150px;height:30px;overflow:hidden;}/*必须的属性,宽度、高度可以自定义*/ 
#list div div{width:150px;height:30px;float:left;}/*必须的属性,宽度、高度最好与#list的定义一样*/ 
--></style><style mce_bogus="1">#list{border:#ccc 1px solid;} 
#list div div{line-height:30px;text-align:center;border-right:#ccc 1px solid;} 
#list{width:150px;height:30px;overflow:hidden;}/*必须的属性,宽度、高度可以自定义*/ 
#list div div{width:150px;height:30px;float:left;}/*必须的属性,宽度、高度最好与#list的定义一样*/</style> 
<title>无标题文档</title> 
</head> 
<body> 
<div id="list"> 
<div> 
<div>第一列</div> 
<div>第二列</div> 
<div>第三列</div> 
<div>第四列</div> 
<div>第五列</div> 
</div> 
</div> 
<a href="javascript:moveLeft()" mce_href="javascript:moveLeft()">左移动</a> 
<a href="javascript:moveRight()" mce_href="javascript:moveRight()">右移动</a> 
<script type="text/javascript"><!-- 
var move=new moveTag(document.getElementById("list")); 
move.start(); 
move.speed=10;//滚动的速度,默认为10 
//move.space=0;//滚动间隔时间,默认为滚动速度 * 16 
//左移动只能移动到最左边,无法一直向左移 
//为了使滚动没有间断,所以最左边的坐标是不断变化的 
//move.moveLeft();//这样可以设置默认自右向左移动 
//move.moveRight();//与move.moveLeft()反方向 
var moveLeft=move.moveLeft; 
var moveRight=move.moveRight; 
//想要一个页面上有N个地方滚动? 
//没问题!只需要多new几个实例即可实现 
// --></script> 
</body> 
</html>
Javascript 相关文章推荐
jquery miniui 教程 表格控件 合并单元格应用
Nov 25 Javascript
用box固定长宽实现图片自动轮播js代码
Jun 09 Javascript
轻松掌握JavaScript中的Math object数学对象
May 26 Javascript
JS触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
Oct 30 Javascript
vue中axios处理http发送请求的示例(Post和get)
Oct 13 Javascript
使用ngrok+express解决本地环境中微信接口调试问题
Feb 26 Javascript
jQuery中$原理实例分析
Aug 13 jQuery
vue中使用axios post上传头像/图片并实时显示到页面的方法
Sep 27 Javascript
JavaScript 对引擎、运行时、调用堆栈的概述理解
Oct 22 Javascript
Vue 页面状态保持页面间数据传输的一种方法(推荐)
Nov 01 Javascript
React如何实现浏览器打印部分内容详析
May 19 Javascript
Emberjs 通过 axios 下载文件的方法
Sep 03 Javascript
js获得地址栏?问号后参数的方法
Aug 08 #Javascript
让页面上两个div中的滚动条(滑块)同步运动示例
Aug 07 #Javascript
js动态创建、删除表格示例代码
Aug 07 #Javascript
关于extjs4如何获取grid修改后的数据的问题
Aug 07 #Javascript
js取两个数组的交集|差集|并集|补集|去重示例代码
Aug 07 #Javascript
js使浏览器窗口最大化实现代码(适用于IE)
Aug 07 #Javascript
javascript一元操作符(递增、递减)使用示例
Aug 07 #Javascript
You might like
用PHP读取RSS feed的代码
2008/08/01 PHP
基于PHP选项与信息函数的使用详解
2013/05/10 PHP
CI框架数据库查询之join用法分析
2016/05/18 PHP
jquery select(列表)的操作(取值/赋值)
2011/03/16 Javascript
DOM基础教程之使用DOM控制表格
2015/01/20 Javascript
浅谈document.write()输出样式
2015/05/07 Javascript
JavaScript实现99乘法表及隔行变色实例代码
2016/02/24 Javascript
JS基于clipBoard.js插件实现剪切、复制、粘贴
2016/05/03 Javascript
AngularJS操作键值对象类似java的hashmap(填坑小结)
2016/11/12 Javascript
jquery对所有input type=text的控件赋值实现方法
2016/12/02 Javascript
vue bootstrap小例子一枚
2017/06/09 Javascript
javascript将list转换成树状结构的实例
2017/09/08 Javascript
以v-model与promise两种方式实现vue弹窗组件
2018/05/21 Javascript
对Vue- 动态元素属性及v-bind和v-model的区别详解
2018/08/27 Javascript
vue+VeeValidate 校验范围实例详解(部分校验,全部校验)
2018/10/19 Javascript
利用Webpack实现小程序多项目管理的方法
2019/02/25 Javascript
React中使用外部样式的3种方式(小结)
2019/05/28 Javascript
nuxt.js添加环境变量,区分项目打包环境操作
2020/11/06 Javascript
Python实现网站表单提交和模板
2019/01/15 Python
在python中求分布函数相关的包实例
2020/04/15 Python
Python定义一个函数的方法
2020/06/15 Python
基于Python下载网络图片方法汇总代码实例
2020/06/24 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
利用python实现后端写网页(flask框架)
2021/02/28 Python
毕业生个人求职的自我评价
2013/10/28 职场文书
文化活动实施方案
2014/03/28 职场文书
文明班集体申报材料
2014/05/23 职场文书
优秀学生党员先进事迹材料
2014/05/29 职场文书
施工单位安全责任书
2014/07/24 职场文书
实习单位证明范例
2014/11/17 职场文书
2014年大堂经理工作总结
2014/11/21 职场文书
护理实习生带教计划
2015/01/16 职场文书
公司员工违法违章行为检讨书
2019/06/24 职场文书
vue组件的路由高亮问题解决方法
2021/05/11 Vue.js
html5调用摄像头截图功能
2022/01/18 Javascript
vue项目proxyTable配置和部署服务器
2022/04/14 Vue.js