美文网首页
原生js写滚动条

原生js写滚动条

作者: Vivian_06e6 | 来源:发表于2018-10-13 16:41 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Scroll</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
        font-size: 18px;
    }

    /* body{
        overflow: hidden;
    } */

    .container {
        position: relative;
        width: 380px;
        height: 400px;
        margin: 100px auto;
        border: 1px solid black;
        overflow: hidden;
    }

    ul {

        position: absolute;
        right: 0;
        top: 0;
        width: 28px;
        height: 398px;
        border: 1px solid #cccccc;
    }

    .contants {
        position: absolute;
        left: 0;
        top: 0;
        width: 350px;
        background: #cccccc;


    }

    .ico {
        width: 28px;
        height: 28px;
        background: #cccccc;
    }
    .ico img{
        width: 100%;
        height: 100%;
    }
    .duration {
        position: relative;
        width: 100%;
        height: 342px;
    }

    .drag {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 0;
        background: black;
    }
</style>

<body>
    <div class="container">
        <div class="contants">渡一教育,渡一教育,,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
            ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育 ,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育,渡一教育
        </div>
        <ul>
            <li class="ico up"><img src="up.png" alt=""></li>
            <li class="duration">
                <div class="drag"></div>
            </li>
            <li class="ico down"><img src="down.png" alt="">
            </li>
        </ul>
    </div>
    <script>
        (function () {
            var drag = document.getElementsByClassName('drag')[0],
                container = document.getElementsByClassName('container')[0],
                duration = document.getElementsByClassName('duration')[0],
                contants = document.getElementsByClassName('contants')[0],
                containerHeight = document.getElementsByClassName('container')[0].offsetHeight,
                contantsHeight = document.getElementsByClassName('contants')[0].offsetHeight;
            var drageHeight = Math.floor(containerHeight / contantsHeight * duration.offsetHeight);
            drag.style.height = drageHeight + 'px';
            init();
            function init() {

                starDrag(drag, duration);
                scrollWheen(drag, container);
                btnScoll(drag)
            }
            function starDrag(item, duration) {//拖拽滑动条

                addEvent(item, 'mousedown', function (e) {
                    e = e || window.event;
                    var topY = e.pageY;
                    document.onmousemove = function (e) {
                        e = e || window.event;
                        var chay = e.pageY - topY;
                        //console.log(chay);
                        if (chay > 0) {
                            // console.log('往下走') ;
                            item.style.top = chay + item.offsetTop + 'px';
                            if ((chay + item.offsetHeight + item.offsetTop) > duration.offsetHeight) {
                                item.style.top = duration.offsetHeight - item.offsetHeight + 'px';
                            }
                        } else {
                            //console.log('往上走');
                            item.style.top = chay + item.offsetTop + 'px';
                            if ((chay + item.offsetTop) <= 0) {
                                item.style.top = 0 + 'px';
                            }
                        }
                        topY = e.pageY;
                        gunScoll(item);
                    }

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

                })

            }


            function scrollWheen(item, box) {//滑轮滚动
                var t = 0;
                addEvent(box, 'mousewheel', fnwheel)
                function fnwheel(e) {
                    // console.log(e.wheelDeltaY);
                    e.preventDefault();
                    if (e.wheelDeltaY > 0) {
                        // console.log('往下走') ;
                        item.style.top = item.offsetTop + 10 + 'px';
                        if ((item.offsetHeight + item.offsetTop) > duration.offsetHeight) {
                            item.style.top = duration.offsetHeight - item.offsetHeight + 'px';
                        }
                    } else {
                        //console.log('往上走');
                        item.style.top = -10 + item.offsetTop + 'px';
                        if ((item.offsetTop) <= 0) {
                            item.style.top = 0 + 'px';
                        }
                    }

                    gunScoll(item);
                }

            }


            function gunScoll(item) {
                var scale = item.offsetTop / (duration.offsetHeight - item.offsetHeight);
                var scollContents = scale * (contantsHeight - containerHeight);
                // console.log(contantsHeight-contantsHeight);
                contants.style.top = -scollContents + 'px';
            }

            //点击上下按钮
            function btnScoll(item) {
                var btnUl = document.getElementsByTagName('ul')[0];
                addEvent(btnUl, 'click', function (e) {
                    // console.log(e.target.parentNode.className);
                    if (e.target.parentNode.className == 'ico up') {
                        console.log('上');
                        item.style.top = -10 + item.offsetTop + 'px';
                        if ((item.offsetTop) <= 0) {
                            item.style.top = 0 + 'px';
                        }

                    } else if (e.target.parentNode.className == 'ico down') {
                        console.log('下');
                        item.style.top = item.offsetTop + 10 + 'px';
                        if ((item.offsetHeight + item.offsetTop) > duration.offsetHeight) {
                            item.style.top = duration.offsetHeight - item.offsetHeight + 'px';
                        }

                    }
                    gunScoll(item);
                })


            }


            //兼容添加事件库
            function addEvent(obj, type, fn, bool) {
                bool = bool || false;//默认冒泡
                //一般情况
                if (obj.addEventListener) {
                    obj.addEventListener(type, handle, bool);
                    //火狐下的滚轮事件
                    if (type == 'mousewheel') {
                        obj.addEventListener('DOMMouseScroll', handle, bool);
                    }
                } else {
                    obj.attachEvent('on' + type, handle);
                }
                //兼容函数
                function handle(e) {
                    e = e || window.event;//event兼容
                    e.wheel = e.wheelDelta || e.detail * -40;//滚轮方向兼容;
                    fn.call(obj, e); //统一this指向
                    e.preventDefault ? e.preventDefault() : e.returnValue = false;//阻止默认;
                }

            }
            //兼容移除事件库
            function removeEvent(obj, type, fn, bool) {
                bool = bool || false;//默认冒泡
                //一般情况
                if (obj.removeEventListener) {
                    obj.removeEventListener(type, fn, bool);
                    // alert('yichu')
                    //火狐下的滚轮事件
                    if (type == 'mousewheel') {
                        obj.removeEventListener('DOMMouseScroll', fn, bool);
                    }
                } else {
                    obj.detachEvent('on' + type, fn);
                }

            }


        })()


    </script>
