首页 > 技术文章 > js 捕捉滚动条事件

ligenyun 原文

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
    <script src="Content/jquery-1.10.2.min.js"></script>
</head>
<body>
    <div id="content">
        <div id="divLeft" style="float:left;200px;height:300px;">
            <div style="position:fixed">
                <a href="#p1">这里是1</a><br />
                <a href="#p2">这里是2</a><br />
                <a href="#p3">这里是3</a><br />
                <a href="#p4">这里是4</a><br />
                <a href="#p5">这里是5</a><br />
                <a href="#p6">这里是6</a><br />
                <a href="#p7">这里是7</a><br />
                <a href="#p8">这里是8</a><br />
                <a href="#p9">这里是9</a><br />
                <a href="#p10">这里是10</a>
            </div>                      
        </div>
        <div id="divRight" style="float:left;">
            <div id="p1" style="height:300px;100%;border:1px solid red;">这里是1</div>
            <div id="p2" style="height:300px;100%;border:1px solid red;">这里是2</div>
            <div id="p3" style="height:300px;100%;border:1px solid red;">这里是3</div>
            <div id="p4" style="height:300px;100%;border:1px solid red;">这里是4</div>
            <div id="p5" style="height:300px;100%;border:1px solid red;">这里是5</div>
            <div id="p6" style="height:300px;100%;border:1px solid red;">这里是6</div>
            <div id="p7" style="height:300px;100%;border:1px solid red;">这里是7</div>
            <div id="p8" style="height:300px;100%;border:1px solid red;">这里是8</div>
            <div id="p9" style="height:300px;100%;border:1px solid red;">这里是9</div>
            <div id="p10" style="height:300px;100%;border:1px solid red;">这里是10</div>
        </div>
    </div>
    <script>
        function ChangeColor(index) {
             
            var aArray = $('#divLeft a');
            for (var i = 0; i < aArray.length; i++) {
                if (i == parseInt(index)) {
                    aArray.eq(i).css('color','red')
                } else {
                    aArray.eq(i).css('color', 'black')
                }
                
            }

        }
        //function scrollFunc(e) {
        //    e = e || window.event;
        //    if (e.wheelDelta) {  //第一步:先判断浏览器IE,谷歌滑轮事件               
        //        if (e.wheelDelta > 0) { //当滑轮向上滚动时  
        //            console.log("滑轮向上滚动");
        //        }
        //        if (e.wheelDelta < 0) { //当滑轮向下滚动时  
        //            console.log("滑轮向下滚动");
        //        }
        //    } else if (e.detail) {  //Firefox滑轮事件  
        //        if (e.detail > 0) { //当滑轮向上滚动时  
        //            console.log("滑轮向上滚动");

        //            var t = window.pageYOffset;
        //            console.log(t);
        //        }
        //        if (e.detail < 0) { //当滑轮向下滚动时  
        //            console.log("滑轮向下滚动");
        //        }
        //    }
        //}

        ////给页面绑定滑轮滚动事件  
        //if (document.addEventListener) {//firefox  
        //    document.addEventListener('DOMMouseScroll', scrollFunc, false);
        //}


        //window.onmousewheel = document.onmousewheel = scrollFunc;


        function getScrollTop() {
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }

            var t = scrollTop / 300;
           
            ChangeColor(t);

            return scrollTop;
        }
        document.onscroll = function () {
            getScrollTop()
        }
    </script>
</body>
</html>

 

天生我材必有用,千金散尽还复来

推荐阅读