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 ——文字上下左右滚动的更多相关文章

  1. js文字向上滚动代码

    js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px;  background:#edfafd; padding-top:2px; ...

  2. html js文字左右滚动插件

    自己写过很多插件,但都是直接嵌入在了工程里,从来没有拿出来单独封装成一个文件过,这是第一次,希望是一个良好的开端. 一个文字过长而可以左右滚动的插件 <!DOCTYPE html> < ...

  3. js文字上下滚动代码

    <div id="dome"> <div id="dome1"> <ul class="express"> ...

  4. js文字无缝滚动

    <div id=demo style="overflow:hidden; width:128px; height:300px;"> <div id=demo1&g ...

  5. js 实现文字列表滚动效果

    今天要实现抽奖名单在首页滚动展示的效果,就用js写了一个,代码如下: html代码: <style type="text/css"> *{margin:;padding ...

  6. JS平滑无缝滚动实现———实现首页广告自动滚动效果(附实例)

    本文我们实现纯JS方式的滚动广告效果. 先show一下成品: 首先是网页样式: 1. #demo { 2. background: #FFF; 3. overflow:hidden; 4. borde ...

  7. 利用js来实现文字的滚动(也就是我们常常见到的新闻版块中的公示公告)

    首先先看一下大致效果图(因为是动态的,在页面无法显示出来) 具体的实现代码如下: 1.首先是css代码: <style type="text/css"> body,ul ...

  8. js实现文字横向滚动

    页面布局      <div id="scroll_div" class="fl">         <div id="scroll ...

  9. js文字滚动效果实现

    纯js实现,完整代码如下: <!doctype html> <html lang="en"> <head> <meta http-equi ...

随机推荐

  1. VS的快捷操作

    连按两下Tab,生成代码块.修改i为n,再按一次Tab,对应位置自动改变. Ctrl+. 或者 Alt+Enter ctor  连按两下Tab,生成无返回值的构造函数(constructor func ...

  2. Dell PowerEdge服务器RAID卡驱动下载

    Dell PowerEdge服务器RAID卡驱动下载 DELL新阵列卡驱动下载 型号 支持系统驱动 H310/710 /710P/810 Win2008 x32 Windows 2008 x64 Wi ...

  3. docker Tomcat镜像

    docker中的管理命令 首先需要去下载镜像. [root@CFCA ~]# docker pull tomcat    下载镜像[root@CFCA ~]# docker images       ...

  4. GUI输入数据并保存

    from tkinter import * def write_to_file(): fileContent = open("deliveries.txt","a&quo ...

  5. 前端使用pdf.js预览pdf文件,超级简单

    现在的浏览器基本都支持直接把pdf文件拖到浏览器就可以打开了,不用安装额外的插件.但是不同的浏览器显示的页面不一样.如果我们想在网页上统一预览pdf怎样实现呢? Mozilla开源了一个插件pdf.j ...

  6. Groovy脚本基础全攻略

    1 背景 Groovy脚本基于Java且拓展了Java,所以从某种程度来说掌握Java是学习Groovy的前提,故本文适用于不熟悉Groovy却想快速得到Groovy核心基础干货的Java开发者(注意 ...

  7. ubuntu安装相关

    安装系统参考:http://xinzhi.wenda.so.com/a/1523530837610141 首先进行更新源 sudo apt-get update 安装codeblocks sudo a ...

  8. 网络通信框架之retrofit

    主页: [https://github.com/square/retrofit](https://github.com/square/retrofit) 注意: 使用Retrofit的前提是**服务器 ...

  9. python 连接oracle数据库:cx_Oracle

    注意:64位操作系统必须安装64位oracle客户端,否则会连接报错 安装第三方库:cx_Oracle 一.连接数据库的三种方式: 1.cx_Oracle.connect('账号/密码@ip:端口/数 ...

  10. 阶段3 3.SpringMVC·_03.SpringMVC常用注解_8 SessionAttributes注解

    SpringMvc提供的Model类 ModelMap继承LinkedHashMap 页面取值 把request这个对象全部输出了. SessionAttribute 取值 从sessionAttri ...