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. jsp中常用操作字符串的el表达式

    由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 准备工作:1)导入jar包:standard.jar和jstl.jar2) ...

  2. display:inline-block的坑

    一直用display:inline-block做某种导航栏还很爽,突然有一个柱状图的需求便也这么做了,于是成功被坑. 简简单单个需求,大致这样 只用几个li加上display:inline-block ...

  3. jQuery问题:$XXX is not a function

    用火狐浏览器打开,js代码一段不执行,F12以后看见下面的错误: 网上查看说是jQuery文件引用的问题,把jQuery.js引入语句修改了一下,果然没有错了. 我原来的引用语句是:<scrip ...

  4. Linux下安装项目管理工具Redmine

    http://www.redmine.org.cn/download Linux下安装项目管理工具Redmine1.Ruby安装Ruby on Rails网站推荐使用1.8.7版. 点击(此处)折叠或 ...

  5. java操作word

    一个使用Apache POI写word文档的实例: 1 package apache.poi; 2 3 import java.io.ByteArrayInputStream; 4 import ja ...

  6. MVC学习Day02之校验

    MVC校验有两种方法: 方法一:自己动手写js---------略 方法二: l在View的页面中,首先指定页面强类型@model 类型 l使用Html.***For(model=>model. ...

  7. 转:浅谈CSS在前端优化中一些值得注意的关键点

    前端优化工作中要考虑的元素多种多样,而合理地使用CSS脚本可以在很大程度上优化页面的加载性能,以下我们就来浅谈CSS在前端优化中一些值得注意的关键点: 当谈到Web的“高性能”时,很多人想到的是页面加 ...

  8. Hibernate-清理一级缓存

    Session执行一些sql语句把内存中的对象的状态同步到数据库,这个过程被称为session清理. 在默认情况下,Session会在下面的时间点清理缓存. 1 当应用程序调用net.sf.hiber ...

  9. Spring AOP详解 、 JDK动态代理、CGLib动态代理

    AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...

  10. UTL_FILE详解

    包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...