Bootstrap 4 设置滚动监听问题

下面是demo,有两个问题:
1、我想让左边的导航菜单接触到顶部的时候,就保持固定不动,bootstrap 4的文档中说,用html5的positon:sticky,应该怎么写呢,下面写的试用了一下,不起作用,不知道.sticky这个css该用在哪里。

2、我想让右边内容滚动距离顶部还有一定距离的时候,比如20px,左边导航菜单就相应变化active,好像应该设置一个data-offset="20",应该设置在哪个元素上呢?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether-theme-basic.min.css" rel="stylesheet">
    <style>
        body {
            position: relative;
        }

        .sticky {
            position: -webkit-sticky;
            position: sticky;
            top: 15px;
        }
    </style>
</head>
<body data-spy="scroll" data-target="#navbar-example">
<div class="container">
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
</div>
<div class="container">
    <div class="row">
        <div class="col-md-3">
            <ul class="nav nav-pills nav-stacked" id="navbar-example">
                <li class="nav-item">
                    <a class="nav-link" href="#article1">Link1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#article2">Link2</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#article3">Link3</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#article4">Link4</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#article5">Link5</a>
                </li>
            </ul>
        </div>

        <div class="col-md-9">
            <h4 id="article1">Article1</h4>
            <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
                Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's
                photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you
                probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil
                tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
            <h4 id="article2">Article2</h4>
            <p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua
                cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater
                food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic
                exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.</p>
            <h4 id="article3">Article3</h4>
            <p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing
                banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl,
                echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim
                messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.</p>
            <h4 id="article4">Article4</h4>
            <p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente
                veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee
                wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't
                heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial
                keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko.
                Locavore enim nostrud mlkshk brooklyn nesciunt.</p>
            <h4 id="article5">Article5</h4>
            <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
                Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's
                photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you
                probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil
                tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
            <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda.
                Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie
                gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident
                brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan
                readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
            </p>
            <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
                Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's
                photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you
                probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil
                tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
            <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda.
                Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie
                gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident
                brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan
                readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
            </p>
            <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui.
                Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's
                photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you
                probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil
                tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
            <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda.
                Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie
                gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident
                brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan
                readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
            </p>
        </div>
    </div>
</div>

<!-- JavaScripts -->
<script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.2.0/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/holder/2.9.3/holder.min.js"></script>
<script>
        $('body').scrollspy({target: '#navbar-example'});
</script>
</body>
</html>
阅读 4.7k
1 个回答

这是一个新的css3属性,可能浏览器不兼容吧,你完全可以用js模拟实现,demo如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />    
    <style>
        .sticky{
            position:fixed;
            top:0;
        }
        .header{
            width:100%;
            background:#F6D565;
            padding:25px 0;
        }
        .content{
            width:100%;
            height:200px;
            margin:25px 0;
            background-color: #ccc;
        }
    </style>
</head>
<body>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="header">nav内容</div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>


<script type="text/javascript">
    var header = document.querySelector('.header');
    var origOffsetY = header.offsetTop;
    function onScroll(e){
        window.scrollY >= origOffsetY ? header.classList.add('sticky'):header.classList.remove('sticky');
    }
    document.addEventListener('scroll', onScroll);
</script>
</body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进