微信小程序中justify-content: flex-end 失效

本文介绍了一个关于在Flex布局中,按钮无法按预期对齐到右侧的问题及解决方案。通过在每个按钮外包裹一层view元素,成功实现了按钮的正确对齐。

不正确的效果是这样的:

页面wxml代码如下:

<scroll-view class="hot-box" scroll-y="true">
      <view class="orderDetails" wx:for="{{order}}" wx:key=" ">

        ........省略部分代码......
        ........省略部分代码......
        ........省略部分代码......

        <view class="productBtn">
          <button class="more" hover-class="hover" bindtap="">删除订单</button>
          <button class="more" wx:if="{{item.isPay===0}}" hover-class="hover" bindtap="">取消订单</button>
          <button class="more" wx:if="{{item.isPay===0}}" hover-class="hover-pay" bindtap="">付款</button>
        </view>
      </view>

    </scroll-view>

wxss代码如下:

.productBtn {
  display: flex;
  flex-direction: row;
  width: 100%;
  margin-bottom: 25rpx;
  margin-top: 30rpx;
  justify-content: flex-end;
  padding: 0;
  height: 70rpx;
}

问题:无论怎么设置 justify-content都不起作用,三个按钮都是均匀分布。

解决方法,修改wxml代码,给每个button外边套一层view:

<scroll-view class="hot-box" scroll-y="true">
      <view class="orderDetails" wx:for="{{order}}" wx:key=" ">
        
        ..........省略部分代码..........
        ..........省略部分代码..........
        ..........省略部分代码..........

        <view class="productBtn">
          <view>
            <button class="more" hover-class="hover" bindtap="">删除订单</button>
          </view>
          <view>
            <button class="more" wx:if="{{item.isPay===0}}" hover-class="hover" bindtap="">取消订单</button>
          </view>
          <view>
            <button class="more" wx:if="{{item.isPay===0}}" hover-class="hover-pay" bindtap="">付款</button>
          </view>
        </view>
      </view>
</scroll-view>

修改后效果如下:

