纯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 ...
随机推荐
- js 原生ajax请求
什么是ajax 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). XMLHttpRequest 用于在后台与服务器交换数据.这意味着可 ...
- BFC的理解
一.BFC概念 BFC即Block Formatting Contexts(块级格式化上下文),它属于普通流.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其他子元素将如何定位,以及和其他元素 ...
- oracle rowtype
v_customer customerinfo%rowtype; select * into v_customer from customerinfo where guid = v_loan.cust ...
- redis 关闭持久化 实验验证
前言 由于redis持久化(RDB),导致我们的线上的磁盘被写炸 线上服务器是 64H 512G 大概写了rdb文件是 200G左右,写满了当时的目录 处理策略 关闭持久化,由于之前的现象表示,我们线 ...
- 【知识库】-数据库_MySQL性能分析之Query Optimizer
简书作者:Sio 文章出处: MySql优化之索引原理与 SQL 优化 Query Optimizer MySQL Optimizer是一个专门负责优化SELECT 语句的优化器模块,它主要的功能就是 ...
- LeetCode---Backtracking && DP
**322. Coin Change 思路:动态规划,构造一个数组,存入当前index最少需要多少个coin public int coinChange(int[] coins, int amount ...
- redux异步
在一个项目中 redux 是必不可少的,redux 中没有提供异步的操作,但是异步又是项目开发中重要的一部分,所以我们的 redux 对此有进行了拓展: 所以我们需要 redux-thunk 的插件, ...
- python全栈开发第6天
作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 3) 为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区 ...
- OGG-01201
OGG-01201 Table of Contents 1. OGG-01201 1.1. 案例1 1.2. 案例2 1 OGG-01201 这种错误,出现的场景之一是 direct load 加载数 ...
- flutter 切换tab后保留tab状态
前言 最近在用flutter写一个小项目,在写主页面(底部导航栏+子页面)时遇到的一个问题:当点击底部item切换到另一页面, 再返回此页面时会重走它的initState方法(我们一般在initSta ...