js 自制滚动条的小例子_javascript技巧

写了个js自制滚动条,首先,先看一下demo(点击这里) 先

有两个demo,右边那个黑色那个,是我一开始写的比较肤浅的代码:

复制代码 代码如下:

var scrollself=(function(){

    var scrollblock, //滚动块
        scrollcontent,  //被滚动的内容
        scrollbar,    //滚动条
        scrollpanel,    //滚动内容的滚动区域
        cdistance,  //滚动内容要滚动的距离
        bdistance,    //滚动块要滚动的距离
        minuTop, //滚动条头尾剩下的空白
        cTop,    //滚动内容的top
        startY=0,    //滚动动作开始初鼠标的位置
        bTop=0,    //滚动动作开始初滚动块的top
        isDrag=false;  //是否拉动滚动块

    function prevent(e){

        if(e.preventDefault){
            e.preventDefault();
        }
        if(e.stopPropagation){
            e.stopPropagation();
        }
        e.cancelBubble=true;
        e.returnValue=false;
    }

    function mouseDown(event){
        isDrag=true;
        event=event||window.event;
        startY=event.clientY;
        bTop=scrollblock.offsetTop;
        cTop=scrollcontent.offsetTop;

        // prevent(event);

    }

    function mouseMove(event){
        if(isDrag){

            event=event||window.event;

            var newbTop=event.clientY-startY+bTop,
                newcTop=cTop-(event.clientY-startY)/bdistance*cdistance;

            if(newbTop<minuTop){
                newbTop=minuTop;
                newcTop=0;
            }else{
                if(newbTop>bdistance+minuTop){
                    newcTop=-cdistance;
                    newbTop=bdistance+minuTop;
                }
            }

            scrollblock.style.top=newbTop+'px';
            scrollcontent.style.top=newcTop+'px';
        }else{
            isDrag=false;
        }

        // prevent(event);
    }

    function mouseUp(event){

        isDrag=false;

        // prevent(event);
    }

    function addHandler(){
        scrollblock.onmousedown=mouseDown;
        scrollblock.onmousemove=mouseMove;
        scrollblock.onmouseup=mouseUp;
        document.onmouseup=mouseUp;
    }

    return{
        init:function(scrollpanel_id,scrollcontent_id,scrollbar_id,scrollblock_id){
            scrollblock=document.getElementById(scrollblock_id);
            scrollcontent=document.getElementById(scrollcontent_id);
            scrollbar=document.getElementById(scrollbar_id);
            scrollpanel=document.getElementById(scrollpanel_id);
            minuTop=scrollblock.offsetTop;
            cdistance=scrollcontent.offsetHeight-scrollpanel.offsetHeight;
            bdistance=scrollbar.offsetHeight-minuTop*2-scrollblock.offsetHeight;

            enclose(scrollpanel,scrollcontent,scrollbar,scrollblock,bdistance,cdistance,minuTop);
            addHandler();
        }
    }

}());

scrollself.init('scrollpanel2','scrollcontent2','scrollbar2','scrollblock2');

之所以说它肤浅,比较一下两个demo的滚动效果就知道了,右边的拉动滚动块时候,体验效果好差,很卡,而左边的就流畅多了。

因为很卡,我就又上网看了一下别人的代码,然后把根据别人的思路把代码改了一下,就有了左边那个绿色的那个demo,很明显,效果好了很多,代码:

复制代码 代码如下:

