Javascript https://gnuschichten.de/ de Die Länge eines Textes mit Javascript überprüfen https://gnuschichten.de/snippets/text-laenge-mit-javascript-ueberpruefen <span class="field field--name-title field--type-string field--label-hidden">Die Länge eines Textes mit Javascript überprüfen</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="https://gnuschichten.de/user/6" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">Holger Weischenberg</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mi., 01.06.2022 - 10:21</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Die Anforderung war bei mehr als 50 Zeichen eine kleinere Schriftgröße anzuzeigen. Über 50 Zeichen wird eine CSS-Klasse in das Parent-Div einer h1 geschrieben.</p> <p>Die Funktion ist in ein Drupal-Behavior eingebettet.</p> <pre> <code class="language-javascript">Drupal.behaviors.pageTitleLength = { attach: function (context) { const title = document.getElementsByTagName('h1', context); const titleLength = title[0].firstChild['length']; const parentDiv = title[0].parentNode; if (titleLength > 60) { parentDiv.classList.add('long-title'); } } };</code></pre> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="https://gnuschichten.de/taxonomy/term/13" hreflang="de">Javascript</a></div> </div> </div> Wed, 01 Jun 2022 08:21:23 +0000 Holger Weischenberg 21 at https://gnuschichten.de Click Event-Listner anstatt Hover auf Mobil-Clients https://gnuschichten.de/snippets/resize-listener <span class="field field--name-title field--type-string field--label-hidden">Click Event-Listner anstatt Hover auf Mobil-Clients </span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="https://gnuschichten.de/user/6" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">Holger Weischenberg</span></span> <span class="field field--name-created field--type-created field--label-hidden">Do., 19.11.2020 - 13:28</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>In einem Projekt sollte ein CTA, der durch Hover aus dem negative Margin geholt wurde, mobil durch ein Klick-Event verfügbar gemacht werden. Hier der kurze Vanilla Javascript Code mit rudimentären Features.</p> <pre> <code class="language-javascript">const items = document.querySelectorAll('.sticky-cta'); const breakpoint = window.innerWidth <= 768; for (const item of items) { const wrapper = item.querySelector('.wrapper-class'); const icon = item.querySelector('.icon-class'); // If initial load is in mobile viewport if (breakpoint) { item.addEventListener('click', ctaClass, true); } function ctaClass() { item.classList.toggle('cta-clicked-js'); wrapper.classList.toggle('cta-clicked-js'); icon.classList.toggle('cta-clicked-js'); } // On viewport change window.addEventListener('resize', (e) => { if (window.innerWidth <= 768) { item.addEventListener('click', ctaClass, true); } else { item.removeEventListener('click', ctaClass, true); item.classList.remove('cta-clicked-js'); } }) }</code></pre> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="https://gnuschichten.de/taxonomy/term/13" hreflang="de">Javascript</a></div> </div> </div> Thu, 19 Nov 2020 12:28:45 +0000 Holger Weischenberg 17 at https://gnuschichten.de "Load more" Button in Vanilla Javascript https://gnuschichten.de/snippets/loadmore <span class="field field--name-title field--type-string field--label-hidden">"Load more" Button in Vanilla Javascript</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="https://gnuschichten.de/user/6" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">Holger Weischenberg</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mo., 16.11.2020 - 09:46</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Beispiel für eine "Mehr laden" Funktionalität.</p> <pre> <code class="language-javascript">let parent = document.querySelector('.news') if (!parent === false) { let items = parent.querySelectorAll('.news-item'); let loadMoreBtn = document.querySelector('.load-more'); let maxItems = 3; let hiddenClass = "hidden-news"; [].forEach.call(items, function (item, idx) { if (idx > maxItems - 1) { item.classList.add(hiddenClass); } }); loadMoreBtn.addEventListener('click', function (e) { e.stopPropagation(); e.preventDefault(); [].forEach.call(document.querySelectorAll('.' + hiddenClass), function (item, idx) { if (idx < maxItems - 1) { item.classList.remove(hiddenClass); } if (document.querySelectorAll('.' + hiddenClass).length === 0) { loadMoreBtn.style.opacity = '0'; } }); }); }</code></pre> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="https://gnuschichten.de/taxonomy/term/13" hreflang="de">Javascript</a></div> </div> </div> Mon, 16 Nov 2020 08:46:15 +0000 Holger Weischenberg 14 at https://gnuschichten.de "Mehr lesen" Accordion in Vanilla Javascript https://gnuschichten.de/snippets/readmore <span class="field field--name-title field--type-string field--label-hidden">"Mehr lesen" Accordion in Vanilla Javascript</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="https://gnuschichten.de/user/6" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">Holger Weischenberg</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mo., 16.11.2020 - 09:30</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Vanilla Javascript für "Mehr lesen" als verstecktes Div.<br />  </p> <pre> <code class="language-javascript">( function() { const items = document.querySelectorAll('.readmore-wrapper'); for (const item of items) { const button = item.querySelector('.readmore-block-button'); const icon = item.querySelector('.readmore-icon'); const body = item.querySelector('.readmore-hidden'); body.classList.add('d-none'); button.addEventListener('click', (e) => { e.stopPropagation(); // Beispiel: Bootstrap Klasse für display: none body.classList.toggle('d-none'); // Icon Styles icon.classList.toggle('readmore-icon-open'); }); } } )(); </code></pre> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="https://gnuschichten.de/taxonomy/term/13" hreflang="de">Javascript</a></div> </div> </div> Mon, 16 Nov 2020 08:30:30 +0000 Holger Weischenberg 13 at https://gnuschichten.de Javascript Grundlagen - Strings https://gnuschichten.de/snippets/javascript-grundlagen-strings <span class="field field--name-title field--type-string field--label-hidden">Javascript Grundlagen - Strings</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="https://gnuschichten.de/user/6" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">Holger Weischenberg</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mi., 30.09.2020 - 12:42</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2><strong>Methoden für Strings</strong></h2> <p>Anzahl der Zeichen</p> <pre> <code class="language-javascript">console.log(foo.length)</code></pre> <p>Zeichen an Position abfragen</p> <pre> <code class="language-javascript">console.log(foo.charAt(3)) // Kurzform console.log(foo[3]) </code></pre> <p>Zeichen im String finden</p> <pre> <code class="language-javascript">console.log(foo.indexOf("f")) // Startet an Position 10 console.log(foo.indexOf("o", 10))</code></pre> <p>String zu Groß- oder Kleinbuchstaben umwanden</p> <pre> <code class="language-javascript">console.log(foo.toUpperCase()) console.log(foo.toLowerCase())</code></pre> <p>Elemente im String ersetzen</p> <pre> <code class="language-javascript">console.log(foo.replace("a", "b"))</code></pre> <p>Zeichen rechts und links entfernen</p> <pre> <code class="language-javascript">console.log(foo.trim())</code></pre> <p>Zeichen entfernen</p> <pre> <code class="language-javascript">// .slice(start, end) console.log("Hallo Welt".slice(6, 8)) // .substr(start, length) console.log("Hallo Welt".substr(6, 2))</code></pre> <p>String wiederholen</p> <pre> <code class="language-javascript">console.log("a".repeat(10))</code></pre> <p>Neue Zeile</p> <pre> <code class="language-javascript">console.log('Hallo\nWelt!')</code></pre> <p> </p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="https://gnuschichten.de/taxonomy/term/13" hreflang="de">Javascript</a></div> </div> </div> Wed, 30 Sep 2020 10:42:40 +0000 Holger Weischenberg 8 at https://gnuschichten.de