Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="app">
  <div
    :class="[
      `total`,
      {
        'total--open': cartOpen,      
      },
    ]"
       
    :style="{
      '--cart-count': cartCount,        
    }"
  >
    <div class="cart">
      <table>
        <template v-for="(item, index) in cart">
          <tr v-if="item.quantity">
  <!--           {{ item }} -->
            <td>{{ item.title }}</td>
            <td>{{ item.quantity }}</td>
            <td>{{ numberToCurrency(item.price) }}</td>
            <td>{{ numberToCurrency(item.price * item.quantity )}}</td>
          </tr>
        </template>
        <tr class="cart__total">
          <td colspan="3">Total</td>
          <td>{{ total }}</td>
        </tr>
      </table>
    </div>
<!--     <template v-if="total === 0">Buy something!</template> -->
<!--     <template v-else><span>{{ total }}</span></template> -->
    <button @click="cartOpen = !cartOpen" :disabled="!cartCount">{{ total === 0 ? `Cart` : total }}</button>
  </div>
  <div class="cards">
    <module-card
      v-for="product in products"
      :key="product.id"

      :product="product"
      :cart="cart"

      @cart-change="onCartChange($event)"
    />
  </div>
</div>
            
          
!

CSS

            
              $card-button-gutter: 16px;
$card-button-decrease-size: 60px;
$card-button-color: #2e91ff;
$card-quantity-size: 35px;

$card-color: #262626;

$timing: 250ms;
$easing: ease-in-out;

