ScrollBar.js是一个仅仅120行的滚动条JS插件,使用非常方便 详情阅读:https://git.oschina.net/wuquanyao/scrollbar.js/*=================================  * ScrollBar.js beta  * Author:wuquanyao  * Email:

ScrollBar.js是一个仅仅120行的滚动条JS插件,使用非常方便
详情阅读:https://git.oschina.net/wuquanyao/scrollbar.js

    1. /*=================================
    2.  * ScrollBar.js beta
    3.  * Author:wuquanyao
    4.  * Email:wqynqa@163.com
    5.  *=================================*/
    6. var ScrollBar = {};
    7. (function(ScrollBar){
    8.     var parent,root,track,bar,tx,ch,h,sh,rate;
    9.     ScrollBar.adapter = function(config)
    10.     {
    11.         init(config);
    12.     }
    13.     function init(config)
    14.     {
    15.         parent = document.querySelector(config['selector']);
    16.         root   = parent.parentNode;
    17.         createBar();
    18.         mouseaction();
    19.     }
    20.     function createBar()
    21.     {
    22.         track = document.createElement('div');
    23.         track.setAttribute('class','scroll-track');
    24.         bar   = document.createElement('div');
    25.         bar.setAttribute('class','scroll-bar');
    26.         track.appendChild(bar);
    27.         root.appendChild(track);
    28.         sh = root.scrollHeight;
    29.         ch = root.offsetHeight;
    30.         tx = root.offsetTop;
    31.         h  = ch/sh*ch;
    32.         if(h<30){
    33.             bar.style.height = '30px';
    34.             h = 30;
    35.         }else{
    36.             bar.style.height = h+'px';
    37.         }
    38.         rate = (sh-ch)/(ch-h);
    39.     }
    40.     function mouseaction()
    41.     {
    42.         function eventparse(obj,type,func){
    43.             if(document.attachEvent){
    44.                 var events = {
    45.                     click:'onclick',
    46.                     mouseover:'onmouseover',
    47.                     mousemove:'onmousemove',
    48.                     mouseout:'onmouseout',
    49.                     mousedown:'onmousedown',
    50.                     mouseup:'onmouseup',
    51.                     wheel:'DOMMouseScroll'
    52.                 };
    53.                 obj.attachEvent(events[type],func);
    54.             }else{
    55.                 var events = {
    56.                     click:'click',
    57.                     mouseover:'mouseover',
    58.                     mousemove:'mousemove',
    59.                     mouseout:'mouseout',
    60.                     mousedown:'mousedown',
    61.                     mouseup:'mouseup',
    62.                     wheel:'DOMMouseScroll'
    63.                 };
    64.                 obj.addEventListener(events[type],func,false);
    65.             }
    66.         }
    67.         
    68.         function init(){
    69.             var bool = false,v;
    70.             eventparse(bar,'mousedown',function(event){
    71.                 bool  = true;
    72.             });            
    73.             eventparse(document,'mousemove',function(event){
    74.                 if(bool){
    75.                     if(event.clientY<=(tx+10)){
    76.                         v = 0;
    77.                     }else if(event.clientY>=(tx+ch-h)){
    78.                         v = tx+ch-h;
    79.                     }else{
    80.                         v = event.clientY;
    81.                     }
    82.                     parent.style.transform = 'translate(0px,'+(-v*rate)+'px)'; 
    83.                     bar.style.transform = 'translateY('+v+'px)';
    84.                 }
    85.             });
    86.             eventparse(document,'mouseup',function(event){ 
    87.                 bool = false;
    88.             });
    89.             // eventparse(track,'click',function(event){
    90.             //     event.stopPropagation();
    91.             //     bar.style.transition = 'all 0ms ease 0ms';
    92.             //     if(event.clientY<(tx+h)){
    93.             //         bar.style.transform = 'translate(0px,0px)';
    94.             //     }else if(event.clientY>=(tx+ch-h)){
    95.             //         bar.style.transform = 'translate(0px,'+(tx+ch-h)+'px)';
    96.             //     }else{
    97.             //         bar.style.transform = 'translate(0px,'+(event.clientY-h/2)+'px)';
    98.             //     }
    99.             //     parent.style.transform = 'translate(0px,'+((-event.clientY+tx)*rate)+'px)'; 
    100.             // });
    101.             var offset=0;
    102.             if (window.navigator.userAgent.indexOf("Firefox") >= 0) {
    103.                 eventparse(parent,'wheel',wheelEvent);    
    104.             }else{
    105.                 parent.onmousewheel=parent.onmousewheel=wheelEvent;
    106.             }
    107.             function wheelEvent(e){
    108.                 var transform,bO,wv = (e.detail/3*20) || (-(e.wheelDelta/120*20));
    109.                 if((offset<(sh-ch) &&(offset>=0))){
    110.                     transform = 'translate(0px,'+(-offset)+'px)'; 
    111.                     bO = 'translateY('+(offset/rate)+'px)';
    112.                     offset = ((offset+wv)>(sh-ch))?offset:( ((offset+wv)<=0)?0:(offset+wv) );
    113.                 }
    114.                 bar.style.transform = bO;
    115.                 parent.style.transform = transform; 
    116.             }
    117.         }
    118.         init();
    119.     }
    120. })(ScrollBar);

