微信小程序绝对定位和相对定位

 时间:2026-02-12 05:43:32

1、打开开发者工具,在项目的pages页面新建mypage文件夹,在文件夹内新建

page,名为mypage,并将mypage设为第一页面

微信小程序绝对定位和相对定位

2、在mypage.wxml中写代码如下:

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

<view class='container1'>

<view class='item'>

1

</view>

<view class='item2'>

2

</view>

<view class='item'>

3

</view>

<view class='item'>

4

</view>

</view>

在mypage.wxss中写代码如下:

/* pages/mypage/mypage.wxss */

page{

height: 100%

}

.container1{

width: 100%;

height: 100%;

background-color: rebeccapurple

}

.item{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black

}

.item2{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black

}

微信小程序绝对定位和相对定位

3、保存代码,模拟器界面如下,这是默认的显示

微信小程序绝对定位和相对定位

4、修改mypage.wxss代码如下,为item2使用相对位置,左偏移100rpx

page{

height: 100%

}

.container1{

width: 100%;

height: 100%;

background-color: rebeccapurple

}

.item{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black

}

.item2{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black;

position: relative;

left: 100rpx;

}

微信小程序绝对定位和相对定位

5、保存代码,模拟器效果如下图

微信小程序绝对定位和相对定位

6、修改mypage.wxss代码改item2为绝对定位,代码如下:

/* pages/mypage/mypage.wxss */

page{

height: 100%

}

.container1{

width: 100%;

height: 100%;

background-color: rebeccapurple;

}

.item{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black

}

.item2{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black;

position: absolute;

left: 100rpx;

}

微信小程序绝对定位和相对定位

7、保存后,效果如下图,这是因为item2的父元素view并没有定位

微信小程序绝对定位和相对定位

8、修改代码,将item2的父元素view定位,代码如下:

/* pages/mypage/mypage.wxss */

page{

height: 100%

}

.container1{

width: 100%;

height: 100%;

background-color: rebeccapurple;

position: absolute;

top:100rpx;

}

.item{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black

}

.item2{

width: 100rpx;

height: 100rpx;

background-color: gray;

border: 5rpx solid black;

position: absolute;

left: 100rpx;

}

微信小程序绝对定位和相对定位

9、保存代码,模拟器效果如下图

微信小程序绝对定位和相对定位

  • 分布式任务调度都包含哪些类型?
  • 《神将世界》PK武魂推荐
  • qt怎么添加图片资源
  • 如何使用Visual Studio 2015进行新建一个空项目
  • cisco软件怎么在两个路由器中部署静态路由
  • 热门搜索
    向右的箭头怎么打 二郎山景区 老君山景区 杭州旅游景点大全 半球电磁炉怎么样 东北旅游线路 粉刺怎么办 脸色不好怎么调理 电脑配置怎么升级 贵州凯里旅游