绝对定位和overflow-y: scroll实现不使用fixed固定定位将元素固定在页面顶部或底部

本文探讨了如何使用绝对定位和overflow-y: scroll属性替代fixed定位,以解决在不同设备上可能出现的位置偏差问题。通过设置元素的CSS样式,确保其在页面顶部或底部保持固定,同时保持良好的滚动体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用position:fixed固定定位可以很方便将元素固定在页面某处。但是它和绝对顶部不同:绝对定位是相对于其position:absolute的父元素来定位;而fixed定位相对于屏幕;这导致在左右分别布局的页面上出现定位的元素在不同显示器或不同型号手机上出现偏差。使用绝对定位能通过相对父元素来保证定位元素在左边某处或右边某处。

此方法有两个步骤:1.需定位元素的css:

  1. z-index90;
  2. positionabsolute;
<template> <view class="container"> <!-- 顶部导航栏 --> <u-navbar :title="title" leftIconColor="#fff" placeholder bgColor="#57dc7f" autoBack titleStyle="color:#fff;text-align:center;" :customStyle="navbarStyle" v-if="title" ></u-navbar> <!-- 滚动容器 --> <view class="scroll-wrapper"> <scroll-view scroll-y class="scroll-view" @scrolltolower="$emit('lower')" :style="scrollStyle"> <!-- 部分页面有的背景图 没有title才展示 --> <image class="bgImg" :src="getImgUrl('/statics/img/backImg.png')" mode="widthFix" v-if="isShow"></image> <!-- 内容主图 --> <slot></slot> </scroll-view> </view> <!-- 底部插槽 --> <slot name="bottom"></slot> </view> </template> <script> import getImgUrl from '@/mixins/getImgUrl.js'; export default { mixins: [getImgUrl], name: 'common-scroll', props: { title: String, isShow: { type: Boolean, default: false } }, computed: { scrollStyle() { const systemInfo = uni.getSystemInfoSync(); const navBarHeight = this.title ? systemInfo.statusBarHeight + 44 : 0; return { minHeight: `calc(100vh - ${navBarHeight}px)` }; } } }; </script> <style lang="scss" scoped> .container { height: 100vh; display: flex; flex-direction: column; } .scroll-wrapper { flex: 1; min-height: 0; /* 关键:允许内容压缩 */ padding-bottom: env(safe-area-inset-bottom); } .scroll-view { height: 100%; overflow-y: auto; box-sizing: border-box; overflow-anchor: none; /* 防止滚动跳动 */ position: relative; overscroll-behavior-y: contain; .bgImg { width: 100%; height: 600rpx !important; position: absolute; top: 0; left: 0; z-index: -1; } } ::v-deep .u-navbar { z-index: 999; flex-shrink: 0; /* 防止导航栏被压缩 */ } </style> 调整这个代码:vue2+js+uni-app+uview需求我需要一个容器:所有的页面都需要包含在这个容器中,有些页面传递title,则需要展示u-navbar,底部有 <slot name="bottom"></slot>是用来放底部按钮的。顶部的navbar底部的bottom说是能滚动的,背景为#f5f5f5,中间可以滚动的部分,如果长度没有超出的话,就能滚动,如果超出了,就滚动。如果没有title,但是有bottom的按钮的话,那就除开bottom后超出的部分 都可滚动,如果都没有的话,那就整个页面超出部分可以滚动。别动我u-navbar的颜色。还有,scroll-wrapper的高度默认是除开u-navbarbottom以外的部分。如果没有u-navbar,scroll-wrapper的高度就是除开bottom以外的部分,如果都没有就是100vh,滚动部分的高度如果超出100vh,就可以滚动,如果没有超出,就更滚动!!!
最新发布
07-25
.signature-trace-drawer display: flex; -webkit-box-orient: vertical; -webkit-box-direction: reverse; -webkit-flex-direction: column-reverse; -moz-box-orient: vertical; -moz-box-direction: reverse; -ms-flex-direction: column-reverse; flex-direction: column-reverse; position: fixed; height: 100%; width: 100%; left: 0; top: 0; z-index: 1001; background: rgba(0, 0, 0, 0.5); justify-content: center align-items: center overflow: scroll .signature-trace-content width 100% justify-content: center align-items: center .signature-trace-wrapper-body position relative background: white margin-left 20px margin-right 20px padding 0 20px border-radius: 12px .signature-trace-wrapper-close position: absolute left 50% bottom -50px height 24px width 24px @extends button.close // background: url(../images/close_lt.png) no-repeat center / cover background-size: 20px background-color: white; opacity: 1 border-radius: 100% transform: translate(-50%, -50%) text-align: center color : rgb(166,166,166) span font-size 20px line-height 24px .signature-trace-title font-size: 17px font-weight bold padding-top: 15px margin-bottom: 10px text-align: center color #202020 .signature-trace-image-container background: url('../images/miziGrid.png') no-repeat center/cover; background-size: 100% 100% background-repeat: repeat; position relative aspect-ratio 1 border-radius: 12px; background-color: #F5F5F5; display: flex justify-content: center align-items: center overflow hidden .ps-sign position absolute height 100% min-width 100% aspect-ratio 1 z-index 1002 .canvas height 100% width 100% .signature-trace-spinner display: none width 24px height 24px position: absolute top: 50% left 50% background: url('../images/loading.gif') no-repeat center/cover; transform translate(-50%, -50%) .signature-trace-template-img display: none; width 100% opacity: 0.4; background: no-repeat center/cover; .signature-trace-template-text display: none; justify-content: center align-items: center width: calc(100% - 50px); height: calc(100% - 50px) position: absolute top: 45% left 50% transform: translate(-50%, -50%) opacity: 0.4; .signature-trace-reload-content display: flex align-items: center justify-content: center position: absolute top: 50% left 50% transform: translate(-50%, -50%) .signature-trace-reload font-size 14px color #808080 white-space: nowrap; span color #808080 .signature-trace-button display: flex; justify-content: space-between; align-items: center; margin: 20px 20px 0 padding-bottom: 20px .signature-trace-resetting font-size 14px flex-grow:1; width 100px height 36px line-height 36px text-align: center background: #F5F5F5; margin-right: 10px border-radius: 16px color #202020 opacity: 0.3; .signature-trace-determine font-size 14px background-color: #ED5151 flex-grow:1; height 36px; width 100px line-height 36px text-align: center color #FFFFFF border-radius: 16px opacity: 0.3; .signature-trace-determine.center-block height 35px width 182px flex-grow 0 position relative left 50% transform translateX(-50%) @media only screen and (min-device-aspect-ratio:3/4) .signature-trace-drawer .signature-trace-content .signature-trace-wrapper-body margin-left 60px margin-right 60px .signature-trace-title font-size 12px 如上的样式,我在特别宽的屏幕上打开比如说平板上,signature-trace-wrapper-body就会变的很大,导致他的子元素signature-trace-wrapper-close关闭按钮看到,signature-trace-button按钮区域也在屏幕上看到,滚动也滚动了,应该如何解决呢?
05-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值