三、图片、字体图标、内外边距、边框、圆角、背景图片

1. Image alt&objectFit

@Entry
@Component
struct Index {
  build() {
    Column(){
      Image('https://www.itheima.com/images/logo.png')
        .width(100)
        .aspectRatio(1)
        .backgroundColor(Color.Pink)
        // 图片占位图 习惯:占位图用本地图片
        .alt($r('app.media.startIcon'))
        // 图片填充方式
        .objectFit(ImageFit.Contain)
    }
  }
}

2. 案例-QQ音乐卡片

@Entry
@Component
struct Index {
  build() {
    Column(){
      // 案例-QQ音乐-卡片
      Column(){
        Text('我的年度歌手榜')
          .height(50)
          .fontSize(20)
          .fontColor(Color.Grey)
          // .backgroundColor(Color.Brown)
        Text('周杰伦')
          .height(50)
          .fontSize(32)
          .fontWeight(600)
        Image($r('app.media.zhoujielun'))
          .width(100)
          .margin({bottom:10})
        Text('真爱就是循环一千遍')
          .height(50)
          .fontSize(20)
          .fontColor(Color.Grey)
      }
      // .width(250/)
      // .height(5)
      .padding(20)
      .backgroundColor(Color.White)
      /*.shadow({
        radius:10,
        color:'rgba(0,0,0,0.5)'
      })*/
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#f5f8f7')
  }
}

3. iconfont

@Entry
@Component
struct Index {
  build() {
    Column(){
      Image($r('app.media.ic_contacts_delete'))
        .width(50)
        .fillColor(Color.Pink)
    }
    .width('100%')
    .height('100%')
    .backgroundColor('f5f8f7')
  }
}

4. padding&margin

@Entry
@Component
struct Index {
  build() {
    Column(){
      Text('内边距test')
        // 加宽高后Text不随内边距大小而变化
        .width(100)
        .height(100)
        .backgroundColor(Color.Orange)
        // 内边距四周都是20vp
        .padding(20)
        // 内边距四周不同,使用对象,属性之间要加逗号
        .padding({
          top:20,
          left:10
        })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('f5f8f7')
  }
}

5. border

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('边框border test')
        .width(200)
        .height(100)
        .backgroundColor(Color.Pink)
        .border({
          width:{top:2,bottom:2},
          color:{top:Color.Red, bottom:Color.Blue},
          style:{bottom:BorderStyle.Dotted}
        })
    }
    .padding(10)
  }
}

6. borderRadius

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('borderRadius test')
        .width(200)
        .height(100)
        .backgroundColor(Color.Pink)
        // 边框圆角
        .borderRadius({
          topLeft:10,
          topRight:20
        })

      // 正圆
      Text('正圆 test')
        .width(100)
        .height(100)
        .backgroundColor(Color.Pink)
          // 边框圆角
        .borderRadius(50)
        .borderRadius('50%')

      Text('胶囊 test')
        .width(200)
        .height(100)
        .backgroundColor(Color.Pink)
          // 边框圆角
        .borderRadius(50)

      Image($r('app.media.zhoujielun'))
        .width(100)
        //   图片设置圆角50%不生效
        // .borderRadius('50%')
        .borderRadius({
          bottomLeft:20,
          bottomRight:30
        })
    }
    .padding(10)
  }
}

7. 案例-小红书商品

@Entry
@Component
struct Index {
  build() {
    Column() {
    //   案例-小红书商品
      Column(){
        Image($r('app.media.pics'))
          .width('100%')
          .aspectRatio(1.1)
          // .height(200)
          .borderRadius({
            topLeft:10,
            topRight:10
          })
        Text('磁力片。工厂直发磁力王。100片,尺寸7.5cm 可兼容其他品牌')
          .fontSize(14)
          .lineHeight(20)
          // .width('100%')
          .maxLines(2)
          .textOverflow({overflow:TextOverflow.Ellipsis})
          .padding(5)
        Text('¥ 87')
          .width('100%')
          // .textAlign(TextAlign.Start)
          .fontColor(Color.Red)
          .padding({
            left:5,
            bottom:10
          })
      }
      .width('50%')
      // .height(300)
      .borderRadius(10)
      // .border({ width:1 })
      .backgroundColor('#fff')

    }
    .width('100%')
    .height('100%')
    // .padding(10)
    .backgroundColor('#EAEAE7')
  }
}