</body>

</html>
up.png down.png

相关文章

  • 原生js写滚动条

  • JS原生监听滚动条

    一.情景 需求:页面中间某个部分能随着鼠标滚动而滚动,且不显示滚动条。 二.思路 如果只是考虑webkit内核那就...

  • 00-jQuery初识

    什么是jQuery jQuery是一个JavaScript库 jQuery可以简化原生JS的操作 对比原生JS,写...

  • 关于WebView

    JS与Android原生交互 首先,webview应该先支持JS脚本 其次,写一个JS交互的接口,用来调用原生的方...

  • 滚动条的浮现与消失 -- scroll & mousewheel

    需求来源 在滚动区域中滚动条在滚动的时候展示,非滚动的时候消失 (overflow-y ) js原生相关事件 sc...

  • 原生js模拟滚动条插件

    最近在做一个模仿微信在线聊天的demo,页面需要用到滚动条,可是不喜欢原生的,干脆就用面向对象的方式自己写了一个,...

  • 滚动条控制操作

    通过JS代码定义滚动条高度,然后通过execute_script()执行JS代码实现滚动条操作。 案例:打开我要自...

  • PC端滚动条滚动

    JS控制滚动条的位置: 竖向滚动条置顶: 竖向滚动条置底: 缓慢的滚动到底部

  • 滚动加载的瀑布流效果

    需要jq的支持哦 也可以自己写原生js

  • 原生JS写AJAX

    AJAX MDN_AJAX 手写AJAX的主要四个步骤:1.创建XMLHttpReauest2.处理响应(指定响应...

网友评论

      本文标题:原生js写滚动条

      本文链接:https://www.haomeiwen.com/subject/hdgunftx.html