js 捕捉滚动条事件


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;width:200px;height:300px;">
            <div style="position:fixed">
                <a href="#p1">这里是1a><br />
                <a href="#p2">这里是2a><br />
                <a href="#p3">这里是3a><br />
                <a href="#p4">这里是4a><br />
                <a href="#p5">这里是5a><br />
                <a href="#p6">这里是6a><br />
                <a href="#p7">这里是7a><br />
                <a href="#p8">这里是8a><br />
                <a href="#p9">这里是9a><br />
                <a href="#p10">这里是10a>
            div>                      
        div>
        <div id="divRight" style="float:left;">
            <div id="p1" style="height:300px;width:100%;border:1px solid red;">这里是1div>
            <div id="p2" style="height:300px;width:100%;border:1px solid red;">这里是2div>
            <div id="p3" style="height:300px;width:100%;border:1px solid red;">这里是3div>
            <div id="p4" style="height:300px;width:100%;border:1px solid red;">这里是4div>
            <div id="p5" style="height:300px;width:100%;border:1px solid red;">这里是5div>
            <div id="p6" style="height:300px;width:100%;border:1px solid red;">这里是6div>
            <div id="p7" style="height:300px;width:100%;border:1px solid red;">这里是7div>
            <div id="p8" style="height:300px;width:100%;border:1px solid red;">这里是8div>
            <div id="p9" style="height:300px;width:100%;border:1px solid red;">这里是9div>
            <div id="p10" style="height:300px;width:100%;border:1px solid red;">这里是10div>
        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>