8. backgroundImage

@Entry
@Component
struct Index {
  build() {
    Column() {
      // 背景图平铺
      Text('文字')
        .width(200)
        .height(200)
        .backgroundColor(Color.Pink)
        .backgroundImage($r('app.media.startIcon'),ImageRepeat.X)
        // 背景图尺寸
        // 开发中,大多数情况图的比例和组件比例相同
        .backgroundImageSize({width:100})
        .backgroundImageSize(ImageSize.Cover)
    }
    .width('100%')
    .height('100%')
    // .padding(10)
    .backgroundColor('#EAEAE7')
  }
}

9. 案例-华为商品卡片

@Entry
@Component
struct Index {
  build() {
    Column() {
    //   华为卡片
      Column(){
        Text('漏洞处理流程')
          .fontColor(Color.White)
          .fontSize(30)
          .fontWeight(600)
          .margin({bottom:20})
        Text('华为PSIRT按照漏洞处理流程对上报的潜在漏洞进行处理')
          .fontColor(Color.White)
          .margin({bottom:10})
        Text('了解更多')
          .padding({
            top:10,
            bottom:10,
            left:30,
            right:30
          })
          .fontColor(Color.White)
          .border({
            width:2,
            color:Color.White,
          })
      }
      .width(300)
      .height(200)
      .padding({
        top:30,
        left:20,
        right:20
      })
      // .backgroundColor(Color.Brown)
      .backgroundImage($r('app.media.hw_bg'))
      .backgroundImageSize(ImageSize.Cover)

    }
    .width('100%')
    .height('100%')
    // .padding(10)
    .backgroundColor('#EAEAE7')
  }
}

10. 作业1-京东产品卡片

