Vue.jsでハンバーガーメニューと横からスライドするメニューリスト
やりたいこと
- ヘッダにハンバーガーメニューを置く。
- ヘッダをクリックすると、横からメニューがスライドして出てくる。
コード
下記ソースは単一ファイルコンポーネントです。
全てコピーして.vue
で保存することで(多分)そのまま動きます。
<template> <div> <header class="header clearfix"> <div class="menu" @click="openMenu"> <span class="menu-line"></span> <span class="menu-line"></span> <span class="menu-line"></span> </div> </header> <div class="menu-list" :class="{'avtive': isOpen}" > <h1>Menu List</h1> </div> <div class="contents"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </div> </template> <script> export default { name: 'AppTest', data() { return { isOpen: false, }; }, methods: { openMenu() { this.isOpen = !this.isOpen; }, }, }; </script> <style> .header { width: 100%; height: auto; position: relative; background: rgba(0, 0, 0, 0.3); display: inline-block; } .clearfix::after { content: ''; display: block; clear: both; } .menu { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; right: 0; width: 44px; padding: 13px 10px; width: 60px; height: 60px; float: right; transition: all .4s; } .menu-line { display: block; width: 60px; height: 2px; margin: 0 auto 0 0; background: #0257BD; transition: all .4s; } .menu-line:nth-child(2){ width: 40px; margin: 0 0 0 auto; } .menu-line:nth-child(3){ width: 35px; margin: 0 0 0 auto; } .menu-list { position: fixed; right: 0; width: 82%; height: 100%; background: #fff; font-size: 14px; transform: translateX(+100%); transition: all .3s; background: gray; } .menu-list.avtive { transform: translateX(0) } </style>