2016-05-02 19 views
-3

Im Folgenden meine Demo ist, ist meine Frage nach Scrollspy und ".sticky" in Bootstrap 4.Über Scrollspy und ".sticky" in Bootstrap 4

1, Über Scrollspy:
I data-offset="20" festlegen möchten Wo soll ich es hinstellen?

2, über ".sticky":
Ich mag die nav id="navbar-example" klebrig sein, wenn sie die Spitze berühren, wie es zu tun? Ich habe versucht, die CSS unten, aber es hat nicht funktioniert.

.sticky { 
      position: -webkit-sticky; 
      position: sticky; 
      top: 15px; 
     } 

Demo:

<!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> 

Antwort

1

So wie für die erste quesiton setzen Sie die Korrekturdaten in dem Body-Tag, wo Sie das scrollspy Plugin wie so nennen:

<body data-spy="scroll" data-target="#navbar-example" data-offset="20"> 

Auch wenn Sie scrolspy durch das body-Tag initiieren, müssen Sie es nicht erneut mit jquery initiieren, damit Sie das Skript entfernen können:

$('body').scrollspy({target: '#navbar-example'}); 

Wie für Ihre zweite Frage haben Sie die richtige CSS, aber Sie müssen dies mit einem Element einfügen. Fügen Sie das also zu .col-md-3 sticky vor Ihrer .nav-pills hinzu. Nur damit Sie wissen, dass css sticky experimentell ist und nicht mit vielen Browsern funktionieren wird, am wichtigsten ist Chrome, schauen Sie sich diesen Artikel an, der Ihnen im Grunde alles gibt, was Sie über sticky zusammen mit Support-Tabellen und einem Polyfill wissen müssen. Sie finden den Artikel hier http://webdesign.tutsplus.com/tutorials/sticky-positioning-with-nothing-but-css--cms-24042. Wenn Sie css sticky verwenden, empfehle ich sehr, dass Sie das polyfill verwenden, es ist wirklich einfach zu verwenden, nur müssen Sie die Javascript-Datei hinzufügen und sie mit jquery aufrufen. Im Folgenden habe ich eine Demo für Sie eingerichtet und einen Link zur Polyfill-CDN eingefügt und den Polyfill mit jquery initiiert.

Hier ist Ihre überarbeitete Demo:

<!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" data-offset="20"> 
<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 sticky"> 
      <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 src="https://cdnjs.cloudflare.com/ajax/libs/stickyfill/1.1.4/stickyfill.min.js"></script> 
<script> 
$('.sticky').Stickyfill(); 
</script> 
</body> 
</html> 

Kopieren Sie diese in und um die Unterschiede zu sehen.

Hoffe, das hilft

+0

Danke eine Million. – zwl1619