Detailed teaching of Uniapp teaching value Match-Media component

The Match-Media component is a very useful component in Uniapp, which can automatically adjust the layout and style of the page according to different screen sizes and device types. On mobile devices, the Match-Media component can help us achieve responsive design, so that the page can present the best results on different devices.
The following is a detailed tutorial on using the Match-Media component:

  1. Add a reference to the Match-Media component in the pages.json file:
{
    
    
  "usingComponents": {
    
    
    "match-media": "/components/match-media/match-media"
  }
}
  1. In the page that needs to use the Match-Media component, add the label of the Match-Media component:
<match-media></match-media>
  1. In the Match-Media component, we can use the media attribute to define different media query conditions. For example, we can use the following code to define a media query for mobile devices:
<match-media media="(max-width: 768px)">
  <!-- 在这里添加针对移动设备的布局和样式 -->
</match-media>
  1. We can also use other properties of the Match-Media component to further control the layout and style of the page. For example, we can use the match attribute to specify the CSS selector that needs to be matched, use the class attribute to add additional style classes, use the style attribute to add inline styles, and so on. Here is a complete sample code demonstrating how to use the Match-Media component for responsive design:
<template>
  <view class="container">
    <match-media media="(max-width: 768px)" match=".mobile">
      <view class="header">
        <image class="logo" src="/static/logo-mobile.png"></image>
        <view class="menu">
          <text class="menu-item">Home</text>
          <text class="menu-item">About</text>
          <text class="menu-item">Contact</text>
        </view>
      </view>
    </match-media>
    <match-media media="(min-width: 769px)" match=".desktop">
      <view class="header">
        <image class="logo" src="/static/logo-desktop.png"></image>
        <view class="menu">
          <text class="menu-item">Home</text>
          <text class="menu-item">About</text>
          <text class="menu-item">Contact</text>
        </view>
      </view>
    </match-media>
    <view class="content">
      <text class="title">Welcome to my website!</text>
      <text class="subtitle">This is a demo of responsive design using Match-Media component.</text>
    </view>
  </view>
</template><style>
  .container {
      
      
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
  }
  .header {
      
      
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }
  .logo {
      
      
    width: 100px;
    height: 100px;
  }
  .menu {
      
      
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 50%;
  }
  .menu-item {
      
      
    font-size: 18px;
    font-weight: bold;
    color: #333;
    margin-right: 20px;
  }
  .content {
      
      
    text-align: center;
  }
  .title {
      
      
    font-size: 36px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
  }
  .subtitle {
      
      
    font-size: 24px;
    color: #666;
  }
  /* 移动设备样式 */
  .mobile .logo {
      
      
    width: 50px;
    height: 50px;
  }
  .mobile .menu {
      
      
    width: 70%;
  }
  .mobile .menu-item {
      
      
    font-size: 16px;
    margin-right: 10px;
  }
  /* 桌面设备样式 */
  .desktop .logo {
      
      
    width: 150px;
    height: 150px;
  }
  .desktop .menu {
      
      
    width: 30%;
  }
  .desktop .menu-item {
      
      
    font-size: 20px;
    margin-right: 30px;
  }
</style>

In this example, we use the Match-Media component to implement a simple responsive design. On mobile devices, the page will display a small logo and a simple menu, while on desktop devices, the page will display a large logo and a more complex menu. By using the Match-Media component, we can easily achieve this kind of responsive design, so that the page can present the best effect on different devices. Summary: The Match-Media component is a very practical component in Uniapp, which can help us achieve responsive design, so that the page can present the best results on different devices. By using the Match-Media component, we can easily define different media query conditions, control the layout and style of the page, and achieve a more flexible and adaptive page design.

Guess you like

Origin blog.csdn.net/qq_36901092/article/details/130921111