Erstellen einer Pyramide mit CSS und jQuery

Rummel17

Ich versuche einen sehr einfachen Pyramidenbausatz zu bauen. Der Benutzer kann einen Block hinzufügen oder einen entfernen.

Dies funktioniert gut mit dem folgenden Code. Derzeit gebe ich jedoch die Breite des nächsten Blocks mit css (.item1 {200px;}, .item2 {300px;}) an. Das Problem ist, dass ich nicht weiß, wie viele Blöcke der Benutzer eventuell hinzufügen wird, und daher kann ich nicht angeben Dies in CSS. Ich möchte die Breite der Breite jedes weiteren Blocks basierend auf der Breite des vorherigen Blocks + 100px (vorherige Breite + 100px) berechnen. Leider weiß ich nicht, wie das geht ... Jede Hilfe wäre willkommen .

einfaches Pyramidenbild

$(document).ready(function() {

  //Add Block Functionality
  $('#add-block .button').click(function() {
    var $block = $('<li><div class="item"><input class="input" type="text" placeholder="#" maxlength="2"></div></li>');
    $('.pyramid').append($block);
    $('ul li div').addClass(function(index) {
      return "item" + index;
    }) //this is to increase the item to item 1, item2, item3 etc.
  });

  //Remove Block Functionality
  $('#remove-block .button').click(function() {
      $('.pyramid li').last().remove();
    }

  )
});
.item {
  margin: 0 auto;
  position: relative;
  display: flex;
  justify-content: center;
}

.item0 {
  height: 0px;
  width: 0px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 60px solid #0488e0;
}

li div.item:not(.item0) {
  border-bottom: 60px solid #0488e0;
  border-left: 45px solid transparent;
  border-right: 45px solid transparent;
  margin-top: 10px;
  height: 0;
}

.item1 {
  width: 200px;
  /*plus 100px to create the trapezoid*/
}

.item2 {
  width: 300px;
  /*plus 100px to create the trapezoid*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="pyramid">
  <li>
  </li>
</ul>

<section class="buttons">
  <div id="add-block">
    <span>Add Block</span>
    <div class="button">+
    </div>
  </div>
  <div id="remove-block">
    <span>Remove Block</span>
    <div class="button">-
    </div>
  </div>
</section>

Pedram

Sie können die letzte .itemBreite erhalten und dann Ihre gewünschte Nummer anhängenvar lastwidth = $('.pyramid li:last-child .item').width();

$(document).ready(function() {

  //Add Block Functionality
  $('#add-block .button').click(function() {
    var lastwidth = $('.pyramid li:last-child .item').width();
    
    if(lastwidth == null){
    var plus = 0;
    } else {
    var plus = lastwidth + 100;
    }
    
    var $block = $('<li><div class="item" style="width:' + plus + 'px"><input class="input" type="text" placeholder="#" maxlength="2"></div></li>');
    $('.pyramid').append($block);

    
    //$('ul li div').addClass(function(index) {
    //return "item" + index;
    //}) //this is to increase the item to item 1, item2, item3 etc.
  });

  //Remove Block Functionality
  $('#remove-block .button').click(function() {
      $('.pyramid li').last().remove();
    }

  )
});
.item {
  margin: 0 auto;
  position: relative;
  display: flex;
  justify-content: center;
}

.item0 {
  height: 0px;
  width: 0px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 60px solid #0488e0;
}

li div.item:not(.item0) {
  border-bottom: 60px solid #0488e0;
  border-left: 45px solid transparent;
  border-right: 45px solid transparent;
  margin-top: 10px;
  height: 0;
}

.item1 {
  width: 200px;
  /*plus 100px to create the trapezoid*/
}

.item2 {
  width: 300px;
  /*plus 100px to create the trapezoid*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="pyramid">
  <li>
  </li>
</ul>

<section class="buttons">
  <div id="add-block">
    <span>Add Block</span>
    <div class="button">+
    </div>
  </div>
  <div id="remove-block">
    <span>Remove Block</span>
    <div class="button">-
    </div>
  </div>
</section>

Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.

Bei Verstößen wenden Sie sich bitte [email protected] Löschen.

bearbeiten am
0

Lass mich ein paar Worte sagen

0Kommentare
LoginNach der Teilnahme an der Überprüfung

Verwandte Artikel

Erstellen einer Pyramide mit Polygonbasis in CZML

Erstellen Sie eine Pyramide mit PHP For Loop und HTML

Erstellen einer Tabelle mit Javascript und jQuery

Erstellen einer dynamischen Checkbox-Liste mit JQuery und Bootstrap-CSS

Erstellen einer interaktiven Halbkreisnavigation mit HTML und CSS

Erstellen einer dynamischen Tabelle mit Objektbezeichnungen und -werten in jQuery?

Erstellen und Zuweisen einer CSS-Klasse in jquery

Erstellen einer Baumausgabe mit - und *

Erstellen eines Unschärfeeffekts mit CSS und jQuery beim Scrollen

Algorithmus zum Aufbau einer Pyramide mit Quadraten

Erstellen einer Seitenleiste in einer Flexbox mit CSS

Erstellen Sie in jquery eine Zeichenfolge mit einer Optgruppe und einer Option

Schlanke Methode zum Erstellen von Hover- und Klickfunktionen mit einer SVG-Verzeichniszuordnung mit jQuery

Erstellen einer URL mit Parametern mit jQuery

Erstellen einer Webanwendung mit WebSockets und AWS

Erstellen einer Profilseite mit tableView und textField

Erstellen einer Formel mit INDEX und IF

Erstellen einer Matrix mit Null und Eins

Erstellen einer Binärfunktion & nur mit ~ und |

Erstellen einer einfachen Foto-Diashow mit JavaScript, CSS und HTML

Erstellen einer Animation zum Auslösen eines Ereignisses mit Javascript und CSS

Erstellen einer responsiven HTML-Tabelle mit CSS-Raster und Sammeln von Daten aus JavaScript

Wie kann ich mit CSS und JQuery ein Div vertikal mit einer Schaltfläche und horizontal mit einer anderen umdrehen?

So erstellen Sie ein ziehbares Element über einer PDF mit PDF.js und jQuery

jQuery: Verwenden einer Schleife, um viele Elemente mit Bildern und HTML zu erstellen

Erstellen einer Schneeflockenanimation mit reduziertem CSS

Kombinieren von HTML, CSS und JS in einer HTML-Datei (Erstellen einer Glockenkurve mit der Google Charts API)

jquery | Show () und hide () mit divs erstellen

Erstellen Sie mit CSS und jQuery eine eigene Vue-Animation <Übergangsgruppe>