123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <p class="instructions">Pick the cities in the order you would wish to visit.</p>

<div id="app">
  <div class="container">
    <!-- create our empty "backing" that our choices will slide into, technically on top -->
    <ul class="list-back">
      <li v-for="city in cities" :key="city"></li>
    </ul>
    <!-- transition-group works the much the same as transition, but for "lists" -->
    <!-- note the tag attribute states "ul", otherwise it defaults to span -->
    <transition-group :name="currentListTransition" tag="ul" class="list">
      <!-- whenever a choice is made, for loop updates the markup -->
      <li v-for="(item, index) in selectedItems" :key="item">{{ item }}</li>
    </transition-group>
  </div>
  <div class="container">
    <ul class="list">
      <!-- creates our list to choose from -->
      <li class="city" v-for="(city, index) in cities" :key="city" :data-index="index" @click="chooseCity">{{ city }}</li>
    </ul>
  </div>
  <div class="panel">
    <!-- this appears to be the button to clear our selected list -->
    <button class="clear" @click="clearSelection">clear</button>
  </div>
</div>

            
          
!
            
              body {
  background-color: #707070;
  font-family: sans-serif;
  height: 100vh;
  overflow: hidden;
  width: 100vw;
}

#app {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  height: 100%;
  justify-content: center;
  width: 100%;
}

.instructions {
  color: white;
  font-size: 20px;
  text-align: center;
}

.container {
  height: 150px;
  margin: 20px;
  position: relative;
  width: 200px;
}

.list,
.list-back {
  align-items: center;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  
  li {
    align-items: center;
    display: flex;
    flex-shrink: 0;
    font-size: 24px;
    height: 34px;
    justify-content: center;
    margin-bottom: 4px;
    width: 100%;
  }
}
.list-back li {
  background-color: #E0E0E0;
  border-radius: 24px;
}

.city {
  background-color: #E0E0E0;
  border-radius: 24px;
  cursor: pointer;
  transition: 0.5s;
  
  &:hover {
    background-color: #307B21;
    color: #F5F5F5;
  }
  &.selected {
    opacity: 0.5;
    pointer-events: none;
  }
}
.panel {
  display: flex;
  justify-content: center;
  width: 100%;
  
  .clear {
    background-color: #C11826;
    border: {
      radius: 8px;
      style: none;
    }
    color: #F5F5F5;
    cursor: pointer;
    padding: 8px 16px;
  }
}

// each transition is prefixed with the direction they are to appear from
.top-enter-active, .top-leave-active { transition: 0.5s; }
.top-enter, .top-leave-to { opacity: 0; transform: translate3d(0, -40px, 0); }
.top-move { opacity: 0.5; transition: 0.5s; }

.left-enter-active, .left-leave-active { transition: 0.5s; }
.left-enter, .left-leave-to { opacity: 0; transform: translate3d(-40px, 0, 0); }
.left-move { opacity: 0.5; transition: 0.5s; }

// this transition is also used from the "clear" button
.right-enter-active, .right-leave-active { transition: 0.5s; }
.right-enter, .right-leave-to { opacity: 0; transform: translate3d(40px, 0, 0); }
.right-move { opacity: 0.5; transition: 0.5s; }

.bottom-enter-active, .bottom-leave-active { transition: 0.5s; }
.bottom-enter, .bottom-leave-to { opacity: 0; transform: translate3d(0, 30px, 0); }
.bottom-move { opacity: 0.5; transition: 0.5s; }

// if animations are reduced at the OS level, turn off transitions
@media screen and (prefers-reduced-motion: reduce) {
  .top-enter-active, .top-leave-active { transition: none; }
  .top-move { transition: none; }
  .left-enter-active, .left-leave-active { transition: none; }
  .left-move { transition: none; }
  .right-enter-active, .right-leave-active { transition: none; }
  .right-move { transition: none; }
  .bottom-enter-active, .bottom-leave-active { transition: none; }
  .bottom-move { transition: none; }
}
            
          
!
            
              new Vue({
  el: "#app",
  data: {
    // array to keep track of selected cities
    selectedItems: [],
    // array of the current choices
    cities: ['Chicago', 'New York', 'Seattle', 'Miami', 'Boston'],
    // this string gets changed as choices are made
    // but we know the first one will be from the top anyway...
    currentListTransition: 'top'
  },
  methods: {
    // method for the city list you choose from
    // the index is passed because that easily relates to the arrays
  	chooseCity: function (event) {
      let selectedLength = this.selectedItems.length;
      let citiesLength = this.cities.length;
      let clt = this.currentListTransition;
    
      // somewhat complicated if/else sequence
      // mainly because I wanted left and right entries for the middle items
      if (selectedLength === 0) {
        // we most likely don't require this since it is default
        // but, you never know
        clt = 'top';
      } else if (selectedLength > 0 && selectedLength < citiesLength - 1) {
        // check the previous transition direction
        // if it was top or left then go right, if it was right then go left
        // could reverse this, or even make it random for a large list
      	clt = clt === 'top' || clt === 'left' ? 'right' : 'left';
      } else if (selectedLength === citiesLength - 1) {
        // always make last one appear from bottom
        clt = 'bottom';
      }
    
      // update the transition
    	this.currentListTransition = clt;
      // push the choice into the array to trigger the transition
    	this.selectedItems.push(this.cities[event.target.dataset.index]);
      // mark the choice as selected
      event.target.classList.add('selected');
    },
    clearSelection: function () {
      // make everything disappear to the right
      // this can be changed to any of the other transitions
    	this.currentListTransition = 'right';
      // clearing the array causes chosen cities to go transition out
    	this.selectedItems = [];
      // clear selections so we can start again
      document.querySelectorAll('.city.selected').forEach(element => {
      	element.classList.remove('selected');
      });
    }
  }
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console