0

I found a codepen example of making an animated dropdown menu. Using CSS, how would I make it slide up from the top of the button?

new Vue({
  el: '#app',
  data: () => ({
    isDropped: false
  }),
  methods: {
    dropIt() {
      this.isDropped = !this.isDropped
    }
  }
})
html{ display: grid; height: 100% }
body{
  margin: auto;
  background: linear-gradient(#5e2973, #692424);
}
button{
  padding: 20px 60px;
  border: solid thin darken(#b50064, 10%);
  background: #b50064;
  color: #eee;
  &:focus{
    outline: solid thin lighten(#b50064, 20%);
  }
}
.list{
  position: absolute;
  width: 204px;
  margin: 0;
  padding: 0;
  list-style-type: none;
  transform-origin: top;
  transition: transform .4s ease-in-out;
  overflow: hidden;
  li{
    padding: 10px;
    background: white;
    border-bottom: solid thin #eee;
    border-left: solid medium #cbc;
  }
}
.slide-enter, .slide-leave-to{
  transform: scaleY(0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="dropIt">Pet Options</button>
  <transition name="slide">
    <ul class="list" v-if="isDropped">
      <li>Dog</li>
      <li>Cat</li>
      <li>Bunny</li>
      <li>Parrot</li>
      <li>Fish</li>
    </ul>
  </transition>
</div>

2
  • To clarify, you want the dropdown menu to be appear above the button? Commented Feb 24, 2022 at 17:11
  • Yes, above the button, expanding upwards. Commented Feb 24, 2022 at 17:12

1 Answer 1

2

Adjust your transform-origin and the position of the .list a bit. See included snippet.

new Vue({
  el: '#app',
  data: () => ({
    isDropped: false
  }),
  methods: {
    dropIt() {
      this.isDropped = !this.isDropped
    }
  }
})
html{ display: grid; height: 100% }
body{
  margin: auto;
  background: linear-gradient(#5e2973, #692424);
}
#app { /* new */
  position: relative;
}
button{
  padding: 20px 60px;
  border: solid thin darken(#b50064, 10%);
  background: #b50064;
  color: #eee;
  &:focus{
    outline: solid thin lighten(#b50064, 20%);
  }
}
.list{
  position: absolute;
  bottom: 100%; /* new */
  width: 204px;
  margin: 0;
  padding: 0;
  list-style-type: none;
  transform-origin: bottom; /* changed to bottom */
  transition: transform .4s ease-in-out;
  overflow: hidden;
  li{
    padding: 10px;
    background: white;
    border-bottom: solid thin #eee;
    border-left: solid medium #cbc;
  }
}
.slide-enter, .slide-leave-to{
  transform: scaleY(0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="dropIt">Pet Options</button>
  <transition name="slide">
    <ul class="list" v-if="isDropped">
      <li>Dog</li>
      <li>Cat</li>
      <li>Bunny</li>
      <li>Parrot</li>
      <li>Fish</li>
    </ul>
  </transition>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.