@Entry
@Component
struct Index {
  build() {
    Column() {
    //   京东产品卡片
      Column(){
        Image($r('app.media.hw_line'))
          .width('100%')
        Text('华为原装6A数据线 USB Type-A转USB Type-C/1m线长/支持66W(11V6A)充电 白色CC790')
          .fontSize(14)
          .lineHeight(20)
          .margin({
            top:8,
            left:5,
            right:5,
            bottom:8
          })
          .maxLines(2)
          .textOverflow({overflow:TextOverflow.Ellipsis})

        Row({space:5}){
          Image($r('app.media.jd'))
            .width(12)
          Text('7天最低价')
            .fontColor('#ff2078')
            .fontSize(12)
        }
        .width('100%')
        .padding({left:5,right:5,bottom:8})

        Text('¥ 29.90')
          .fontColor('#ff4142')
          .width('100%')
          .padding({left:5,bottom:8})
        Row(){
          Text('自营')
            .fontSize(14)
            .fontColor(Color.White)
            .backgroundColor('#ff6465')
            .padding(5)
            .borderRadius(5)
        }
        .width('100%')
        .padding({left:5})
        .margin({bottom:10})
      }
      .width('50%')
      // .height(300)
      .backgroundColor(Color.White)
      .borderRadius({bottomLeft:10,bottomRight:10})
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#EAEAE7')
  }
}

11. 作业2_京东APP提示

@Entry
@Component
struct Index {
  build() {
    Column() {
      // 京东app提示
      Row(){
        Row(){
          Image($r('app.media.close'))
            .width(12)
            .margin({left:10,right:10})
          Image($r('app.media.jd_logo'))
            .width(35)
            .margin({right:20})
          Text('打开京东APP,实惠又轻松')
            .fontColor(Color.White)
            .fontSize(14)
        }
        .width(360-88)
        .height('100%')
        .backgroundColor('#333333')

        Text('立即打卡')
          .textAlign(TextAlign.Center)
          .fontSize(14)
          .fontColor(Color.White)
          .width(88)
          .height('100%')
          .backgroundColor('#F63514')
      }
      .height(50)
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#EAEAE7')
  }
}

12. 作业3_微信发现界面

@Entry
@Component
struct Index {
  build() {
    Column() {
      // 微信发现界面
      Column(){
        // 朋友圈
        Row(){
          Image($r('app.media.pyq'))
            .width(40)
            .margin({left:20,right:12,top:10,bottom:10})
          Text('朋友圈')
            .fontSize(20)
        }
        .width('100%')
      //   视频号
        Row(){
          Image($r('app.media.sph'))
            .width(40)
            .margin({left:20,right:12,top:10,bottom:10})
          Text('视频号')
            .fontSize(20)
        }
        .width('100%')
        // 扫一扫
        Row(){
          Image($r('app.media.sys'))
            .width(40)
            .margin({left:20,right:12,top:10,bottom:10})
          Text('扫一扫')
            .fontSize(20)
        }
        .width('100%')
        // 摇一摇
        Row(){
          Image($r('app.media.yyy'))
            .width(40)
            .margin({left:20,right:12,top:10,bottom:10})
          Text('摇一摇')
            .fontSize(20)
        }
        .width('100%')
        // 小程序
        Row(){
          Image($r('app.media.xcx'))
            .fillColor('#a4579d')
            .width(40)
            .margin({left:20,right:12,top:10,bottom:10})
          Text('小程序')
            .fontSize(20)
        }
        .width('100%')
      }
      .backgroundColor('#FFFFFF')

    }
    .padding(10)
    .width('100%')
    .height('100%')
    .backgroundColor('#EAEAE7')
  }
}

13. 作业4_腾讯新闻简介

@Entry
@Component
struct Index {
  build() {
    Column() {
    //   腾讯新闻简介
      Column(){
        // 标题
        Text('华为新工艺可造5nm芯片,耶伦访华,给她准备了大惊喜?')
          .lineHeight(28)
          .fontSize(18)
          .fontColor('#444444')

        // 图片
        Row(){
          Image($r('app.media.hw0'))
            .width(102)
            .margin({left:3,right:3,top:5,bottom:5})
          Image($r('app.media.hw1'))
            .width(102)
            .margin({left:3,right:3,top:5,bottom:5})
          Image($r('app.media.hw2'))
            .width(102)
            .margin({left:3,right:3,top:5,bottom:5})
        }

        // 新闻作者及时间
        Text('大图鉴 83评 6小时前')
          .width('100%')
          .fontColor('#999999')
          .margin({left:10,top:5})
      }
      .backgroundColor('#FFFFFF')
      .padding(10)
      .border({
        width:{top:1,bottom:1},
        color:'#f1f1f1'
      })

    }
    // .padding(10)
    .width('100%')
    .height('100%')
    // .backgroundColor('#EAEAE7')
  }
}

14. 预习_点击事件

onClick&if-else

@Entry
@Component
struct Index {
  /*思路:分析需要几个可以数据变化UI也变化的数据
  1.准备状态
  2.使用状态
  3.写点击事件修改数据
   * */

  // 购买数量
  @State
  num: number = 0
  // 现价
  @State
  price: number = 20.2
  // 原价
  @State
  o_price: number = 40.4

