Responsive

QResponsive 组件会强制内容根据其宽度保持长宽比。

QResponsive API

QResponsive API

ratio
: String | Number
说明
内容的宽高比;如果值是一个字符串,则避免使用计算语句(如 '16/9'),而是直接指定结果的字符串值(例如 '1.7777')

用法

提示

  • 这个组件可以放置任何内容,只要只指定一个直接子元素即可。如果您有多个元素,可以使用一个 <div> 包裹起来。
  • 您需要确保内容不会溢出容器。

WARNING

不要将其用于已经具有 ratio 属性的 Quasar 组件,如 QImg 或 QVideo,或具有强制高度的组件。

基础



<template>
  <div class="q-pa-md">
    <div class="q-gutter-md" style="max-width: 300px">
      <q-responsive :ratio="16/9">
        <div class="rounded-borders bg-primary text-white flex flex-center">
          Ratio 16:9
        </div>
      </q-responsive>

      <q-responsive :ratio="1">
        <div class="rounded-borders bg-primary text-white flex flex-center">
          Ratio 1:1
        </div>
      </q-responsive>
    </div>
  </div>
</template>

Ratio 16:9
Ratio 1:1

Flex row

注意,我们在下面的示例中使用了 items-start 来设置垂直对齐替换默认的 stretch,所以 flexbox 不会给 QResponsive 组件强制高度。



<template>
  <div class="q-pa-md">
    <div class="row items-start q-gutter-md">
      <q-responsive :ratio="16/9" class="col">
        <div class="rounded-borders bg-primary text-white flex flex-center">
          Ratio 16:9
        </div>
      </q-responsive>

      <q-responsive :ratio="1" class="col">
        <div class="rounded-borders bg-primary text-white flex flex-center">
          Ratio 1:1
        </div>
      </q-responsive>
    </div>
  </div>
</template>

Ratio 16:9
Ratio 1:1

一些组件示例

下面只是一些例子。 QResponsive 不仅限于 QCard 和 QCarousel。



<template>
  <div class="q-pa-md">
    <div class="row items-start q-gutter-md">
      <q-responsive :ratio="16/9" class="col">
        <q-card class="column">
          <q-img class="col" src="https://cdn.quasar.dev/img/parallax2.jpg" />

          <q-card-section>
            <div>Ratio 16:9</div>
          </q-card-section>
        </q-card>
      </q-responsive>

      <q-responsive :ratio="1" class="col">
        <q-card class="column">
          <q-img class="col" src="https://cdn.quasar.dev/img/parallax1.jpg" />

          <q-card-section>
            <div>Ratio 1:1</div>
          </q-card-section>
        </q-card>
      </q-responsive>
    </div>
  </div>
</template>

Ratio 16:9
Ratio 1:1


<template>
  <div class="q-pa-md">
    <div class="row items-start q-gutter-md">
      <q-card flat bordered class="col">
        <q-item>
          <q-item-section avatar>
            <q-avatar>
              <img src="https://cdn.quasar.dev/img/boy-avatar.png">
            </q-avatar>
          </q-item-section>

          <q-item-section>
            <q-item-label>Title</q-item-label>
            <q-item-label caption>
              Subhead
            </q-item-label>
          </q-item-section>
        </q-item>

        <q-separator />

        <q-responsive :ratio="16/9">
          <!-- notice "border-radius-inherit" below; it's important when in a QCard -->
          <q-card-section class="border-radius-inherit flex flex-center bg-grey-1">
            <div>QCardSection with ratio 16:9</div>
          </q-card-section>
        </q-responsive>
      </q-card>

      <q-card flat bordered class="col">
        <q-item>
          <q-item-section avatar>
            <q-avatar>
              <img src="https://cdn.quasar.dev/img/boy-avatar.png">
            </q-avatar>
          </q-item-section>

          <q-item-section>
            <q-item-label>Title</q-item-label>
            <q-item-label caption>
              Subhead
            </q-item-label>
          </q-item-section>
        </q-item>

        <q-separator />

        <q-responsive :ratio="1">
          <!-- notice "border-radius-inherit" below; it's important when in a QCard -->
          <q-card-section class="border-radius-inherit flex flex-center bg-grey-1">
            <div>QCardSection with ratio 1:1</div>
          </q-card-section>
        </q-responsive>
      </q-card>
    </div>
  </div>
</template>

Title
Subhead

QCardSection with ratio 16:9
Title
Subhead

QCardSection with ratio 1:1


<template>
  <div class="q-pa-md">
    <q-responsive :ratio="16/9">
      <q-table
        class="my-sticky-table"
        virtual-scroll
        v-model:pagination="pagination"
        :rows-per-page-options="[0]"
        :virtual-scroll-sticky-size-start="48"
        row-key="index"
        title="Table aspect ratio: 4/3"
        :rows="rows"
        :columns="columns"
      />
    </q-responsive>
  </div>
</template>

Table aspect ratio: 4/3
#Dessert (100g serving)CaloriesFat (g)Carbs (g)Protein (g)Sodium (mg)Calcium (%)Iron (%)
0Frozen Yogurt15962448714%1%
1Ice cream sandwich2379374.31298%1%
2Eclair262162363376%7%
3Cupcake3053.7674.34133%8%
4Gingerbread35616493.93277%16%
5Jelly bean3750940500%0%
6Lollipop3920.2980380%2%
7Honeycomb4083.2876.55620%45%
8Donut45225514.93262%22%
9KitKat518266575412%6%
10Frozen Yogurt15962448714%1%
11Ice cream sandwich2379374.31298%1%
12Eclair262162363376%7%
13Cupcake3053.7674.34133%8%
14Gingerbread35616493.93277%16%
15Jelly bean3750940500%0%
16Lollipop3920.2980380%2%
17Honeycomb4083.2876.55620%45%
1-1000 / 1000

注意,当我们在 QCarousel 上使用 QResponsive 时,我们不会为它提供 height 属性,因为由 QResponsive 会负责。



<template>
  <div class="q-pa-md">
    <q-responsive :ratio="16/9" style="width: 500px; max-width: 100%;">
      <q-carousel
        swipeable
        animated
        arrows
        v-model="slide"
        infinite
      >
        <q-carousel-slide :name="1" img-src="https://cdn.quasar.dev/img/mountains.jpg" />
        <q-carousel-slide :name="2" img-src="https://cdn.quasar.dev/img/parallax1.jpg" />
        <q-carousel-slide :name="3" img-src="https://cdn.quasar.dev/img/parallax2.jpg" />
        <q-carousel-slide :name="4" img-src="https://cdn.quasar.dev/img/quasar.jpg" />

        <template v-slot:control>
          <q-carousel-control
            position="bottom"
            :offset="[16, 8]"
            class="text-white text-center rounded-borders"
            style="background: rgba(255, 255, 255, .2); padding: 4px 8px;"
          >
            Ratio 16:9
          </q-carousel-control>
        </template>
      </q-carousel>
    </q-responsive>
  </div>
</template>

最大高度

通过 CSS 类或内联样式设置的最大高度(或最大宽度等)将直接应用于 QResponsive 组件。请记住,您需要确保内容不会溢出容器。



<template>
  <div class="q-pa-md">
    <div class="row items-start q-gutter-md">
      <q-responsive :ratio="4/3" class="col" style="max-height: 100px">
        <q-card class="column">
          <q-img class="col" src="https://cdn.quasar.dev/img/parallax1.jpg" />

          <q-card-section>
            <div>Ratio 4:3, but max height of 100px</div>
          </q-card-section>
        </q-card>
      </q-responsive>
    </div>
  </div>
</template>

Ratio 4:3, but max height of 100px