• 微信小程序基本组件使用介绍


    目录

    1、视图容器 view

    2、滚动视图容器 scroll-view

    3.轮播图 swiper&swiper-item

     4.文本组件text&富文本组件rich-text

    5.按钮组件button

    6.图片组件image


    1、视图容器 view

    • 普通视图区域
    • 类似于HTML中的div,是一个块级元素
    • 常用来实现页面的布局效果

      

    页面结构:

    1. <!--pages/view/view.wxml-->
    2. <view class="container1">
    3. <view>A</view>
    4. <view>B</view>
    5. <view>C</view>
    6. </view>

     页面样式:

    1. /* pages/view/view.wxss */
    2. .container1 view{
    3. width: 100px;/*盒子的宽度与高度*/
    4. height: 100px;
    5. text-align: center;/*居中显示*/
    6. line-height: 100px; /*文本的行间距*/
    7. }
    8. .container1 view:nth-child(1){
    9. background-color:lightcoral;/*设置子块的背景颜色*/
    10. }
    11. .container1 view:nth-child(2){
    12. background-color: lightgreen;
    13. }
    14. .container1 view:nth-child(3){
    15. background-color: magenta;
    16. }
    17. .container1{
    18. display: flex;/*盒子横向布局*/
    19. justify-content: space-around;/*分散布局*/
    20. }

    2、滚动视图容器 scroll-view

    • 可滚动的视图区域
    • 常用来实现滚动列表效果

     

     页面结构:

    1. <!--pages/view/view.wxml-->
    2. <!-- scroll-x 横向滚动 scroll-y 纵向滚动 -->
    3. <scroll-view class="container1" scroll-y>
    4. <view>A</view>
    5. <view>B</view>
    6. <view>C</view>
    7. </scroll-view>

    页面样式:

    1. /* pages/view/view.wxss */
    2. .container1 view{
    3. width: 100px;/*盒子的宽度与高度*/
    4. height: 100px;
    5. text-align: center;/*居中显示*/
    6. line-height: 100px; /*文本的行间距*/
    7. }
    8. .container1 view:nth-child(1){
    9. background-color:lightcoral;/*设置子块的背景颜色*/
    10. }
    11. .container1 view:nth-child(2){
    12. background-color: lightgreen;
    13. }
    14. .container1 view:nth-child(3){
    15. background-color: magenta;
    16. }
    17. .container1{
    18. /*包裹滚动列表的边框线及其宽度*/
    19. border :1px solid red;
    20. width: 100px;
    21. /*给其强制限定一个高度,超过该高度的部分将进行滚动显示*/
    22. height: 130px;
    23. }

    3.轮播图 swiper&swiper-item

    • 滑块视图容器
    • 可内置swiper-item组件
    • 一般用来实现图片的自动播放展示(这里以view块代替展示)

     页面结构:

    1. <!--pages/swiper/swiper.wxml-->
    2. <text>轮播图效果展示</text>
    3. <!-- 轮播图的结构 ,最外层为包裹性容器-->
    4. <swiper class="swiper-container" indicator-dots indicator-color="white" autoplay circular>
    5. <!-- 第一张轮播图 -->
    6. <swiper-item>
    7. <view class="item">A</view>
    8. </swiper-item>
    9. <!-- 第二张轮播图 -->
    10. <swiper-item>
    11. <view class="item">B</view>
    12. </swiper-item>
    13. <!-- 第三张轮播图 -->
    14. <swiper-item>
    15. <view class="item">C</view>
    16. </swiper-item>
    17. </swiper>

    页面样式:

    1. /* pages/swiper/swiper.wxss */
    2. .swiper-container{
    3. height: 150px;
    4. }
    5. .item{
    6. height: 100%;
    7. line-height: 150px;
    8. text-align: center;
    9. }
    10. swiper-item:nth-child(1) .item{
    11. background-color: lightblue;
    12. }
    13. swiper-item:nth-child(2) .item{
    14. background-color: lightgreen;
    15. }
    16. .swiper-item:nth-child(3) .item{
    17. background-color: lightseagreen;
    18. }

    一些其它常用属性

     4.文本组件text&富文本组件rich-text

    • text类似于HTML中的span标签,是一个行内元素
    • text可实现长按选中复制的功能(只能使用text)
    • rich-text可以通过nodes属性节点,把HTML字符串渲染成对应的UI结构

     页面结构:

    1. <!--文本text与富文本rich-text的演示-->
    2. <text>文本text:</text>
    3. <view>
    4. 手机号支持长按选中效果:
    5. <!-- selectable属性表示文本可长按被选中,且只有在text标签中才有效 -->
    6. <text selectable>1234567896</text>
    7. </view>
    8. <text>富文本rich-text:</text>
    9. <rich-text nodes="<h1 style='color:red;'>hello world</h1>"></rich-text>

    属性

     

    5.按钮组件button

    • 其功能比HTML中的button丰富
    • 可以通过open-type属性可以调用微信提供的各种功能(客服、转发、获取用户的授权、获取用户信息、打开定位服务等)

     页面结构:

    1. <view>----------通过type属性指定按钮类型---------</view>
    2. <button>默认按钮</button>
    3. <button type="primary">主色调按钮</button>
    4. <button type="warn">警告按钮</button>
    5. <view>----------设置size=mini变成小尺寸按钮---------</view>
    6. <button size="mini">默认按钮</button>
    7. <button type="primary" size="mini">主色调按钮</button>
    8. <button type="warn" size="mini">警告按钮</button>
    9. <view>----------设置plain属性成为“镂空”型按钮---------</view>
    10. <button size="mini" plain="">默认按钮</button>
    11. <button type="primary" size="mini" plain>主色调按钮</button>
    12. <button type="warn" size="mini" plain="">警告按钮</button>

    属性

     

    6.图片组件image

    • 用于展示图片元素
    • 组件默认的宽度约为300px,高度约为240px

     页面结构:

    1. <!--pages/image/image.wxml-->
    2. <text>添加图片演示</text>
    3. <!-- heightFix高度固定,宽度自适应填满边框区域 -->
    4. <image src="/images/2.jfif" mode="heightFix"></image>
    5. <!-- widthFix宽度固定,高度自适应填满边框区域 -->
    6. <image src="/images/2.jfif" mode="widthFix"></image>

    属性

     

    image mode重要属性

     

  • 相关阅读:
    Web Components详解-Shadow DOM样式控制
    java——类作为成员变量类型
    tcpdump常用命令
    SpringBoot热部署
    常见的几种池化操作:MaxPool2d/AdaptiveMaxPool2d/AvgPool2d/AdaptiveAvgPool2d...(Pytorch)
    CMake
    什么是应变能力?如何提高应变能力?
    vue-pdf结合alloyfinger手势缩放旋转上下翻页pdf文件
    .NET 纯原生实现 Cron 定时任务执行,未依赖第三方组件 (Timer 优化版)
    O2OA(翱途)开发平台 V8.2已发布,更安全、更高效、更开放
  • 原文地址:https://blog.csdn.net/qq_52487066/article/details/125608945