javascript滚动条之ScrollBar.js
ScrollBar.js是一个仅仅120行的滚动条JS插件,使用非常方便
详情阅读:https://git.oschina.net/wuquanyao/scrollbar.js
/*=================================
* ScrollBar.js beta
* Author:wuquanyao
* Email:wqynqa@163.com
*=================================*/
var ScrollBar = {};
(function(ScrollBar){
var parent,root,track,bar,tx,ch,h,sh,rate;
ScrollBar.adapter = function(config)
{
init(config);
}
function init(config)
{
parent = document.querySelector(config['selector']);
root = parent.parentNode;
createBar();
mouseaction();
}
function createBar()
{
track = document.createElement('div');
track.setAttribute('class','scroll-track');
bar = document.createElement('div');
bar.setAttribute('class','scroll-bar');
track.appendChild(bar);
root.appendChild(track);
sh = root.scrollHeight;
ch = root.offsetHeight;
tx = root.offsetTop;
h = ch/sh*ch;
if(h<30){
bar.style.height = '30px';
h = 30;
}else{
bar.style.height = h+'px';
}
rate = (sh-ch)/(ch-h);
}
function mouseaction()
{
function eventparse(obj,type,func){
if(document.attachEvent){
var events = {
click:'onclick',
mouseover:'onmouseover',
mousemove:'onmousemove',
mouseout:'onmouseout',
mousedown:'onmousedown',
mouseup:'onmouseup',
wheel:'DOMMouseScroll'
};
obj.attachEvent(events[type],func);
}else{
var events = {
click:'click',
mouseover:'mouseover',
mousemove:'mousemove',
mouseout:'mouseout',
mousedown:'mousedown',
mouseup:'mouseup',
wheel:'DOMMouseScroll'
};
obj.addEventListener(events[type],func,false);
}
}
function init(){
var bool = false,v;
eventparse(bar,'mousedown',function(event){
bool = true;
});
eventparse(document,'mousemove',function(event){
if(bool){
if(event.clientY<=(tx+10)){
v = 0;
}else if(event.clientY>=(tx+ch-h)){
v = tx+ch-h;
}else{
v = event.clientY;
}
parent.style.transform = 'translate(0px,'+(-v*rate)+'px)';
bar.style.transform = 'translateY('+v+'px)';
}
});
eventparse(document,'mouseup',function(event){
bool = false;
});
// eventparse(track,'click',function(event){
// event.stopPropagation();
// bar.style.transition = 'all 0ms ease 0ms';
// if(event.clientY<(tx+h)){
// bar.style.transform = 'translate(0px,0px)';
// }else if(event.clientY>=(tx+ch-h)){
// bar.style.transform = 'translate(0px,'+(tx+ch-h)+'px)';
// }else{
// bar.style.transform = 'translate(0px,'+(event.clientY-h/2)+'px)';
// }
// parent.style.transform = 'translate(0px,'+((-event.clientY+tx)*rate)+'px)';
// });
var offset=0;
if (window.navigator.userAgent.indexOf("Firefox") >= 0) {
eventparse(parent,'wheel',wheelEvent);
}else{
parent.onmousewheel=parent.onmousewheel=wheelEvent;
}
function wheelEvent(e){
var transform,bO,wv = (e.detail/3*20) || (-(e.wheelDelta/120*20));
if((offset<(sh-ch) &&(offset>=0))){
transform = 'translate(0px,'+(-offset)+'px)';
bO = 'translateY('+(offset/rate)+'px)';
offset = ((offset+wv)>(sh-ch))?offset:( ((offset+wv)<=0)?0:(offset+wv) );
}
bar.style.transform = bO;
parent.style.transform = transform;
}
}
init();
}
})(ScrollBar);
javascript滚动条之ScrollBar.js的更多相关文章
- 【JavaScript】使用纯JS实现多张图片的懒加载(附源码)
一.效果图如下 上面的效果图,效果需求如下 1.还没加载图片的时候,默认显示加载图片背景图 2.刚开始进入页面,自动加载第一屏幕的图片 3.下拉界面,当一张图片容器完全显露出屏幕,即刻加载图片,替换背 ...
- 初探JavaScript(三)——JS带我"碰壁"带我飞
已经写了两篇关于小白的JavaScript之行,不可否认,每一种语言都有其精华与糟粕之处,来不及细细体味其精华奥妙,也没法对其评头论足,只能先了解,后深入.到目前为止已经看完<JavaScrip ...
- 初探JavaScript(二)——JS如何动态操控HTML
除去五一三天,我已经和<JavaScript Dom编程艺术>磨合了六天,第一印象很好.慢慢的,我发现这是一块排骨,除了肉还有骨头.遇到不解的地方就会多看几遍,实在不懂的先跳过,毕竟,初次 ...
- 强大的JavaScript动画图形库mo.js
最近在学习前端动画方面知识时发现了挺有趣的一个动画的图形库mo.js,页面效果真是酷炫,有兴趣的同学可以研究下:). 酷炫的效果: 以下是官方的demo效果,更多详情请查看 mo.js http:// ...
- 省市县 三级 四级联动Javascript JQ 插件PCASClass.js
想要使用这款组件,需要页面引入 PCASClass.js 核心文件,该文件在您的HTML文档<head>标签之内. <script type="text/javascrip ...
- 【转】《高级前端3.6》JavaScript多线程——Concurrent.Thread.js, WebWork
原文链接:http://www.cnblogs.com/woodk/articles/5199536.html JavaScript多线程,在HTML5 WebWork没出现之前很多人都是用Concu ...
- JavaScript模板引擎Template.js使用详解
这篇文章主要为大家详细介绍了JavaScript模板引擎Template.js使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 template.js 一款 JavaScript 模板引 ...
- 坑人的 Javascript 模块化编程 require.js
坑人的 Javascript 模块化编程 require.js
- 坑人的 Javascript 模块化编程 sea.js
坑人的 Javascript 模块化编程 sea.js 忧伤 加 蛋疼的 开始了 看文档 Sea.js 进行配置 seajs.config({ // 设置路径,方便跨目录调用 paths: { 'ar ...
随机推荐
- [CareerCup] 5.4 Explain Expression ((n & (n-1)) == 0) 解释表达式
5.4 Explain what the following code does: ((n & (n-1)) == 0). 这道题让我们解释一个表达式((n & (n-1)) == 0 ...
- 信息安全系统设计基础exp_3
详情请看搭档20135322郑伟博客,传送门如下:http://www.cnblogs.com/zhengwei0712/p/4996017.html
- GDB代码调试与使用
GDB代码调试与使用 Linux下GDB调试代码 源代码 编译生成执行文件 gcc -g test.c -o test 使用GDB调试 启动GDB:gdb test 从第一行列出源代码:list 直接 ...
- 百度地图ip定位,不算bug的bug
做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...
- Object C学习笔记22-#define 用法
上一篇讲到了typedef 关键字的使用,可以参考文章 Object C 学习笔记--typedef用法 .而在c中还有另外一个很重要的关键字#define. 一. #define 简介 在C中利用预 ...
- [转载]NSString中判断中文,英文,数字
曾有需求做个用户名中非法字符的判断,要求是只能输入中英文和数字,其他字符一律非法,故写了下边一个程序mark一下吧~~ NSString *testString = @"春1mianBU觉晓 ...
- 每天一个linux命令(16):whereis 命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和 find相比,whereis查找的速度 ...
- Beta 分工比例
组员在Beta版本的分工和个人贡献百分比. 人员 任务完成情况 贡献比 031302331 闹钟,爬取知乎数据,书籍下载,解决bug,帮助队友 25% 031302442 注册登录逻辑,书籍评论评分页 ...
- WebView与JavaScript的交互
目录: 一.整体思路 二.简单例子实现过程 1.打开项目的asset目录,创建新的文件test.html 2.补充html代码:添加供本地调用的js方法.调用本地方法的js ...
- 开课选课系统APP基本功能实现
队员:031302511 031302505 效果图如下: 上述功能分析: 功能分析:我们考虑到手机自带的SQLite会被清理软件清理掉数据,这样就没有实际用处.所以我们就自己搭建服务器 ...