鍍金池/ 問答/HTML/ 滾動頁面滾動條,當觸發(fā)錨點時如何控制另一個DIV里的滾動條一起滾動?

滾動頁面滾動條,當觸發(fā)錨點時如何控制另一個DIV里的滾動條一起滾動?

具體描述:

當前頁面兩個滾動條,一個是頁面滾動條,一個是DIV存放錨點的滾動條。需求是,滾動頁面滾動條時,DIV滾動條隨著頁面滾動條滾動?

意思是指,比如,右側(cè)錨點位置在#5,DIV里的錨點鏈接也是在#5這個對應(yīng)的錨點上,并且獲取出當前這個錨點鏈接距離這個存放錨點鏈接的DIV頂部多少距離使其一直處于(滾動于)這個DIV的第二個位置上。

如圖所示:

圖片描述

請問該如何實現(xiàn),先謝謝啦!~

回答
編輯回答
還吻

監(jiān)聽頁面的滾動事件,然后計算,設(shè)置div的滾動高度

2017年4月10日 16:17
編輯回答
心癌
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            #toc{
                height: 250px;
                width: 200px;
                border:1px solid #ddd;
                overflow: auto;
                position: fixed;
                left: 50px;
                top: 50%;
                margin-top: -125px;
            }
            #toc li{
                height: 30px;
                margin: 10px 0px;
                width: 100%;
                padding-left: 20px;
            }
            
            .article-content{
                margin-left: 350px;
            }
            .article-content article{
                height: 800px;
                border: 1px solid #ddd;
                margin: 10px 0;
            }
            li.active{
                position: absolute;
                top: 40px;
                background-color: #f2f5f2;
                color: red;
            }
            h6.active{
                color: red;
            }
            ol{
                padding: 0px;
            }
        </style>
    </head>
    <body>
        <div id="toc" class="toc-article fixed height-255 overflow-auto">
            <ol class="toc">
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="1" href='###'>
                        <span class="toc-number">1.</span> <span class="toc-text">錨點鏈接1</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="2" href='###'>
                        <span class="toc-number">2.</span> <span class="toc-text">錨點鏈接2</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="3" href='###'>
                        <span class="toc-number">3.</span> <span class="toc-text">錨點鏈接3</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="4" href='###'>
                        <span class="toc-number">4.</span> <span class="toc-text">錨點鏈接4</span>
                    </a>
                </li>
                 <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="5" href='###'>
                        <span class="toc-number">5.</span> <span class="toc-text">錨點鏈接5</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="6" href='###'>
                        <span class="toc-number">6.</span> <span class="toc-text">錨點鏈接6</span>
                    </a>
                </li>
              
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="7" href='###'>
                        <span class="toc-number">7.</span> <span class="toc-text">錨點鏈接7</span>
                    </a>
                </li>
                 <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="8" href='###'>
                        <span class="toc-number">8.</span> <span class="toc-text">錨點鏈接8</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="9" href='###'>
                        <span class="toc-number">9.</span> <span class="toc-text">錨點鏈接9</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="10" href='###'>
                        <span class="toc-number">10.</span> <span class="toc-text">錨點鏈接10</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="11" href='###'>
                        <span class="toc-number">11.</span> <span class="toc-text">錨點鏈接11</span>
                    </a>
                </li>
                <li class="toc-item toc-level-6">
                    <a class="toc-link" data-id="12" href='###'>
                        <span class="toc-number">12.</span> <span class="toc-text">錨點鏈接12</span>
                    </a>
                </li>
            </ol>
        </div>

        <div class="article-content">
            <article>
                <h6 id="1">錨點標題位置1</h6>
                <p>內(nèi)容1</p>
            </article>
            <article>
                <h6 id="2">錨點標題位置2</h6>
                <p>內(nèi)容2</p>
            </article>
            <article>
                <h6 id="3">錨點標題位置3</h6>
                <p>內(nèi)容3</p>
            </article>
            <article>
                <h6 id="4">錨點標題位置4</h6>
                <p>內(nèi)容4</p>
            </article>
            <article>
                <h6 id="5">錨點標題位置5</h6>
                <p>內(nèi)容5</p>
            </article>
            <article>
                <h6 id="6">錨點標題位置6</h6>
                <p>內(nèi)容6</p>
            </article>
            <article>
                <h6 id="7">錨點標題位置7</h6>
                <p>內(nèi)容7</p>
            </article>
            <article>
                <h6 id="8">錨點標題位置8</h6>
                <p>內(nèi)容8</p>
            </article>
            <article>
                <h6 id="9">錨點標題位置9</h6>
                <p>內(nèi)容9</p>
            </article>
            <article>
                <h6 id="10">錨點標題位置10</h6>
                <p>內(nèi)容10</p>
            </article>
            <article>
                <h6 id="11">錨點標題位置11</h6>
                <p>內(nèi)容11</p>
            </article>
            <article>
                <h6 id="12">錨點標題位置12</h6>
                <p>內(nèi)容12</p>
            </article>
        </div>
    </body>
    <script src="js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        $(document).ready(function (){
            elevator.init();
            debugger;
            $('#toc').on('click','.toc-link',function(e){
                $(this).parent('li').addClass('active').siblings().removeClass('active');
                $('#toc').scrollTop(0);
                var elemTop=getElementTop($('#'+$(this).attr('data-id')).parent()[0]);
                window.scrollTo(0,elemTop-elevator.UPLEVEL);
                
            });
        });

function getElementTop(elem){
    var elemTop=elem.offsetTop;
    elem=elem.offsetParent;
    while(elem!=null){
        elemTop+=elem.offsetTop;
        elem=elem.offsetParent;
    }
    return elemTop;
}
var elevator={
    FLOORHEIGHT:0,
    UPLEVEL:0,
    DOWNLEVEL:0,
    init:function(){
        var style=getComputedStyle($('article')[0]);
        this.FLOORHEIGHT=
            parseFloat(style.height)+parseFloat(style.marginBottom)
        this.UPLEVEL=(innerHeight-this.FLOORHEIGHT)/2;
        this.DOWNLEVEL=this.UPLEVEL+this.FLOORHEIGHT;
        window.addEventListener('scroll',this.scroll.bind(this));
    },
    scroll:function(){//在頁面滾動時觸發(fā)檢測
        debugger
        var h=$('article h6');
        for(var i=0;i<h.length;i++){
            var elemTop=getElementTop(h[i]);
            var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;

            if(scrollTop>elemTop-this.UPLEVEL){
                h[i].className="";
            }else if(scrollTop>elemTop-this.DOWNLEVEL){
                $(h[i]).addClass('active');
                debugger;
                $('a[data-id="'+($(h[i]).attr('id'))+'"').parent().addClass('active').siblings().removeClass('active');
            }else{
                h[i].className="";
            }
        }
    },
    

}









    </script>
</html>

//這個是以前做的一個項目用到的代碼,在這個基礎(chǔ)上改的,所以有原生的,有jquery的。你試一下效果。

2017年4月4日 23:21