纯Js ——文字上下左右滚动

ScrollBaseJs.js
var $$ = function (id) {
return typeof id == 'string' ? document.getElementById(id) : id;
};
Object.extend = function (destination, sourse) {
for (var item in sourse) {
destination[item] = sourse[item];
}
return destination;
};
var Class = {
create: function () {
return function () {
this.initialize.apply(this, arguments);
}
}
}
var CurrentStyle = function (element) {
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
//容器对象,滑块对象,参数
var ScrollCon = Class.create();
ScrollCon.prototype = {
//构造函数
initialize: function (area, bar, itag, options, ibarTgNum) {
this._area = $$(area);
this._bar = $$(bar);
this._time = null;
this._barTgNum = $$(ibarTgNum);
this.SetOption(options);
this._step = this._options.step; //改变量
this._pause = this._options.pause;
this._speed = this._options.speed;
this._mgSide = this._options.side == "up" ? "marginTop" : "marginLeft";
this._index = 0; //滚动索引
this._tagIndex = 1; //滚动子项索引
this._bar.appendChild(this._bar.cloneNode(true));
this._barTagNum = this._bar.getElementsByTagName(itag).length;
this.barH = this._bar.style.height || this._bar.offsetHeight;
this.areaH = this._area.style.height || this._area.offsetHeight;
this.areaW = this._area.style.width || this._bar.offsetWidth;
this.barW = this._bar.style.width || this._bar.offsetWidth;
this._bar.style[this._mgSide] = "0px";
this.BindNumHtml();
//添加样式
// this.Auto();
},
//参数初始化
SetOption: function (options) {
//默认参数
this._options = {
step: 1, //改变量,
side: "up",
pause: 50, //隔多高停一次
speed: 20, //滚动速度
pauseStep: 1500//滚动停留时间
};
Object.extend(this._options, options || {});
},
EditMargin: function () { //改变滚动条属性
oThis = this;
var Step = this._step;
this._speed = this._options.speed;
if (this._index >= this._pause) { //判断是否到了停顿的时候
this._index = Step = 0; //索引,滚动改变量设为0
this._speed = this._options.pauseStep; //滚动速度设为滚动停留时间
this._tagIndex++;
} else {
this._index += Step;
}
var margin = Math.abs(parseInt(oThis._bar.style[this._mgSide]));
this._bar.style[this._mgSide] = -(margin + Step) + "px";
if (margin + Step >= (this._barTagNum - 1) * this._pause) {
// 将滚动距离跳到滚动条复制前的最后一个标签
this._bar.style[this._mgSide] = -(this._barTagNum / 2 - 1) * this._pause + "px";
this._tagIndex = this._barTagNum / 2; //直接赋值:即将要跳转的值为滚动条复制“值” 的第一个标签
}
oThis.Write(this.barW);
},
//获得偏移距离
GetMargin: function () {
return this._bar.style[this._mgSide];
},
Auto: function () {//自动滚动
oThis = this; oThis.EditMargin();
oThis.Re();
},
MoveTo: function (index) {
oThis = this;
this._index = 0;
this._tagIndex = index;
this._speed = 1000;
oThis._bar.style[this._mgSide] = -index * this._pause + "px";
oThis.Re();
oThis.Write();
},
Move: function () {
oThis = this;
oThis.EditMargin();
this._time = setTimeout(function () {
oThis.Start();
}, this._speed);
},
Start: function () {
oThis = this;
oThis.EditMargin();
},
Next: function () {
oThis = this;
this._index = 0;
this._speed = 1000;
this._tagIndex == 0 ? 1 : this._tagIndex;
this._tagIndex++;
var margin = this._tagIndex * this._pause;
if (this._tagIndex >= this._barTagNum) {
this._tagIndex = 0;
margin = 0;
}
oThis._bar.style[this._mgSide] = -margin + "px";
oThis.Re();
oThis.Write(this._tagIndex);
},
Re: function () {
oThis = this;
clearTimeout(this._time);
this._time = setTimeout(function () {
oThis.Auto();
}, this._speed);
oThis.Write(this._tagIndex);
},
Pre: function () {
oThis = this;
var margin = Math.abs(parseInt(oThis._bar.style[this._mgSide])) - this._pause;
if (margin == -this._pause) {
margin = 0;
}
oThis._bar.style[this._mgSide] = -margin + "px";
oThis.Re();
},
BindNumHtml: function () {
oThis = this;
for (var i = 1; i <= oThis._barTagNum / 2; SetHTML(i++)) { }
function SetHTML(z) {
var barNum = oThis._barTgNum.appendChild(document.createElement("li"));
barNum.onclick = function () {
oThis._bar.style.marginTop = oThis.MoveTo(z);
}
barNum.innerHTML = z--;
barNum.style.cursor = "pointer";
barNum.style.listStyle = "none";
barNum.style.float = "left";
barNum.style.paddingLeft = "5px";
}
},
Write: function (value) {
oThis = this;
var strHtml = '<div style="border:red solid 1px;width:200px;float:left;">';
strHtml += '<br>滚动区域高度:' + oThis.areaH;
strHtml += '<br>滚动对象高度:' + oThis.barH;
strHtml += '<br>滚动条距离:' + oThis.GetMargin();
strHtml += '<br>oThis._index:' + oThis._index;
strHtml += '<br>this._step:' + this._step;
strHtml += '<br>_tagIndex:' + oThis._tagIndex;
strHtml += '<br>_barTagNum:' + oThis._barTagNum;
strHtml += '<br>value:' + value;
strHtml += "</div>";
$$("divWrite").innerHTML = strHtml;
}
}
Scroll_8_31.htm:
<!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>
<title></title>
<style type="text/css">
.con
{
width: 400px;
height: 50px;
border: solid 1px #000000;
line-height: 50px;
padding: 0px 10px;
overflow: hidden;
}
.con *
{
margin: 0px;
padding: 0px;
}
.conList
{
padding: 0px;
width: 5000px;
} .conList li
{
float: left;
width: 390px;
list-style: none;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="divCon" class="con">
<ul id="divConList" class="conList" style="">
<li>热点新闻1</li>
<li>热点新闻2</li>
<li>热点新闻3</li>
<li>热点新闻4</li>
<li>热点新闻5</li>
<li>热点新闻6</li>
</ul>
</div>
<span></span>
<ul id="barNum" style="float: left; border: red solid 1px; text-align: left">
</ul>
<input type="button" value="上一个" onclick="a.Pre()" />
<input type="button" value="下一个" onclick="a.Next()" />
<input type="button" value="自动" onclick="a.Auto()" />
<input type="button" value="重启" onclick="a.Re()" />
<div id="divWrite">
</div>
</body>
</html>
<script src="ScrollBaseJs.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
window.a = new ScrollCon("divCon", "divConList", "li"
, { speed: 10, pause: 400, pauseStep: 500, side: "left" }, "barNum");
}
</script>
纯Js ——文字上下左右滚动的更多相关文章
- js文字向上滚动代码
js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px; background:#edfafd; padding-top:2px; ...
- html js文字左右滚动插件
自己写过很多插件,但都是直接嵌入在了工程里,从来没有拿出来单独封装成一个文件过,这是第一次,希望是一个良好的开端. 一个文字过长而可以左右滚动的插件 <!DOCTYPE html> < ...
- js文字上下滚动代码
<div id="dome"> <div id="dome1"> <ul class="express"> ...
- js文字无缝滚动
<div id=demo style="overflow:hidden; width:128px; height:300px;"> <div id=demo1&g ...
- js 实现文字列表滚动效果
今天要实现抽奖名单在首页滚动展示的效果,就用js写了一个,代码如下: html代码: <style type="text/css"> *{margin:;padding ...
- JS平滑无缝滚动实现———实现首页广告自动滚动效果(附实例)
本文我们实现纯JS方式的滚动广告效果. 先show一下成品: 首先是网页样式: 1. #demo { 2. background: #FFF; 3. overflow:hidden; 4. borde ...
- 利用js来实现文字的滚动(也就是我们常常见到的新闻版块中的公示公告)
首先先看一下大致效果图(因为是动态的,在页面无法显示出来) 具体的实现代码如下: 1.首先是css代码: <style type="text/css"> body,ul ...
- js实现文字横向滚动
页面布局 <div id="scroll_div" class="fl"> <div id="scroll ...
- js文字滚动效果实现
纯js实现,完整代码如下: <!doctype html> <html lang="en"> <head> <meta http-equi ...
随机推荐
- kafka的maxPollIntervalMs设置太小引发的惨案 (转)
本地启动kafka后,不断报一下信息: 表示本地consumer节点在不断的重新加入group,并且不断伴随着offset commit失败. 具体原因是因为ConsumerCoordinator没有 ...
- css实现翻面效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用Navicat客户端运行SQL语句出现中文乱码
出现乱码无非就是编码方式不统一造成的,通过查阅资料解决了问题. (简 体中文系统环境支持国标 GB2312.GB18030 和 Unicode (UTF-8) 编码.它们在系统中设置的locale(亦 ...
- luogu 3441 [POI2006]MET-Subway 拓扑排序+思维
Description 给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多. Input 第一行两个正整数N.L(2 <= N <= 1,000,000, ...
- canvas addHitRegion()
CanvasRenderingContext2D.addHitRegion() 是 Canvas 2D API 给位图添加点击区域的方法. 但是 mobile 都不兼容
- rename、remove
/*** remove.c ***/ #include<stdio.h> int main() { remove("./b.txt"); } 运行结果: ubuntu1 ...
- JavaWeb_(Hibernate框架)Hibernate与c3p0与Dbutils的区别
JavaWeb_(Hibernate框架)使用Hibernate开发用户注册功能 传送门 JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能 传送门 Hiberna ...
- Pytorch:使用GPU训练
1.模型转为cuda gpus = [0] #使用哪几个GPU进行训练,这里选择0号GPU cuda_gpu = torch.cuda.is_available() #判断GPU是否存在可用 net ...
- 学号 20175329 《Java程序设计》第10周学习总结
20175329 <Java程序设计>第十周学习总结 教材学习内容总结 线程与进程 进程时程序的一次动态执行过程.线程是比进程更小的执行单位,一个进程在其执行过程中,可以产生多个线程. J ...
- mysql java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone.
连接数据库时报错: The server time zone value '?й???????' is unrecognized or represents more than one time zo ...