微信小程序笔记1

第一步:创建项目与项目初始化

1.创建项目:

 

2.项目初始化

 重置app.js中的代码。

 删除 app.json 中 pages 下的 "rendererOptions" 以及 "componentFramework" 字段。

 重置 app.wxss中的代码。

删除app.json中pages 下的"pages/logs/logs"路径,同时删除pages/logs ⽂件夹。

删除 components 中的⾃定义组件。

重置 pages/index ⽂件夹下的 index.js 、 index.wxss 、 index.html 以及 index.json ⽂件。

更新 utils 下 util.js 的⽂件名为 formatTime.js。

二.自定义构建

首先在project.config.json配置miniprogramRoot选项,指定小程目录。

 然后配置project.config.json的setting.packNpmManually为true.

初始化目录,在终端中打开。

 

安装vant,然后进行npm构建,测试是否能够正常vant构建成功。 

 三.构建项目页面

assets文件导入。

配置app.json文件。

{
  "pages": [
    "pages/cart/cart",
    "pages/category/category",
    "pages/index/index",
    "pages/my/my",
    "pages/index/banner/banner",
    "pages/index/entrance/entrance"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "慕尚花坊",
    "navigationBarBackgroundColor": "#FF734C"
  },
  "tabBar": {
    "color": "#252933",
    "selectedColor": "#FF734C",
    "backgroundColor": "#ffffff",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "assets/tabbar/index.png",
        "selectedIconPath": "assets/tabbar/index-active.png"
      },
      {
        "pagePath": "pages/category/category",
        "text": "分类",
        "iconPath": "assets/tabbar/cate.png",
        "selectedIconPath": "assets/tabbar/cate-active.png"
      },
      {
        "pagePath": "pages/cart/cart",
        "text": "购物车",
        "iconPath": "assets/tabbar/cart.png",
        "selectedIconPath": "assets/tabbar/cart-active.png"
      },
      {
        "pagePath": "pages/my/my",
        "text": "我的",
        "iconPath": "assets/tabbar/my.png",
        "selectedIconPath": "assets/tabbar/my-active.png"
      }
    ]
  },
  "sitemapLocation": "sitemap.json",
  "lazyCodeLoading": "requiredComponents"
  }
}

 四.首页

1.首页结构

<view class="index-container">
  <!-- 首页背景图 -->
  <view class="window-bgc"></view>
  <!-- 页面主体区域 -->
  <view class="container">

    <!-- 轮播图区域 -->
    <banner/>
    <!-- 导航区域 -->
    <entrance/>
    <!-- 广告区域 -->
  <view class="adver">
    <view class="adver-left">
      <navigator url="">
        <image src="../../assets/images/love.jpg" mode="widthFix"/>
      </navigator>
    </view>
    <view class="adver-right">
      <view>
        <navigator url="">
          <image src="../../assets/images/elder.jpg" mode="widthFix"/>
        </navigator>
      </view>
      <view>
        <navigator url="">
          <image src="../../assets/images/friend.jpg" mode="widthFix"/>
        </navigator>
      </view>
    </view>
  </view>
    <!-- 商品列表 -->
    <goods-list title="猜你喜欢"></goods-list>
    <goods-list title="人气推荐"></goods-list>
  </view>
</view>

 2.首页样式

/**index.wxss**/
.index-container {
  // 首页背景图
  .window-bgc{
    height: 200rpx;
    position:absolute;
    width: 100%;
    background-color: #f3514f;
    border-radius: 0 0 25% 25%;
  }
  .adver {
    display: flex;
    margin: 0 20rpx;
    width: 96%;
    padding: 18rpx;
    box-sizing: border-box;
    background-color: white;
    border-radius: 18rpx;
    .adver-left{
      width: 50%;
      padding: 8rpx 8rpx 0rpx 8rpx;
    }
    .adver-right{
      width: 50%;
      padding: 8rpx 8rpx 0rpx 8rpx;
    }
    // 选中第二张图片
    view:laat-child{
      padding-top: 10rpx;
    }
    image{
      width: 100%;
    }
  }
}

 

 3.banner组件

创建banner组件

创建banner组件结构

banner.js

 

// banner.json 

{

  "component": true,

  "usingComponents": {}

}

 banner.scss

 

 banner.wxml

//  index.json :引入bann.json组件