<template> <view class="page-container"> <!-- 打开弹框的按钮 --> <!-- 遮罩层 --> <view class="mask" v-if="localShow" @click="hideDialog"></view> <!-- 弹框 --> <view class="dialog" :class="{ 'dialog-show': localShow }"> <view class="dialog-content"> <view class="dialog-header"> <text class="dialog-title">提示</text> <text class="close-icon" @click="hideDialog">×</text> </view> <!-- 内容区 --> <view > <text>申请提前结束考核,您的入职考核结束时间=申请审批通过的时间。考核结束后的已缴费订单将都不再计入考核结果,是否确认提前结束考核?</text> </view> <view class="sec"> <view class="inComeList" v-if="filteredActions"> <inComeItem v-for="(item, index) in filteredActions" :key="item.settleMonth" :lastDom="index == filteredActions.length - 1" :data="item" /> </view> <view class="button-group"> <button class="dialog-button" >取消</button> <button class="dialog-button">确认</button> </view> </view> </view> </view> </view> </template> <script> import inComeItem from "../inComeItem/index.vue"; export default { components: { inComeItem, }, props: { showDialog: { type: Boolean, default: false, }, showModel: { type: Boolean, default: false, }, }, watch: { showModel(newVal) { // 监听父组件数据变化 this.localShow = newVal; }, }, data() { return { localShow: this.showDialog, // 本地副本 // 返回假数据 filteredActions: [] }; }, created() { console.log("============onload===========", this.showDialog) }, onload() { console.log("============onload===========", this.showDialog) this.getConfirmApply(); }, methods: { async getConfirmApply(){ const { data } = await uni.$u.http.get("/app/staffAssess/getConfirmApply"); console.log("APP-查询提前结束审批信息",data) }, // 隐藏弹框 hideDialog() { this.localShow = false; this.$emit("update:show-model", false); // 通知父组件更新 }, }, }; </script> <style lang="scss" scoped> .page-container { padding: 20rpx; font-family: "PingFangSC-Regular", "PingFang SC", sans-serif; font-weight: 400; font-style: normal; color: #0d0d0d; background: #f3f5f8; } .mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 998; } .dialog { position: fixed; bottom: -100%; left: 0; width: 100%; height: calc(100% * 2 / 3); transition: bottom 0.3s ease; z-index: 999; display: flex; justify-content: center; align-items: flex-end; // bottom: 0; } .dialog-show { bottom: 0; } .dialog-content { background-color: #fff; width: 100%; height: 100%; border-top-left-radius: 10rpx; border-top-right-radius: 10rpx; padding: 30rpx; box-sizing: border-box; } .dialog-header { display: flex; justify-content: space-between; align-items: center; padding-bottom: 20rpx; border-bottom: 1rpx solid #ccc; } .dialog-title { font-size: 32rpx; font-weight: normal; text-align: center; flex: 1; } .close-icon { font-size: 36rpx; cursor: pointer; } .radio-group { margin-top: 60rpx; } .radio-item { display: flex; align-items: center; margin-bottom: 10rpx; font-size: 28rpx; } .radio-item radio { transform: scale(0.6); } .audit-status { display: flex; justify-content: space-between; align-items: center; margin-top: 25rpx; padding: 15rpx; padding-top: 30rpx; background-color: rgba(245, 246, 249, 1); border-radius: 15rpx; } .status-label { font-size: 28rpx; } .status-value { display: flex; font-size: 28rpx; } .audit-text { color: #d01212; } .audit-date { color: #666; margin-top: 10rpx; font-family: "PingFangSC-Regular", "PingFang SC", sans-serif; font-weight: 400; font-style: normal; color: #0d0d0d; } .dialog-footer { padding-top: 20rpx; text-align: center; margin-top: 25rpx !important; } /deep/.next-button { width: 80%; padding: 10rpx; background-color: rgba(12, 195, 199, 1) !important; border: none !important; color: #fff !important; border-radius: 15rpx; font-size: 30rpx; } .titleName { margin-top: 40rpx; } .radioName { margin-top: 40rpx; } /* 添加按钮组样式 */ .button-group { display: flex; /* 使用 Flex 布局 */ justify-content: space-between; /* 按钮之间平均分配空间 */ margin-top: 40rpx; /* 顶部间距 */ gap: 20rpx; /* 按钮之间的间隙 */ } /* 按钮样式 */ .dialog-button { flex: 1; /* 每个按钮平分可用空间 */ height: 80rpx; line-height: 80rpx; background-color: rgba(12, 195, 199, 1); color: white; border-radius: 15rpx; font-size: 30rpx; border: none; } </style> <template> <view class="page-container"> <!-- 打开弹框的按钮 --> <!-- 遮罩层 --> <view class="mask" v-if="localShow" @click="hideDialog"></view> <!-- 弹框 --> <view class="dialog" :class="{ 'dialog-show': localShow }"> <view class="dialog-content"> <view class="dialog-header"> <text class="dialog-title">提示</text> <text class="close-icon" @click="hideDialog">×</text> </view> <!-- 内容区 --> <view > <text>申请提前结束考核,您的入职考核结束时间=申请审批通过的时间。考核结束后的已缴费订单将都不再计入考核结果,是否确认提前结束考核?</text> </view> <view class="sec"> <view class="inComeList" v-if="filteredActions"> <inComeItem v-for="(item, index) in filteredActions" :key="item.settleMonth" :lastDom="index == filteredActions.length - 1" :data="item" /> </view> <view class="button-group"> <button class="dialog-button" >取消</button> <button class="dialog-button">确认</button> </view> </view> </view> </view> </view> </template> <script> import inComeItem from "../inComeItem/index.vue"; export default { components: { inComeItem, }, props: { showDialog: { type: Boolean, default: false, }, showModel: { type: Boolean, default: false, }, }, watch: { showModel(newVal) { // 监听父组件数据变化 this.localShow = newVal; }, }, data() { return { localShow: this.showDialog, // 本地副本 // 返回假数据 filteredActions: [] }; }, created() { console.log("============onload===========", this.showDialog) }, onload() { console.log("============onload===========", this.showDialog) this.getConfirmApply(); }, methods: { async getConfirmApply(){ const { data } = await uni.$u.http.get("/app/staffAssess/getConfirmApply"); console.log("APP-查询提前结束审批信息",data) }, // 隐藏弹框 hideDialog() { this.localShow = false; this.$emit("update:show-model", false); // 通知父组件更新 }, }, }; </script> <style lang="scss" scoped> .page-container { padding: 20rpx; font-family: "PingFangSC-Regular", "PingFang SC", sans-serif; font-weight: 400; font-style: normal; color: #0d0d0d; background: #f3f5f8; } .mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 998; } .dialog { position: fixed; bottom: -100%; left: 0; width: 100%; height: calc(100% * 2 / 3); transition: bottom 0.3s ease; z-index: 999; display: flex; justify-content: center; align-items: flex-end; // bottom: 0; } .dialog-show { bottom: 0; } .dialog-content { background-color: #fff; width: 100%; height: 100%; border-top-left-radius: 10rpx; border-top-right-radius: 10rpx; padding: 30rpx; box-sizing: border-box; } .dialog-header { display: flex; justify-content: space-between; align-items: center; padding-bottom: 20rpx; border-bottom: 1rpx solid #ccc; } .dialog-title { font-size: 32rpx; font-weight: normal; text-align: center; flex: 1; } .close-icon { font-size: 36rpx; cursor: pointer; } .radio-group { margin-top: 60rpx; } .radio-item { display: flex; align-items: center; margin-bottom: 10rpx; font-size: 28rpx; } .radio-item radio { transform: scale(0.6); } .audit-status { display: flex; justify-content: space-between; align-items: center; margin-top: 25rpx; padding: 15rpx; padding-top: 30rpx; background-color: rgba(245, 246, 249, 1); border-radius: 15rpx; } .status-label { font-size: 28rpx; } .status-value { display: flex; font-size: 28rpx; } .audit-text { color: #d01212; } .audit-date { color: #666; margin-top: 10rpx; font-family: "PingFangSC-Regular", "PingFang SC", sans-serif; font-weight: 400; font-style: normal; color: #0d0d0d; } .dialog-footer { padding-top: 20rpx; text-align: center; margin-top: 25rpx !important; } /deep/.next-button { width: 80%; padding: 10rpx; background-color: rgba(12, 195, 199, 1) !important; border: none !important; color: #fff !important; border-radius: 15rpx; font-size: 30rpx; } .titleName { margin-top: 40rpx; } .radioName { margin-top: 40rpx; } /* 添加按钮组样式 */ .button-group { display: flex; /* 使用 Flex 布局 */ justify-content: space-between; /* 按钮之间平均分配空间 */ margin-top: 40rpx; /* 顶部间距 */ gap: 20rpx; /* 按钮之间的间隙 */ } /* 按钮样式 */ .dialog-button { flex: 1; /* 每个按钮平分可用空间 */ height: 80rpx; line-height: 80rpx; background-color: rgba(12, 195, 199, 1); color: white; border-radius: 15rpx; font-size: 30rpx; border: none; } </style> onload为啥不执行
07-15
### 使用 `justify-content: flex-end;` 的方法 `justify-content` 是 CSS Flexbox 布局中的一个关键属性,用于控制容器内项目在主轴上的对齐方式。当设置为 `flex-end` 时,所有项目会沿着主轴的末尾对齐。 #### 基本语法 ```css .container { display: flex; justify-content: flex-end; } ``` 上述代码将创建一个 Flex 容器,并使所有子元素在主轴上右对齐(或根据主轴方向进行对齐)。 #### 主轴方向的影响 Flexbox 的主轴方向由 `flex-direction` 属性决定,因此 `justify-content: flex-end;` 的实际对齐方向取决于该属性的值: - 如果 `flex-direction: row;`(默认值),则项目会沿着水平轴向右对齐。 - 如果 `flex-direction: column;`,则项目会沿着垂直轴向下对齐。 - 如果 `flex-direction: row-reverse;` 或 `column-reverse;`,则主轴的方向会反转,影响 `flex-end` 的对齐方向[^1]。 #### 常见使用场景 1. **水平右对齐**:通常用于导航栏、按钮组等需要右对齐的布局。 2. **垂直底部对齐**:在卡片或容器中,将内容对齐到容器底部。 3. **响应式布局**:结合媒体查询,根据不同屏幕尺寸调整对齐方式。 #### 注意事项 - **Flex 容器必须正确设置**:确保父容器的 `display` 属性为 `flex`,否则 `justify-content` 不会生效。 - **避免与 `flex-grow` 冲突**:如果某个子元素设置了 `flex-grow: 1;`,它会占据剩余空间,可能导致 `justify-content` 看起来无效。这是因为 `flex-grow` 会改变子元素的大小,从而影响对齐效果[^2]。 - **浏览器兼容性**:现代浏览器普遍支持 Flexbox,但在旧版本浏览器中可能需要添加前缀。 ### 示例代码 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .container { display: flex; justify-content: flex-end; border: 1px solid #ccc; padding: 10px; height: 100px; } .item { width: 50px; height: 50px; background-color: lightblue; margin: 5px; } </style> </head> <body> <div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> </body> </html> ``` 在上述示例中,`.container` 是一个 Flex 容器,其中的 `.item` 元素会沿着主轴的末尾对齐。 ### 常见问题 - **为什么 `justify-content: flex-end;` 不起作用?** - 检查容器是否设置了 `display: flex;`。 - 确保没有子元素使用了 `flex-grow`,这可能导致对齐效果不明显。 - 确认主轴方向是否正确(通过 `flex-direction` 设置)。 - **如何在垂直方向上使用 `justify-content: flex-end;`?** - 将 `flex-direction` 设置为 `column`,这样 `justify-content: flex-end;` 会使项目在垂直方向上底部对齐。 - **`flex-end` 和 `end` 有什么区别?** - `flex-end` 是 Flexbox 布局中的关键字,专用于 Flex 容器。 - `end` 是 CSS Logical Properties 中的关键字,适用于更广泛的布局模型(如 Grid),但在 Flexbox 中可能不生效---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值