Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

vue.js - How to default navigation drawer to closed on mobile and open on desktop?

Im using the quasar framework for vue js. I have a sliding nav drawer. I am struggling to get its open/closed state to behave properly on mobile/desktop.

My requirements:

  • Drawer default open when screen size > 1023 pixels
  • When screen size > 1023 pixels, provide option to put drawer into mini mode via button (which is a compressed version of the drawer serving as a visual indicator it exists to the user)
  • When screen < 1023 pixels, drawer should be in mobile mode(i.e. requires a swipe gesture from left to right to display it).

I can accomplish the first two bullet points above, but when I'm on a mobile device I can't seem to get the drawer to start hidden on page load. When the page loads, the drawer is in open state. The drawer should only be in open state on mobile if the user opens it themselves.

Here is what I have tried:

<q-drawer
  v-model="rightDrawer"
  side="right"
  :mini="miniStateRight"
  :width="350"
  :breakpoint="1023"
  bordered
  overlay
  content-class="bg-grey-3"
>
  <q-page-sticky class="gt-sm" position="left" :offset="[-15, 0]">
    <q-btn
      @click="miniStateRight = !miniStateRight"
      round
      color="primary"
      size="sm"
      icon="mdi-feature-search-outline"
    />
  </q-page-sticky>
</q-drawer>
data: () => ({
  rightDrawer: true,
  miniStateRight: false,
}),

How can I get the drawer to default to the closed position on page load when the screen is < 1023 pixels? I have access to the current screen width via $q.screen.width plugin.

Many thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can just set the drawer value to true or false in data by comparing screen width.

Example - drawer: this.$q.screen.width < 1023?false:true,

Working codepen - https://codepen.io/Pratik__007/pen/poELqgK


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...