小程序背景图固定以及标题栏遮挡内容滚动时显示内容

滚动页面时背景图固定:

采用一张图片作为背景图,通过设置position为fixed以及z-index为-1,目的是为了让它在最底层。

<!-- 背景图 -->
<view>
    <image class="bg-image" mode="scaleToFill" src="/img/bg2.png"></image>
</view>

.bg-image {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
}

标题栏背景固定并且处于更高层

只需要通过获取标题栏的宽度和高度,然后进行剪切
获取高度和宽度:

wxml:
<!-- 为了和背景图一样,需要使用的图片还是背景图以及层次需要比背景图更高,不过要低于标题栏的块的层次 -->
<view>
    <image class="top-bg-image" mode="scaleToFill" src="/img/bg2.png" style="clip: rect(0, {{top_width}}px,{{top_height-12}}px, 0)"></image>
</view>
<!-- 标题栏宽度和高度 -->
<view id='top' class="top" style="padding-top: {{statusHeight+10}}px;">
    <view class="head">
        <view class="btn-arrowhead" bindtap="Return"></view>
        <view class="title"> 注册(学生端)</view>
    </view>
</view>
js:
  getTopHeight: function() {
        let that = this;//注意要将this用that来代替
//selectAll(元素选择器)
       wx.createSelectorQuery().selectAll('#top').boundingClientRect(function(rect) {
            that.setData({
                top_height: rect[0].height,
                top_width: rect[0].width,
            });
            console.log("高度是:", that.data.top_height);
        }).exec();
    },

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值