var scroll=(function(){

    var scrollblock, //滚动块
        scrollcontent,  //被滚动的内容
        scrollbar,    //滚动条
        scrollpanel,    //滚动内容的滚动区域
        cdistance,  //滚动内容要滚动的距离
        bdistance,    //滚动块要滚动的距离
        minuTop, //滚动条头尾剩下的空白
        cTop,    //滚动内容的top
        startY=0,    //滚动动作开始初鼠标的位置
        bTop=0;    //滚动动作开始初滚动块的top

    function mouseDown(event){
        event=event||window.event;
        startY=event.clientY;
        bTop=scrollblock.offsetTop;
        cTop=scrollcontent.offsetTop;
        if(scrollblock.setCapture){

            scrollblock.onmousemove=doDrag;
            scrollblock.onmouseup=stopDrag;
            scrollblock.setCapture();
        }else{
            document.addEventListener("mousemove",doDrag,true);
            document.addEventListener("mouseup",stopDrag,true);
        }

    }

    function doDrag(event){
        event=event||window.event;

        var newbTop=event.clientY-startY+bTop,
            newcTop=cTop-(event.clientY-startY)/bdistance*cdistance;

        if(newbTop<minuTop){
            newbTop=minuTop;
            newcTop=0;
        }else if(newbTop>bdistance+minuTop){
            newcTop=-cdistance;
            newbTop=bdistance+minuTop;
        }
        scrollblock.style.top=newbTop+'px';
        scrollcontent.style.top=newcTop+'px';

    }

    function stopDrag(event){
        if(scrollblock.releaseCapture){

            scrollblock.onmousemove=doDrag;
            scrollblock.onmouseup=stopDrag;
            scrollblock.releaseCapture();
        }else{
            document.removeEventListener("mousemove",doDrag,true);
            document.removeEventListener("mouseup",stopDrag,true);
        }
        scrollblock.onmousemove=null;
        scrollblock.onmouseup=null;
    }
    return{
        init:function(scrollpanel_id,scrollcontent_id,scrollbar_id,scrollblock_id){
            scrollblock=document.getElementById(scrollblock_id);
            scrollcontent=document.getElementById(scrollcontent_id);
            scrollbar=document.getElementById(scrollbar_id);
            scrollpanel=document.getElementById(scrollpanel_id);
            minuTop=scrollblock.offsetTop;
            cdistance=scrollcontent.offsetHeight-scrollpanel.offsetHeight;
            bdistance=scrollbar.offsetHeight-minuTop*2-scrollblock.offsetHeight;
            scrollblock.onmousedown=mouseDown;
            enclose(scrollpanel,scrollcontent,scrollbar,scrollblock,bdistance,cdistance,minuTop);
        }
    }

}());

scroll.init('scrollpanel','scrollcontent','scrollbar','scrollblock');

比较了一下两个的代码,其实修改的不多,就有一点很大的不同,流畅的那一个(左边绿色那一个)多了这个东西——setCapture、releaseCapture。

具体是怎样的,再研究一下先。

时间: 2024-10-25 20:58:22

js 自制滚动条的小例子_javascript技巧的相关文章

JS求平均值的小例子_javascript技巧

效果: 事件:当输入完三个文本框后触发blur事件.计算出平均值 说明:调用方法计算出平均值, 代码: 文本框中: 复制代码 代码如下: <asp:TextBox ID="tblmyd11" Field="lmyd11" TableName="gqls_sy" EnabledAction="(12.*)"  CssClass="erp_textbox_short" runat="server

js跟随滚动条滚动浮动代码_javascript技巧

复制代码 代码如下: var str="客服"; var objFT=new FloatT("FloatMenu1",str,942,137,125,200,80,10); objFT.FloatRun(); //js跟随滚动条滚动 浮动 //pObjName:html 元素ID //pText:html 元素内容 //其它自己看 function FloatT(pObjName,pText,pstmnLEFT,pstmnGAP1,pstmnGAP2,pstmnBA

js获取指定日期周数以及星期几的小例子_javascript技巧

JS获取日期时遇到如下需求,根据某年某周获取一周的日期.如开始日期规定为星期四到下一周的星期五为一周. 代码如下: function getNowFormatDate(theDate) { var day = theDate; var Year = 0; var Month = 0; var Day = 0; var CurrentDate = ""; // 初始化时间 Year= day.getFullYear();// ie火狐下都可以 Month= day.getMonth()+

js获取当前月的第一天和最后一天的小例子_javascript技巧

引入XDate.JS 这个小插件,不知道的百度谷歌吧 代码如下: 复制代码 代码如下: var firstDate = new Date(); firstDate.setDate(1); //第一天 var endDate = new Date(firstDate); endDate.setMonth(firstDate.getMonth()+1); endDate.setDate(0); alert("第一天:"+new XDate(firstDate).toString('yyyy

js实现点击注册按钮开始读秒倒计时的小例子_javascript技巧

js 复制代码 代码如下: <script language="javascript"> <!-- var wait_time = 8; //设置秒数(单位秒) var secs_time = 0;          for(var i=1;i<=wait_time;i++) {  window.setTimeout("sTimer("+i+")",i*1000); } function sTimer(num) {  if

js遍历、动态的添加数据的小例子_javascript技巧

复制代码 代码如下: function jsonObj(){     var person= {name: 'zhangsan',pass: '123' ,'sni.ni' : 'XMQ',back:function (){         for(var i=0;i<arguments.length;i++){         //在不知参数个数情况下可通过for循环遍历         // arguments这个是js 默认提供         alert("arr["+i

JS在textarea光标处插入文本的小例子_javascript技巧

下面是代码: 复制代码 代码如下: --------------------------------------------------------------------------------<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html x

js 验证密码强弱的小例子_javascript技巧

页面代码: 复制代码 代码如下: <table>        <tr><td><input type="text" id="txtPwd" /></td></tr>        <tr><td>            <table id="pwdLever">                <tr>             

js动态创建表格,删除行列的小例子_javascript技巧

复制代码 代码如下: <!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><meta http-equiv="