body {
  background: mix(#f9f9f9, $card-button-color, 90%);
  font-family: sans-serif;
  padding-top: 80px; // WHY: Offset header
}

.total {
  --cart-height: calc((var(--cart-count, 1) + 1) * 40px + 10px);
  
  background: white;
  box-shadow: rgba(black, 0.2) 0 2px 8px;
  position: fixed;
  padding: 20px;
  top: calc(var(--cart-height) * -1);
  left: 0;
  width: 100%;
  z-index: 99;
  transition: transform $timing $easing;
  
  &--open {
    transform: translateY(var(--cart-height));
  }
  
  > span {
    font-weight: 600;
  }
  
  button {
    appearance: none;
    background: $card-button-color;
    border: 0;
    border-radius: 4px;
    color: white;
    cursor: pointer;
    outline: 0;
    line-height: 32px;
    position: relative;
    user-select: none;
    
    &[disabled] {
      opacity: 0.5;
      pointer-events: none;
    }
  }
}

.cart {
  height: var(--cart-height);
  
  table {
    margin-left: -10px;
    margin-right: -10px;
  }
  
  td {
    line-height: 30px;
    padding: 0 10px;
  }
  
  &__total {
    font-weight: bold;
  }
}

.cards {
  display: flex;
  flex-wrap: wrap;
}

.card {
  box-shadow: rgba(black, 0.2) 0 2px 8px;
  background: white;
  color: $card-color;
  display: flex;
  flex-direction: column;
  border-radius: 4px;
  padding: 20px;
  margin: 20px;
  max-width: 200px;
  position: relative;
  user-select: none;
  
  &__quantity {
    background: $card-button-color;
    border-radius: 50%;
    font-size: $card-quantity-size / 2;
    margin: $card-quantity-size / -2 $card-quantity-size / -2 0 0;
    color: white;
    height: $card-quantity-size;
    width: $card-quantity-size;
    line-height: $card-quantity-size;
    position: absolute;
    top: 0;
    right: 0;
    transition:
      transform $timing $easing;
    
    .card--empty & {
      transform: scale(0);
    }
  }
  
  &__count {
    animation: count-down $timing $easing;
    position: absolute;
    font-weight: bold;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    opacity: 0;
    
    @keyframes count-down {
      0% {
        opacity: 1;
      }
      100% {
        opacity: 0;
      }
    }
    
    &--active {
      opacity: 1;
      animation: count-up $timing $easing;
      
      @keyframes count-up {
        0% {
          opacity: 0;
          transform: translateY(25%);
        }
        100% {
          opacity: 1;
          transform: none;
        }
      }
    }
  }
  
  &__title {
    font-size: 2em;
  }
  
  &__price {
    display: block;
    margin: 1em 0 auto;
  }
  
  &__full-price {
    display: inline-block;
    position: relative;
    transition: color $timing $easing;
    
    &::after {
      background: $card-color;
      box-shadow:
          white 0 1px 0,
          white 0 -1px 0;
      position: absolute;
      left: 0;
      content: '';
      height: 1.5px;
      top: 45%;
      margin-top: -1px;
      width: 100%;
      transform: rotate(2deg) scale(0, 1);
      transition: transform $timing $easing;
      transform-origin: bottom left;
    }
    
    &--discounted {
      color: rgba($card-color, 0.5);
      
      &::after {
        transform: rotate(2deg) scale(1, 1);
      }
    }
  }
  
  &__discount {
    font-weight: bold;
    display: inline-block;
    opacity: 0;
    
    &--active {
      opacity: 1;
    }
  }
  
  &__buttons {
    display: flex;
    overflow: hidden;
    margin: 30px auto 0;
    width: 100%;
  }
  
  &__button {
    appearance: none;
    background: $card-button-color;
    border: 0;
    border-radius: 4px;
    color: white;
    cursor: pointer;
    outline: 0;
    line-height: 32px;
    position: relative;
    user-select: none;
    
    > span {
      display: block;
      position: relative;
      z-index: 2;
    }
    
    &--increase {
      margin-right: $card-button-gutter;
      width: calc(100% - #{$card-button-gutter + $card-button-decrease-size});
      z-index: 3;
      
      > span {
        transition:
          transform $timing $easing;
        
        .card--empty & {
          transform: translateX(#{($card-button-gutter + $card-button-decrease-size) / 2});
        }
      }
      
      &::before,
      &::after {
        background: inherit;
        border-radius: inherit;
        content: '';
        height: 100%;
        left: 0;
        position: absolute;
        top: 0;
        width: 100%;
        z-index: 1;
        transition:
          transform $timing $easing;
      }
      
      &::after {
        left: 75%;
        width: calc(25% + #{$card-button-gutter + $card-button-decrease-size});
        transform: translateX(#{($card-button-gutter + $card-button-decrease-size) * -1});

        .card--empty & {
          transform: none;
        }
      }
    }
    
    &--decrease {
      background: none;
      color: $card-button-color;
      transition:
        opacity $timing $easing,
        transform $timing $easing;
      transform: none;
      width: $card-button-decrease-size;
      z-index: 2;
      
      &::before {
        box-sizing: border-box;
        border: 2px solid currentColor;
        border-radius: inherit;
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
      }
      
      .card--empty & {
        // transform: translateX(calc(100% + #{$card-button-gutter}));
        transform: scale(0.5);
        opacity: 0;
      }
    }
  }
}
            
          
!

JS

            
              // Polyfills
if (!Object.prototype.forEach) {
	Object.defineProperty(Object.prototype, 'forEach', {
		value: function (callback, thisArg) {
			if (this == null) {
				throw new TypeError('Not an object');
			}
			thisArg = thisArg || window;
			for (var key in this) {
				if (this.hasOwnProperty(key)) {
					callback.call(thisArg, this[key], key, this);
				}
			}
		}
	});
}

// Services
const numberToCurrency = number => !number ? number : number.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });

// Modules
const ModuleCard = Vue.component(`module-card`, {
  template: `
<div
  :class="[
    \`card\`,
    {
      'card--empty': quantity <= 0,
    },
  ]"
>
  <span class="card__quantity">
    <b
      v-for="count in quantity"
      :key="count"

      :class="[
        \`card__count\`,
        {
          'card__count--active': quantity == count,
        },
      ]"

      v-html="count"
    />
  </span>
  <span class="card__title">{{ product.title }}</span>
  <span class="card__price">
    <span :class="[\`card__full-price\`, { 'card__full-price--discounted': price < product.prices[1] }]">{{ numberToCurrency(product.prices[1]) }} each</span>
    <span :class="[\`card__discount\`, { 'card__discount--active': price < product.prices[1] }]">Multibuy: {{ numberToCurrency(price) }} each</span>
  </span>
  <!--<ul class="card__prices">
    <li v-for="(pricePoint, priceQuantity, index) in product.prices" :key="index">
      {{ priceQuantity }}: {{ pricePoint }}
    </li>
  </ul>-->
  <span class="card__buttons">
    <button class="card__button card__button--increase" @click="cartChange(1)"><span>Add to cart</span></button>
    <button class="card__button card__button--decrease" @click="cartChange(-1)"><span>Remove</span></button>
  </span>
  <!--<pre>{{ product }}</pre>-->
  <!--<pre>{{ cart }}</pre>-->
</div>`,
  
  // Props
  props: {
    product: {
      type: Object,
      default: () => ({}),
    },
    cart: {
      type: Object,
      default: () => ({}),
    },
  },
  
  // Data
  computed: {
    quantity() {
      return this.cart[this.product.id].quantity || 0;
    },
    price() {
      return this.getPrice(this.quantity);
    },
  },
  
  // Methods
  methods: {
    getPrice(quantity) {
      let setPrice = 0;
      const prices = this.product.prices;
      
      prices.forEach((price, pricePoint) => {
        if (quantity >= parseInt(pricePoint, 10)) {
          setPrice = price;
        }
      });
      
      return setPrice || prices[`1`];
    },
    cartChange(diff) {
      let quantity = this.quantity + diff;
      
      if (quantity <= 0) quantity = 0;
      
      this.$emit(`cart-change`, {
        id: this.product.id,
        quantity,
        price: this.getPrice(quantity),
      });
    },
  },
});

// Models
const products = [
  {
    id: `1`,
    title: `Socks`,
    prices: {
      6: 9.99,
      3: 10.99,
      1: 12.99,
    },
  },
  {
    id: `gloves`,
    title: `Merino Wool Gloves`,
    prices: {
      9: 12.99,
      6: 14.99,
      3: 17.25,
      1: 20.00,
    },
  },
  {
    id: `hat`,
    title: `Wooly Hat`,
    prices: {
      1: 19.99,
    },
  }
];

// Vue
new Vue({
  el: '#app',
  
  // Components
  components: {
    ModuleCard,
  },
  
  // Data
  data() {
    return {
      products,
      cart: {},
      cartOpen: false,
    };
  },
  computed: {
    total() {
      let total = 0;
      
      this.cart.forEach(item => {
        total += (item.quantity * item.price);
      });
      
      return numberToCurrency(total);
    },
    cartCount() {
      let count = 0;
      
      this.cart.forEach(item => {
        count += item.quantity ? 1 : 0;
      });
      
      return count;
    },
  },
  
  // Methods
  methods: {
    onCartChange({ quantity, id, price }) {
      if (quantity < 0) quantity = 0;
      
      this.cart[id].quantity = quantity;
      this.cart[id].price = price;
    },
    numberToCurrency(number) {
      return numberToCurrency(number);
    },
  },
  
  // Lifecycle
  created() {
    this.products.forEach(({ id, title }) => {
      this.$set(this.cart, id, {
        quantity: 0,
        price: 0,
        title,
      });
    });
  },
});
            
          
!
999px

Console