Bootstrap-Affix-Plugin

Der Bootstrap Affix ist eine Navigation, die an den Inhalt derselben Webseite angehängt ist. Mit diesem Plugin können wir ein fest positioniertes Element erstellen und dessen Fixierung ein- und ausschalten. Es kann verwendet werden, um zu einem bestimmten Abschnitt im Dokument zu springen, und es zeigt an, welchen Teil des Inhalts der Benutzer betrachtet. Wir können das Affix-Plugin für eine große Datenmenge verwenden und die Bildlaufleiste ist Teil der Daten, die die Seite anzeigen.

Das Affix-Plugin schaltet zwischen den folgenden drei Klassen um:

  • .affix-top
  • .Affix
  • .affix-bottom

lesen Sie auch:

  • Bootstrap-Setup
  • Bootstrap-Typografie
  • Der .affix-top Die Klasse gibt das Element an seiner obersten Position an und es ist keine CSS-Positionierung erforderlich.
  • Der .Affix Die Klasse legt die Position fest, wodurch die Symbolleiste neben dem Element angebracht wird.
  • Der .affix-bottom Die Klasse gibt das Element an seiner untersten Position und CSS-Positionierung an Fest wird eingesetzt.
  • Die entsprechende Top- oder Bottom-Eigenschaft ist erforderlich, um die Position des Affix-Elements im Ansichtsfenster anzugeben.
  • Die folgenden Beispiele veranschaulichen die Verwendung verschiedener Techniken zur Implementierung des Affixes Bootstrap. Wenn Sie Fragen dazu haben Bootstrap-Affix-Pluginschreiben Sie es bitte in den Kommentarbereich.

    Bootstrap-Affix über Datenattribute

    Wir können ganz einfach ein Affix-Verhalten für jedes Element erstellen, indem wir es einfach hinzufügen data-affix=“affix“ auf das Element, das ausspioniert werden soll. Der Daten-Offset Attribute geben an, um wie viele Pixel gescrollt werden muss, um die Position eines Elements umzuschalten. Wir nutzen die Attribute Spitze Und unten Damit das angeheftete Element seine Position im Ansichtsfenster festlegt. Wenn die Affix-Klasse aktiv ist, wird die Position auf gesetzt Fest.
    Das Folgende ist ein Beispiel:

    <!DOCTYPE html>
    <head>
    <title>Bootstrap Example</title>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div  >
    <h2>Enable Affix via Data Attributes</h2>
       <div  >
             <h2>Bootstrap Affix Plugin Demo</h2>
        </div>
       <div id="my-demo" data-spy="affix" data-offset-top="60"
          data-offset-bottom="200">
          <div  >
             <ul   data-spy="affix" data-offset-top="125">
                <li  ><a href="#myval">HTML</a></li>
                <li><a href="#myval1">JavaScript</a></li>
                <li><a href="#myval2">JQuery</a></li>
             </ul>
          </div>
          <div  >
    	  <h2 id="myval">HTML</h2>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    	     HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
    
          <h2 id="myval1">JavaScript</h2>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    		JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript. The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <h2 id="myval2">JQuery</h2>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
         </div>
       </div>
    </div>
    </div>
    </body>
    </html>
    
    • Führen Sie Bootstrap Affix über Datenattribute aus – Beispiel

    Beispiel für Bootstrap-Affix über Datenattribute

    Bootstrap-Affix über JavaScript

    Wir können das Affix-Plugin manuell aktivieren, indem wir verwenden Affix() Methode zusammen mit der ID oder dem Klassenselektor im Javascript-Code, wie im folgenden Beispiel gezeigt:

    <!DOCTYPE html>
    <head>
    <title>Bootstrap Example</title>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script>
    $(document).ready(function(){
       $("#myval").affix({
          offset:{
             top:100
          }
       });
    });
    </script>
    </head>
    <body>
    <div  >
    <h2>Enable Affix via JavaScript</h2>
       <div  >
             <h2>Bootstrap Affix Plugin Demo</h2>
        </div>
       <div  >
             <ul   id="myval">
                <li  ><a href="#myval">HTML</a></li>
                <li><a href="#myval1">JavaScript</a></li>
                <li><a href="#myval2">JQuery</a></li>
             </ul>
          </div>
          <div  >
    	  <h2 id="myval">HTML</h2>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    	     HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
          <h2 id="myval1">JavaScript</h2>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    		JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    		JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <h2 id="myval2">JQuery</h2>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
         </div>
       </div>
    </div>
    </div>
    </body>
    </html>
    

    Das obige Beispiel ähnelt dem vorherigen Beispiel, der einzige kleine Unterschied besteht darin, dass wir hier Javascript-Code anstelle von Datenattributen verwenden. Der Code verwendet die Methode affix(), die wiederum das Attribut offset verwendet, um anzugeben, um wie viele Pixel gescrollt werden muss, um die Position eines Elements umzuschalten.

    • Führen Sie Bootstrap Affix über ein JavaScript-Beispiel aus

    Bootstrap-Affix über JavaScript-Beispiel

    Bootstrap-Affix-Optionen

    Es gibt bestimmte Optionen, die mit dem Bootstrap-Affix-Plugin verwendet werden können, wie in der folgenden Tabelle dargestellt:

    Name Typ Standardwert Beschreibung

    versetztNummer |
    Funktion |
    Objekt
    10Es berechnet die Position des Bildlaufs, wenn es die Anzahl der zu versetzenden Pixel angibt. Wenn es eine einzelne Zahl gibt, wird diese sowohl oben als auch unten angewendet. Wir bieten einen Objektversatz wie offset: {top:100, bottom:150}.
    ZielSelektor |
    Knoten |
    jQuery-Element
    das FensterobjektEs definiert das Zielelement des Affixes.

    Bootstrap-Affix-Ereignisse

    Es gibt einige Ereignisse, die mit der Affix-Klasse von Bootstrap verwendet werden, wie in der folgenden Tabelle gezeigt:

    Eventbeschreibung

    affix.bs.affixEs wird ausgelöst, bevor das Element angebracht wurde.
    angebracht.bs.affixEs wird ausgelöst, nachdem das Element angebracht wurde.
    affix-top.bs.affixEs wird ausgelöst, bevor das Element oben angebracht wurde.
    angebracht.bs.affixEs wird ausgelöst, nachdem das Element oben angebracht wurde.
    affix-bottom.bs.affixEs wird ausgelöst, bevor das Element unten angebracht wurde.
    affixed-bottom.bs.affixEs wird ausgelöst, nachdem das Element unten angebracht wurde.

    Das Folgende ist ein Beispiel:

    <!DOCTYPE html>
    <head>
    <title>Bootstrap Example</title>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script>
    $(document).ready(function(){
       $("#myval").affix({
          offset:{
             top:100
          }
       });
       $("#myval").on('affix-top.bs.affix',function(){
          alert("The navigation has been affixed...doesn't scroll with page!!!");
       });
    });
    </script>
    </head>
    <body>
    <div  >
    <h2>Enable Affix via JavaScript</h2>
       <div  >
             <h2>Bootstrap Affix Plugin Demo</h2>
        </div>
       <div  >
             <ul   id="myval">
                <li  ><a href="#myval">HTML</a></li>
                <li><a href="#myval1">JavaScript</a></li>
                <li><a href="#myval2">JQuery</a></li>
             </ul>
          </div>
          <div  >
    	  <h2 id="myval">HTML</h2>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    	     HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
    	  <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    		 HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages.
    		 HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms.
    		 It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items.
    	  </p>
          <h2 id="myval1">JavaScript</h2>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    		JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    		JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world.
    		All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
    		JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
    	  </p>
    	  <h2 id="myval2">JQuery</h2>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    	    jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    	    The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
    	  <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming.
    		jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library.
    		The purpose of jQuery is to make it much easier to use JavaScript on your website.
    	  </p>
         </div>
       </div>
    </div>
    </div>
    </body>
    </html>
    

    Das obige Skript verwendet affix-top.bs.affix Ereignis, das ausgelöst wird, bevor das Element oben angebracht wurde. Das heißt, wenn wir die Seite von unten nach oben scrollen, wird die Warnmeldung angezeigt, wenn wir oben mit der angegebenen Nachricht wie im Code angekommen sind.

    • Führen Sie Bootstrap Affix mithilfe eines Beispiels für Ereignisse aus

    Beispiel für Bootstrap-Affix mit Ereignissen

    Kommentar verfassen

    Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

    Nach oben scrollen