原生js实现滚动条

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生js实现滚动条相关的知识,希望对你有一定的参考价值。

var SimulateScroll = (function(){
    var oParent = document.getElementById(‘wrap-scroll-bar‘),
        oBox = document.getElementById(‘scroll-bar‘),
        oWp = document.getElementById(‘container‘),
        oDiv = document.getElementById(‘cont‘),
        bDown = true,      
        downFun = function(ev){
            var oEv = ev || event;
            var disY = oEv.clientY - oBox.offsetTop;
            document.onmousemove = function(ev){
                var oEv = ev || event;
                var l = oEv.clientY - disY;
                setTop(l);
            };

            document.onmouseup = function(){
                document.onmousemove = null;
                document.onmouseup = null;
            };

            return false;
        };

        function mouseWheel(ev){

            var oEv = ev || event;

            bDown = oEv.wheelDelta ? oEv.wheelDelta < 0 : oEv.detail > 0;

            if(bDown){
                setTop(oBox.offsetTop + 10);
            }else{
                setTop(oBox.offsetTop - 10);
            }

            if(oEv.preventDefault){
                oEv.preventDefault();
            }
            return false;
        }

        function setTop(l){

            var h = oParent.offsetHeight - oBox.offsetHeight;

            if(l < 0){
                l = 0;
            }else if(l > h){
                l = h;
            }

            oBox.style.top = l + ‘px‘;

            var bl = l/h;

            oDiv.style.top =- (oDiv.offsetHeight - oWp.offsetHeight) * bl + ‘px‘;
        }

        function setBarHeight(){
            var containerHeight = oWp.offsetHeight,
                contentHeight = oDiv.offsetHeight;
            oBox.style.height = (containerHeight * containerHeight /contentHeight) + ‘px‘;
        }

        function addEvent(obj, sEv, fn){
            if(obj.addEventListener){
                obj.addEventListener(sEv,fn,false);
            }else{
                obj.attachEvent(‘on‘ + sEv,fn);
            }
        }

        return {
            oWp : oWp,
            oDiv : oDiv,
            scrollHidden : function(){
                oBox.style.height = 0;
                oBox.style.top = 0;
                oDiv.style.top = 0;
                oBox.onmousedown = null;
                query.EventUtil.remove(oParent, ‘mousewheel‘, mouseWheel);
                query.EventUtil.remove(oParent, ‘DOMMouseScroll‘, mouseWheel);
                query.EventUtil.remove(oWp, ‘mousewheel‘, mouseWheel);
                query.EventUtil.remove(oWp, ‘DOMMouseScroll‘, mouseWheel);
            },
            isScrollShow : function(){

                if(oDiv.offsetHeight > oWp.offsetHeight){

                    SimulateScroll.inIt();

                }else{

                    SimulateScroll.scrollHidden();

                }
            },
            inIt : function(){
                setBarHeight();
                oBox.onmousedown = downFun;
                query.EventUtil.add(oParent, ‘mousewheel‘, mouseWheel);
                query.EventUtil.add(oParent, ‘DOMMouseScroll‘, mouseWheel);
                query.EventUtil.add(oWp, ‘mousewheel‘, mouseWheel);
                query.EventUtil.add(oWp, ‘DOMMouseScroll‘, mouseWheel);
            }
        }
})();

SimulateScroll.isScrollShow();

query.EventUtil.add(window,‘resize‘,SimulateScroll.isScrollShow);

//query.EventUtil 为原生js事件库。如需使用以上滚动条方法,需加上事件库,或者改为jq写法

 html结构:

技术分享

以上是关于原生js实现滚动条的主要内容,如果未能解决你的问题,请参考以下文章

原生js实现滚动条

js原生 + jQuery实现页面滚动字幕

怎么能实现div里的滚动条滚动时有动画效果

原生js实现使滚动条缓慢回到顶部的效果

原生js禁止页面滚动

原生js实现上拉加载