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 1.7.2中getAll方法的疑惑分析
May 23 Javascript
jQuery的3种请求方式$.post,$.get,$.getJSON
Mar 28 Javascript
浅谈javascript中自定义模版
Jan 29 Javascript
javascript日期格式化方法小结
Dec 17 Javascript
JavaScript简单实现弹出拖拽窗口(一)
Jun 17 Javascript
jQuery制作网页版选项卡
Jul 28 Javascript
JS实现微信弹出搜索框 多条件查询功能
Dec 13 Javascript
Vue原理剖析 实现双向绑定MVVM
May 03 Javascript
jQuery模拟12306城市选择框功能简单实现方法示例
Aug 13 jQuery
Vue 菜单栏点击切换单个class(高亮)的方法
Aug 22 Javascript
JS异步宏队列微队列原理详解
Sep 09 Javascript
Vite和Vue CLI的优劣
Jan 30 Vue.js
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
PHP5函数小全(分享)
2013/06/06 PHP
php 启动时报错的简单解决方法
2014/01/27 PHP
php生成二维码不保存服务器还有下载功能的实现代码
2018/08/09 PHP
jQuery Ajax 实例全解析
2011/04/20 Javascript
js获取class的所有元素
2013/03/28 Javascript
jQuery使用元素属性attr赋值详解
2015/02/27 Javascript
全面解析多种Bootstrap图片轮播效果
2016/05/27 Javascript
Bootstrap Metronic完全响应式管理模板之菜单栏学习笔记
2016/07/08 Javascript
原生javascript 学习之js变量全面了解
2016/07/14 Javascript
JavaScript计算器网页版实现代码分享
2016/07/15 Javascript
jQuery实现鼠标经过购物车出现下拉框代码(推荐)
2016/07/21 Javascript
JS设计模式之访问者模式定义与用法分析
2018/02/05 Javascript
微信运维交互机器人的示例代码
2018/11/12 Javascript
使用Node.js在深度学习中做图片预处理的方法
2019/09/18 Javascript
一起来了解一下JavaScript的预编译(小结)
2021/03/01 Javascript
[01:04:02]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第二场 1月24日
2021/03/11 DOTA
常见python正则用法的简单实例
2016/06/21 Python
Python两个内置函数 locals 和globals(学习笔记)
2016/08/28 Python
python中文分词,使用结巴分词对python进行分词(实例讲解)
2017/11/14 Python
Python常见数据类型转换操作示例
2019/05/08 Python
Python OpenCV实现鼠标画框效果
2020/08/19 Python
Python3 使用pillow库生成随机验证码
2019/08/26 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
2020/02/21 Python
Python基于Twilio及腾讯云实现国际国内短信接口
2020/06/18 Python
Python3合并两个有序数组代码实例
2020/08/11 Python
美国滑雪和滑雪板商店:Buckman
2018/03/03 全球购物
俄罗斯名牌服装网上商店:UNIQUE FABRIC
2019/07/25 全球购物
入党自我评价优缺点
2014/01/25 职场文书
商场中秋节活动方案
2014/02/07 职场文书
仓库管理员岗位职责
2014/03/19 职场文书
工程质量承诺书
2014/03/27 职场文书
中国汉字听写大会观后感
2015/06/02 职场文书
莫言诺贝尔获奖感言(全文)
2015/07/31 职场文书
2016婚礼主持词开场白
2015/11/24 职场文书
帮你提高开发效率的JavaScript20个技巧
2021/06/18 Javascript
python自动计算图像数据集的RGB均值
2021/06/18 Python