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. [CareerCup] 5.1 Insert Bits 插入位

    5.1 You are given two 32-bit numbers, N and M, and two bit positions, land j. Write a method to inse ...

  2. 《Linux内核设计与实现》 Chapter4 读书笔记

    <Linux内核设计与实现> Chapter4 读书笔记 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子 ...

  3. 一种仿照Asp.net Mvc思维构建WebSocket服务器的方法

    问题场景 Asp.net Mvc提供了DependencyResolver.Routing.Filter. Modelbinder等webForm所没有新概念,提高Web服务编写的便利性,记得很久之前 ...

  4. Java学习笔记(十八)——Java DTO

    [前面的话] 在和技术人员的交流中,各种专业术语会出现,每次都是默默的记录下出现的术语,然后再去网上查看是什么意思.最近做项目,需要使用到DTO,然后学习一下吧. 这篇文章是关于Java DTO的,选 ...

  5. 《TCP/IP详解卷1:协议》第5章 RARP:逆地址解析协议-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  6. EntityFramework_MVC4中EF5 新手入门教程之五 ---5.通过 Entity Framework 读取相关数据

    在前面的教程中,您完成School数据模型.在本教程中,您会读取和显示相关的数据 — — 那就是,实体框架将加载到导航属性的数据. 下面的插图显示页面,您将完成的工作. 延迟. 预先,和显式加载的相关 ...

  7. jQuery使用之(四)处理页面的表单元素

    表单是一个特殊的页面元素,value的值是最受关注的,jQuery提供了强大的val()方法来处理相关的操作. 1.获取表单元素的值. 直接调用val()方法时可以获取选择器的 中的第一个元素的val ...

  8. js回掉页面后台代码-简单demo

    后台代码: public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler { protected void Pag ...

  9. NLP的两种工具的java版使用:复旦FudanNLP,中科院计算所ICTCLAS2013

    编程语言:java 三种工具的简要介绍: FudanNLP google project上的介绍是: FudanNLP主要是为中文自然语言处理而开发的工具包,也包含为实现这些任务的机器学习算法和数据集 ...

  10. 复习CSS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...