过渡背景图头部 透明

<template>
  <view class="container">
    <view class="header">
      <view class="header-bg" :style="headerStyle"></view>
      <view class="header-inner">
        <view style="width: 100%;" class="search-box flex-cn" @click="toSearchGoods">
          <text class="qh-rt-single qh-rt-a-zu2432 search-icon"></text>
          <text class="search-text">请输入商品名称</text>
          <view class="search-button">搜索</view>
        </view>
      </view>
    </view>
    <scroll-view @scroll="onScroll" scroll-y="true" class="content">
      <view class="content-items">
        <view v-for="item in 50" :key="item" class="item">内容 {{ item }}</view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      scrollTop: 0, // 滚动位置
    };
  },
  computed: {
    headerStyle() {
      // 计算透明度,滚动超过300px时透明度为0
      const opacity = Math.max(1 - this.scrollTop / 100, 0);
      return {
        backgroundImage: `url(".png")`,
        backgroundSize: 'cover',
        backgroundPosition: 'center',
        height: '300rpx',
        width: '100%',
        position: 'fixed',
        top: 0,
        left: 0,
        opacity: opacity,
        transition: 'opacity 0.5s',
      };
    },
  },
  methods: {
    onScroll(event) {
      this.scrollTop = event.detail.scrollTop;
    },
  },
};
</script>

<style scoped lang="less">
.search-box {
  position: sticky;
  top: 0;
  border: 1px solid #f5f5f5;

  margin-left: 24rpx;
  width: 400rpx;
  height: 66rpx;
  background-color: #ffffff;
  border-radius: 34rpx;
  padding-left: 10rpx;

  box-sizing: border-box;
  line-height: 32rpx;

  .search-icon {
    font-size: 28rpx;
    color: #999999;
  }

  .search-text {
    font-size: 28rpx;
    color: #999999;
    margin-left: 12rpx;
  }

  .search-button {
    color: #000;
    border-radius: 20px;
    height: 56rpx;
    width: 122rpx;
    line-height: 56rpx;
    margin-left: auto;
    text-align: center;
    font-size: 28rpx;
  }
}
.container {
  height: 100vh;
  overflow: hidden;
}

.header {
  z-index: 10;
  position: relative;
  width: 100%;
  height: 300rpx;
}

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

.header-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  padding-top: 300rpx; /* 确保内容不被头部遮挡 */
  height: 100%;
  overflow: scroll;
}

.content-items {
  padding: 20px;
  background-color: #f5f5f5;
  height: 2000px;
}

.item {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #ccc;
  text-align: center;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值