  build() {
    Column() {
      Column() {
        // 产品
        Row({ space: 10 }) {
          // 图片
          Image($r('app.media.product1'))
            .width(100)
            .borderRadius(8)
          // 文字
          Column({ space: 10 }) {
            Column({ space: 6 }) {
              Text('冲销量1000ml缤纷八果水果捞')
                .lineHeight(20)
                .fontSize(14)
              Text('含1份折扣商品')
                .fontSize(12)
                .fontColor('#7f7f7f')
            }
            .width('100%')
            .alignItems(HorizontalAlign.Start)

            Row() {
              // 价格
              Row({ space: 5 }) {
                Text() {
                  Span('¥')
                    .fontSize(14)
                  Span(this.price.toString())
                    .fontSize(18)
                }
                .fontColor('#ff4000')

                Text() {
                  Span('¥')
                  Span(this.o_price.toString())
                }
                .fontSize(14)
                .fontColor('#999')
                .decoration({ type: TextDecorationType.LineThrough, color: '#999' })
              }

              // 加减
              Row() {
                Text('-')
                  .width(22)
                  .height(22)
                  .border({ width: 1, color: '#e1e1e1', radius: { topLeft: 5, bottomLeft: 5 } })
                  .textAlign(TextAlign.Center)
                  .fontWeight(700)
                  .onClick(() => {
                    // this.num--
                    if (this.num > 0) {
                      this.num--
                    } else {
                      AlertDialog.show({ message: '购物车已经空啦,不能再减了' })
                    }
                  })
                Text(this.num.toString())
                  .height(22)
                  .border({ width: { top: 1, bottom: 1 }, color: '#e1e1e1' })
                  .padding({ left: 10, right: 10 })
                  .fontSize(14)
                Text('+')
                  .width(22)
                  .height(22)
                  .border({ width: 1, color: '#e1e1e1', radius: { topRight: 5, bottomRight: 5 } })
                  .textAlign(TextAlign.Center)
                  .fontWeight(700)
                  .onClick(() => {
                    this.num++
                  })
              }
            }
            .width('100%')
            .justifyContent(FlexAlign.SpaceBetween)
          }
          .height(75)
          .layoutWeight(1)
          .justifyContent(FlexAlign.SpaceBetween)
        }
        .width('100%')
        .alignItems(VerticalAlign.Top)
        .padding(20)

        // 结算
        Row({ space: 10 }) {
          // 价格
          Column({ space: 5 }) {
            Text() {
              // 斜引
              Span(`已选 ${this.num} 件,`)// Span('已选' + this.num + '件,')
                .fontColor('#848484')
              Span('合计:')
              Span('¥')
                .fontColor('#fd4104')
              Span((this.price * this.num).toFixed(2))
                .fontColor('#fd4104')
                .fontSize(16)
            }
            .fontSize(14)

            // Text('共减¥20.2')
            Text(`共减¥${((this.o_price - this.price) *
            this.num).toFixed(2)}`)// Text('共减¥' + ((this.o_price-this.price)*this.num).toFixed(2))
              .fontColor('#fd4104')
              .fontSize(12)
          }
          .alignItems(HorizontalAlign.End)

          // 结算按钮
          Button('结算外卖')
            .width(110)
            .height(40)
            .backgroundColor('#fed70e')
            .fontColor('#564200')
            .fontSize(16)
            .fontWeight(600)
        }
        .width('100%')
        .height(70)
        .backgroundColor('#fff')
        .position({ x: 0, y: '100%' })
        .translate({ y: '-100%' })
        .padding({ left: 20, right: 20 })
        .justifyContent(FlexAlign.End)
      }
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#f3f3f3')
  }
}

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自行设计轮播图</title> <style type="text/css"> /*标题样式*/ /* 清除所有元素默认内外 */ * { margin: 0; padding: 0; } /* 标题样式:居中显示,字体大小25px */ p { text-align: center; font-size: 25px; } /* 轮播图容器样式: - 宽700px、高320px - 水平居中 - 相对定位(作为子元素绝对定位的参考) - 隐藏溢出内容(超出容器的图片不显示) */ .imgBox { width: 700px; height: 320px; margin: 0 auto; position: relative; text-align: center; overflow: hidden; } /* 轮播图片样式:固定宽高,水平居中 */ .imgBox img { width: 700px; height: 320px; margin: 0 auto; } /* 图片列表样式:去除默认列表标记 */ .box {list-style-type: none;} /* 初始显示第一张图片,其余隐藏 */ .img1 { display: block; } .img2, .img3, .img4, .img5 { display: none; } /* 左箭头样式: - 使用背景图(left-right.jpg) - 定位在容器左侧中间位置 - 0px -80px:设置背景图片的位置,其中: 第一个值0px是水平方向位置(离元素左缘 0 像素) 第二个值-80px是垂直方向位置(离元素上缘向上偏移 80 像素) - 层级1000(保证在图片上方显示) - 默认透明度0.2 */ #prev { width: 95px; height: 95px; background: url(image/left-right.jpg) no-repeat 0px -80px; position: absolute; top: 115px; left: 0px; z-index: 1000; opacity: 0.2; } /* -165px -80px:设置背景图片在元素中的位置,其中: 第一个值 -165px 是水平方向的偏移量(负值表示图片向左移动 165 像素); 第二个值 -80px 是垂直方向的偏移量(负值表示图片向上移动 80 像素)。 */ #next { width: 95px; height: 95px; /* background: url(image/left-right.jpg) no-repeat 0px -80px; */ background: url(image/left-right.jpg) no-repeat -165px -80px; position: absolute; top: 115px; right: 0px; z-index: 1000; opacity: 0.2; } /* 箭头 hover 效果:透明度提升至0.7 */ #prev:hover,#next:hover {opacity: 0.7;} /* 圆点按钮容器样式: - 定位在容器底部中间 - 去除默认列表样式 */ #circlebutton { position: absolute; bottom: 20px; left: 260px; list-style-type: none; text-align: center; } /* 圆点按钮列表项:水平排列,间10px */ #circlebutton li { margin-left: 10px; float: left; } /* 设置圆点按钮样式,圆角边框 */ /* 圆点按钮样式: - 宽高20px的圆形(border-radius:10px) - 灰色背景 - 鼠标指针变为手型 */ #circlebutton li div { width: 20px; height: 20px; background: #DDDDDD; border-radius: 10px; cursor: pointer; text-align: center; vertical-align: middle; } </style> </head> <body> <p>简易轮播图设计</p> <div class="imgBox"> <!-- 轮播图箭头事件处理 --> <div id="prev"></div> <div id="next"></div> <!-- 图像轮播区 --> <ul class="box"> <li><img class="img-slide img1" src="image/s1.jpg" alt="1"></li> <li><img class="img-slide img2" src="image/s2.jpg" alt="2"></li> <li><img class="img-slide img3" src="image/s3.jpg" alt="3"></li> <li><img class="img-slide img4" src="image/s4.jpg" alt="4"></li> <li><img class="img-slide img5" src="image/s5.jpg" alt="5"></li> </ul> <!-- 数字与图像同步切换 --> <ul id="circlebutton"> <li> <div class="divEle" style="background: #FF0000;">1</div> </li> <li> <div class="divEle">2</div> </li> <li> <div class="divEle">3</div> </li> <li> <div class="divEle">4</div> </li> <li> <div class="divEle">5</div> </li> </ul> </div> <script type="text/javascript"> // 获取页面上相关元素 // 圆点按钮鼠标悬停事件:切换到对应图片 // 自动轮播函数:切换到下一张图片 //设置定时器,每隔3秒切换一张图片 // 设置每个图像的默认样式为不显示,将指定index序号的图像设置为block // 左箭头点击事件:切换到上一张 // 右箭头点击事件:切换到下一张 // 箭头鼠标悬停时:停止自动轮播 // 箭头鼠标移出时:恢复自动轮播 </script> </body> </html> 基础要求: 页面结构搭建 按照示例格式创建 HTML 文件,包含标题、轮播图容器、图片列表、控制按钮(左右箭头)和指示器(圆点按钮) 确保 HTML 结构语义化,合理使用 div、ul、li 等标签 样式设计: 清除默认内外(使用* {margin: 0; padding: 0;}) 设置轮播图容器固定尺寸(参考 700px×320px),并使其居中显示 配置图片样式,确保图片填充轮播容器 设计左右箭头按钮:固定定位在轮播图两侧,设置背景图片或背景色,添加 hover 效果(透明度变化) 设计圆点指示器:固定在轮播图底部居中位置,设置圆形样式,当前激活项使用不同颜色(参考红色 #FF0000,默认灰色 #DDDDDD) 功能实现: 初始状态显示第一张图片,第一个圆点为激活状态 实现自动轮播功能:每隔 3 秒自动切换到下一张图片,最后一张切换后回到第一张 实现左右箭头控制:点击左箭头显示上一张图片,点击右箭头显示下一张图片 实现圆点指示器交互:鼠标悬停在圆点上时,切换到对应图片 实现暂停 / 恢复功能:鼠标悬停在箭头按钮上时暂停自动轮播,鼠标离开后恢复自动轮播,仅保留原有注释,不需要额外注释
12-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值