javascript滚动条之ScrollBar.js的更多相关文章

  1. 【JavaScript】使用纯JS实现多张图片的懒加载(附源码)

    一.效果图如下 上面的效果图,效果需求如下 1.还没加载图片的时候,默认显示加载图片背景图 2.刚开始进入页面,自动加载第一屏幕的图片 3.下拉界面,当一张图片容器完全显露出屏幕,即刻加载图片,替换背 ...

  2. 初探JavaScript(三)——JS带我"碰壁"带我飞

    已经写了两篇关于小白的JavaScript之行,不可否认,每一种语言都有其精华与糟粕之处,来不及细细体味其精华奥妙,也没法对其评头论足,只能先了解,后深入.到目前为止已经看完<JavaScrip ...

  3. 初探JavaScript(二)——JS如何动态操控HTML

    除去五一三天,我已经和<JavaScript Dom编程艺术>磨合了六天,第一印象很好.慢慢的,我发现这是一块排骨,除了肉还有骨头.遇到不解的地方就会多看几遍,实在不懂的先跳过,毕竟,初次 ...

  4. 强大的JavaScript动画图形库mo.js

    最近在学习前端动画方面知识时发现了挺有趣的一个动画的图形库mo.js,页面效果真是酷炫,有兴趣的同学可以研究下:). 酷炫的效果: 以下是官方的demo效果,更多详情请查看 mo.js http:// ...

  5. 省市县 三级 四级联动Javascript JQ 插件PCASClass.js

    想要使用这款组件,需要页面引入 PCASClass.js 核心文件,该文件在您的HTML文档<head>标签之内. <script type="text/javascrip ...

  6. 【转】《高级前端3.6》JavaScript多线程——Concurrent.Thread.js, WebWork

    原文链接:http://www.cnblogs.com/woodk/articles/5199536.html JavaScript多线程,在HTML5 WebWork没出现之前很多人都是用Concu ...

  7. JavaScript模板引擎Template.js使用详解

    这篇文章主要为大家详细介绍了JavaScript模板引擎Template.js使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下   template.js 一款 JavaScript 模板引 ...

  8. 坑人的 Javascript 模块化编程 require.js

    坑人的 Javascript 模块化编程 require.js

  9. 坑人的 Javascript 模块化编程 sea.js

    坑人的 Javascript 模块化编程 sea.js 忧伤 加 蛋疼的 开始了 看文档 Sea.js 进行配置 seajs.config({ // 设置路径,方便跨目录调用 paths: { 'ar ...

随机推荐

  1. swift第一季基础语法

    同: 一.基础 同: 1常量和变量 2数据类型和数据类型转换 3别名 不同: 1可选类型optional 2BOOL类型 3元组类型 4断言Assertion 二.基本操作符 同: 1赋值和算术运算及 ...

  2. iOS如何上传代码到Github

    iOS如何上传代码到Github 很多iOS开发者想开源自己的代码或者demo,开源到Github是个不错的选择,那么如何上传我们的代码到Github,令所有人可以下载使用呢?这里我们的目的很明确,就 ...

  3. Fedora 12 环境搭建

    又来折腾发行版了. 这一回是Fedora12,搞的挺艰难的 下载了Fedora-12-i386-DVD.iso,无论使用ultraiso还是dd都无法安装. 后来下载了一个ImageWriter.ex ...

  4. 20145208 《Java程序设计》第8周学习总结

    20145208 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 NIO与IO的区别 IO           NIO 面向流             面向缓冲 阻 ...

  5. ubuntu 安装编译nginx,并实现HLS推送,,可以实现摄像头直播

    1.安装nginx的依赖包  zlib pcre openssl(可以源码安装也可以直接系统安装) sudo apt-get install libpcre3 libpcre3-dev zlib1g- ...

  6. windows API 开发飞机订票系统 图形化界面 (一)

    去年数据结构课程设计的作品,c语言实现,图形化界面使用windows API实现. 首发在我csdn博客:http://blog.csdn.net/u013805360/article/details ...

  7. Bootstrap系列 -- 39. 导航条添加标题

    在Web页面制作中,常常在菜单前面都会有一个标题(文字字号比其它文字稍大一些),其实在Bootstrap框架也为大家做了这方面考虑,其通过“navbar-header”和“navbar-brand”来 ...

  8. 序列化和反序列化的几种方式(DataContractSerializer)(二)

    DataContractSerializer 类 使用提供的数据协定,将类型实例序列化和反序列化为 XML 流或文档. 无法继承此类. 命名空间: System.Runtime.Serializati ...

  9. c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字

    TextBox只允许输入数字,最大长度为10 //TextBox.ShortcutsEnabled为false 禁止右键和Ctrl+v private void txtNumber_KeyPress( ...

  10. 第九章:Java----泛型学习(最后过一遍)

    泛型:让集合记住里面元素的类型,避免取出时需要强制类型转换(大到小).   ClassCastException! 编译阶段就能发现错误.  语法更严格! 更不容易犯错! 1. 构造器的名字还是类名, ...