No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Detect screen size (xs, sm, md, etc) via javascript?

    Framework
    5
    6
    8118
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      toymachiner62 last edited by

      I see there are all these fabulous css helpers for showing things or not based on the screen size (xs, sm, md, etc) https://quasar-framework.org/components/visibility.html#Window-Width-Related and these work for most of my use cases, but i came across a case where I need to set a boolean variable based on the screen size.

      For example I want my q-layout-drawer to render open on page load for anything greater than size md, but i want it to render closed on page load for small screens.

      Is there a way I can set a variable in mounted () by detecting the screen size?

      1 Reply Last reply Reply Quote 1
      • metalsadman
        metalsadman last edited by metalsadman

        @toymachiner62
        check this out https://quasar-framework.org/components/window-resize-observable.html, then use a boolean v-model for your q-layout-drawer <q-layout-drawer v-model="drawerState" ..

        ...
        methods: {
          onResize (size) {
              console.log(size)
              // {
              //   width: 1200 // width of viewport (in px)
              //   height: 920 // height of viewport (in px)
              // }
              if (size.width > 992) { //md is 992px
                this.drawerState = true
              }
          }
        ...
        
        1 Reply Last reply Reply Quote 2
        • T
          toymachiner62 last edited by

          I just came across the screen plugin which seems like it would solve what i’m looking for! https://quasar-framework.org/components/screen-plugin.html

          1 Reply Last reply Reply Quote 2
          • DarkLite1
            DarkLite1 last edited by DarkLite1

            I stumbled upon the same question and solved it as documented by the Quasar team:

            Instead of using this css:

            @media screen and (min-width: 599px) {
              .q-footer {
                display: none;
              }
            }
            

            I now do this in my template:

            <div class="lt-sm">Small display</div>
            <div class="gt-xs">Large display</div>
            

            Another option if you can’t use css classes is using $q.screen in your <script> section of a component:

             methods: {
                setSomething() {
                  if (this.$q.screen.width > 1023) { 
                     console.log('screen large') 
                  }
                  else {
                     console.log('screen small') 
                  }
                },
              },
              watch: {
                "$q.screen.width"() {
                  this.setSomething()
                }
              }
            

            If it’s not in a component and you want to know the screen size on the startup of your app for example you can use the following:

            import { Screen } from 'quasar'
            Screen.lt.sm
              ? console.log('screen small')
              : console.log('screen large')
            

            I hope this helps someone.

            1 Reply Last reply Reply Quote 3
            • L
              luizamorim last edited by luizamorim

              console.log(this.$q.screen)
              
              {
                xs: false,
                sm: false,
                md: false,
                lg: true,
                xl: false,
                sizes: {
                  sm: 600,
                  md: 1024,
                  lg: 1440,
                  xl: 1920
                }
              }
              
              1 Reply Last reply Reply Quote 4
              • D
                dsl101 last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post