{

  "usingComponents": {

    "banner": "./banner/banner"

  }

//  index.html :将轮播图放入合适位置

 <!-- 轮播图区域 -->

    <banner/>

 

 4.entrance组件

创建entrance组件

完成entrance组件

// entrance.html

 entrance.js

 entrance.json

 entrance.scss

 // index.wxml

<!-- 导航区域 -->

    <entrance/>

 // index.json

{

  "usingComponents": {

    "banner": "./banner/banner",

    "entrance": "./entrance/entrance"

  }

}

 

 

flex弹性布局 

 5.广告区域

// index.wxml

 <!-- 广告区域 -->

  <view class="adver">

    <view class="adver-left">

      <navigator url="">

        <image src="../../assets/images/love.jpg" mode="widthFix"/>

      </navigator>

    </view>

    <view class="adver-right">

      <view>

        <navigator url="">

          <image src="../../assets/images/elder.jpg" mode="widthFix"/>

        </navigator>

      </view>

      <view>

        <navigator url="">

          <image src="../../assets/images/friend.jpg" mode="widthFix"/>

        </navigator>

      </view>

    </view>

广告区域的样式

// index.scss

.adver {

    display: flex;

    margin: 0 20rpx;

    width: 96%;

    padding: 18rpx;

    box-sizing: border-box;

    background-color: white;

    border-radius: 18rpx;

    .adver-left{

      width: 50%;

      padding: 8rpx 8rpx 0rpx 8rpx;

    }

    .adver-right{

      width: 50%;

      padding: 8rpx 8rpx 0rpx 8rpx;

    }

    // 选中第二张图片

    view:laat-child{

      padding-top: 10rpx;

    }

    image{

      width: 100%;

    }

  }

 

 6.注册goods-card和goods-list组件

创建文件夹components,将goods-card和goods-list组件放入文件夹中。

1.完成goods-card组件

// goods-card

Component({

  /**

   * 组建的属性列表

   */

  properties:{

    // 每一项商品的数据

    goodItem: {

      type:Object,

      value: {}

    }

  },

  /**

   * 组件的初始数据

   */

  data: {},

  /**

   * 组建的方法列表

   */

  methods: {}

})

 // goods-card.json

{

  "component": true,

  "usingComponents": {}

}

// goods.card.scss

/* components/goods-card/goods-card.wxss */

.goods_card_container{

  width: 350rpx;

  margin-top: 18rpx;

  background-color: #fff;

  border-radius: 18rpx;

  .goods_img{

    width: 100%;

    max-height: 360rpx;

    border-top-left-radius: 18rpx;

    border-top-right-radius: 18rpx;

  }

  .goods_item_info{

    // 设置弹性布局

    display: flex;

    // 更改主轴方向为纵轴

    flex-direction: column;

    padding: 10rpx 20rpx;

    .goods_item_info_name {

      // 设置字体粗细

      font-weight: bolder;

      font-size: 30rpx;

      line-height: 20px;

      // 溢出隐藏

      overflow: hidden;

      // 溢出之后的文本

      text-overflow: ellipsis;

      // 超过一行就算溢出

      white-space: nowrap;

    }

    .goods_item_info_promo {

      margin: 10rpx 0;

      padding-top: 20rpx;

      padding-bottom: 3px;

      font-size: 12px;

      color: #71797f;

      // 溢出隐藏

      overflow: hidden;

      // 溢出之后的文本

      text-overflow: ellipsis;

      // 超过一行就算溢出

      white-space: nowrap;

    }

  }

  .goods_item_info_bottom{

    // 设置弹性布局

    // 将块级元素一行显示

    display: flex;

    line-height: 50rpx;

    margin-top: 20rpx;

    .goods_item_info_price{

      font-size: 30rpx;

      font-weight: bold;

      color: #f3514f;

      .text{

        font-size: 24rpx;

        padding-right: 4rpx;

        font-weight: normal;

      }

    }

    .goods_item_info_origin_price{

      // flex设置为1

      // 则该元素拥有所有的剩余部分

      flex: 1;

      font-size: 20rpx;

      color: #71797f;

      text-decoration-line: line-through;

      margin-left: 6%;

      margin-top: 4rpx;

      .text{

        font-size: 20rpx;

      }

    }

    .goods_image {

      width: 48rpx;

      height: 48rpx;

    }

  }

}

 // goods-card.wxml

<!--components/goods-card/goods-card.wxml-->

<view class="goods_card_container">

<!-- 图片 -->

  <navigator class="navigator_nav" url="/pages/goods/detail/detail?goodsId={{goodItem.id}}">

    <image src="../../assets/images/floor.jpg" mode="widthFix" class="goods_img"/>

  <!-- 详细信息 -->

  <view class="goods_item_info">

    <!-- 商品名称 -->

    <text class="goods_item_info_name">亲爱的/情人节网红款/19枝</text>

    <!-- 商品描述 -->

    <text class="goods_item_info_promo">情⼈节新品情⼈节新品情⼈节新品情⼈节新品</text>

    <!-- 商品价格 -->

    <view class="goods_item_info_bottom">

      <view class="goods_item_info_price">

        <text class="text">¥</text>399

      </view>

      <view class="goods_item_info_origin_price">

        <text class="text">¥</text>1{{goodItem.marketPrice}}

      </view>

      <!-- 加入购物车图片 -->

      <view class="goods_item_info_btn">

        <image class="goods_image" src="../../assets/images/buybtn.png" mode=""/>

      </view>

    </view>

  </view>

  </navigator>

</view>

2.完成goods-list组件 

// goods-list.js

Component({

  /**

  * 组件的属性列表

  */

  properties: {

    // 列表标题

    title: {

     type: String,

     value: ''

    },

   // 传递的列表数据

   list: {

   type: Array,

   value: [{}, {}, {}]

   }

   },

   /**

   * 组件的初始数据

   */

   data: {}, 

     /**

     * 组件的⽅法列表

     */

    methods: {}

})

 // goods-list.json

{

  "component": true,

  "usingComponents": {

    "goods-card":"/components/goods-card/goods-card"// 引入goods-card组件

  }

}

 // goods-list.scss

.goods_container{

  width: 95%;

  margin: 0 auto;

  .goods_title{

    // 设置文本居中

    text-align: center;

    font-size: 48rpx;

    line-height: 52rpx;

    font-weight: bold;

    color: #232628;

    padding: 20rpx 0 0rpx 0rpx;

  }

  .goods_card_list{

    display: flex;

    // 设置换行

    flex-wrap: wrap;

    // 设置元素在主轴上的排列方式

    justify-content: space-between;

    .goods_card_list::after{

      content: '';

      width: 350rpx;

    }

  }

  .goods_more_btn{

    height: 40px;

    width: 95%;

    background-color: white;

    border-radius: 20px;

    margin: 0 auto;

    // 以下三句话设置元素垂直居中显示

    text-align: center;

    display: block;

    color: rgba(35,38,40,0.7);

  }

  .goods_more{

    margin: 20rpx 0;

  }

}

 // goods-list.wxml

<!--components/goods-list/goods-list.wxml-->

<view class="goods_container">

  <!-- 标题 -->

  <view class="goods_title">{{title}}</view>

  <!-- 列表区域 -->

  <view class="goods_card_list">

    <block wx:for="{{ 3 }}" wx:key="index">

      <goods-card/>

    </block>

  </view>

  <!-- 查看更多 -->

  <view class="goods_more">

    <navigator url="" class="goods_more_btn" url="/pages/goods/list/list" hover-class="navigator-hover" open-type="navigate">

      查看更多

    </navigator>

  </view>

</view>

// index.json

{

  "usingComponents": {

    "banner": "./banner/banner",

    "entrance": "./entrance/entrance",

    "goods-list": "../../components/goods-list/goods-list"

  }

}

 // index.wxml

<!-- 商品列表 -->

    <goods-list title="猜你喜欢"></goods-list>

    <goods-list title="人气推荐"></goods-list>

 

五. 分类页面

// category.scss

/* pages/category/category.wxss */

.category-container{

  display: flex;

  // 左侧⼀级分类

  .category-left-view {

    width: 220rpx;

    height: 100vh;

    background-color: white;

    .left-view-item {

      // 文本间隔

      line-height: 100rpx;

      // 文本的排列方式

      text-align: center;

      // 文字大小

      font-size: 30rpx;

    }

    .active {

      background-color: #ffffff;

      position: relative;

      color: #f3514f;

      // &表示当前标签

      // :::before是伪元素选择器

      // 在指定元素前添加内容

      &::before {

        content: " ";

        display: block;

        width: 6rpx;

        height: 66rpx;

        background-color: #f3514f;

        position: absolute;

        // 沿着Y轴平移

        transform: translateY(25%);

      }

    }

  }

  // 右侧⼆级分类

  .category-right-view {

    width: 100%;

    height: 100vh;

    flex-wrap: wrap;

    background: #fff !important;

    margin-left: 8rpx;

    &::before{

      content: " ";

      display: block;

      width: 6rpx;

      height: 100vh;

      background-color: black;

      position: absolute;

    }

    .right-view-item {

      width: 33%;

      float: left;

      margin-top: 30rpx;

      .navigator{

        display: flex;

        // 设置纵轴方向上排列

        flex-direction: column;

        align-items: center;

      }

      image{

        width: 90rpx;

        height: 90rpx;

      }

      text{

        font-size: 26rpx;

        margin-top: 18rpx;

      }

    }

  }

}

 // category.wxml

<!--pages/category/category.wxml-->

<view>

  <view class="category-container">

    <!-- 左侧的滚动视图区域 -->

    <scroll-view class="category-left-view" scroll-y>

    <!-- active:被选中的内容 -->

      <view class="left-view-item active">爱礼精选</view>

      <view class="left-view-item">鲜花玫瑰</view>

      <view class="left-view-item">永生玫瑰</view>

      <view class="left-view-item">玫瑰珠宝</view>

    </scroll-view>

    <!-- 右侧的滚动视图区域  -->

    <scroll-view class="category-right-view" scroll-y>

    <!-- 二级分类 -->

      <view class="test">

      <view wx:for="{{ 10 }}" wx:key="index" class="right-view-item">

        <navigator class="navigator" url="/pages/goods/list/list?category2Id=1">

          <image class="" src="../../assets/images/cate-1.png" mode=""/>

          <text class="goods_item_name">真情告白</text>

        </navigator>

      </view>

      </view>

    </scroll-view>

  </view>

</view>

 

元素定位 

 伪元素选择权

  • 37
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值