组件page-container的使用 避免踩坑!!

属性介绍

page-container是微信小程序的一个视图容器,主要的功能就是实现一个弹窗的功能,该组件所包含的属性不多,比较关键的属性就是显示容器和容器位置的属性以及各种事件触发函数。如下

 show主要通过一些page-container外的事件触发后更改将show的值改为true,从而显示page-container组件。

position属性控制着组件弹出的位置,但是属性的值bottom,top与right,center的显示效果不同。

bottom和top只是在当前页面中显示出一个弹窗,而right和center是相当于直接载入了一个子页面,

雷区tips

当前页面最多只有 1 个容器,若已存在容器的情况下,无法增加新的容器

自定义实现底部弹出层

wxml

<view class="modals modals-bottom-dialog" hidden="{{hideModal}}">
    <view class="modals-cancel" bindtap="hideModal"></view>
    <view class="bottom-dialog-body bottom-pos" animation="{{animationData}}"></view>
</view>
<button bindtap="showModal">点我</button>

wxss

/*模态框*/
.modals {
    position: fixed;
    z-index: 999;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.modals-cancel {
    position: absolute;
    z-index: 1000;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, .5);
}

.bottom-dialog-body {
    position: absolute;
    z-index: 10001;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30rpx;
    height: 85vh;
    background-color: #fff;
    border-radius: 30rpx;
}

/*动画前初始位置*/
.bottom-pos {
    -webkit-transform: translateY(100%);
    transform: translateY(100%);
}

js

  data: {
    hideModal: true, //模态框的状态 true-隐藏 false-显示
    animationData: {},//
  },
  methods: {
    showModal: function () {
      var that = this;
      that.setData({
        hideModal: false
      })
      var animation = wx.createAnimation({
        duration: 300,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation
      setTimeout(function () {
        that.fadeIn();//调用显示动画
      }, 200)
    },

    // 隐藏遮罩层
    hideModal: function () {
      var that = this;
      var animation = wx.createAnimation({
        duration: 300,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation
      that.fadeDown();//调用隐藏动画 
      setTimeout(function () {
        that.setData({
          hideModal: true
        })
      }, 200)//先执行下滑动画,再隐藏模块

    },

    //动画集
    fadeIn: function () {
      this.animation.translateY(0).step()
      this.setData({
        animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
      })
    },
    fadeDown: function () {
      this.animation.translateY(650).step()
      this.setData({
        animationData: this.animation.export(),
      })
    },
  }

@media print { /* 隐藏整个body的所有内容 */ body * { visibility: hidden; } /* 单独显示要打印的区域 */ #printArea, #printArea * { visibility: visible; } /* 将打印区域定位到页面左上角 */ #printArea { position: absolute; left: 0; top: 0; width: 100%; max-width: 100% !important; } #printArea h1 { font-size: 20px !important; color: black !important; margin-bottom: 10px; } #printArea h2 { font-size: 18px !important; color: black !important; margin-bottom: 8px; } #printArea h3 { font-size: 16px !important; color: black !important; margin-bottom: 6px; } #printArea h4 { font-size: 14px !important; color: black !important; margin-bottom: 4px; } .print-table { width: 100% !important; max-width: 100% !important; font-size: 10px !important; table-layout: fixed !important; page-break-inside: auto !important; } .print-table .cell { padding: 2px !important; line-height: 1.2 !important; font-size: 10px !important; white-space: normal !important; word-break: break-word !important; page-break-inside: avoid !important; } .table-container .el-table { table-layout: fixed; border: 1px solid #000 !important; border-collapse: collapse; page-break-inside: auto; } .table-container .el-table__header th, .table-container .el-table__body td { border: 1px solid #000 !important; color: #000 !important; background-color: #fff !important; } .table-container tr { page-break-inside: avoid; page-break-after: auto; } } 為什麼 表格標題下面明明還有大約一整頁的空白,但是表格內容行還是分到第二頁
最新发布
08-27
### 问题分析 表格内容在打印时被错误分页到第二页,即使第一页仍有足够空间,通常是因为浏览器在打印时对分页的默认处理方式导致。这种行为可能由以下几个原因引起: - **表格行被分页符打断**:浏览器在打印时可能会在表格的某一行中间插入分页符,造成表格内容被拆分。 - **表格标题与内容之间存在空白**:表格的标题(`thead`)和内容(`tbody`)之间可能因为分页逻辑而出现空白页。 - **CSS 分页控制不足**:未使用适当的 CSS 属性来控制分页行为,导致表格内容被错误地分页。 ### 解决方案 为避免表格内容在打印时被错误分页,可以使用以下 CSS 属性进行优化: #### 1. 防止表格行被分页符打断 为表格行(`tr`)设置 `page-break-inside: avoid;` 和 `page-break-after: avoid;` 属性,确保表格行不会被分页符打断: ```css tr { page-break-inside: avoid; page-break-after: avoid; } ``` 此设置可以防止表格行在打印时被分页符打断,从而避免表格首行单独出现在下一页的情况[^1]。 #### 2. 控制表格标题在每一页重复显示 为表格标题(`thead`)设置 `display: table-header-group;` 属性,确保表格标题在每一页都能重复显示: ```css thead { display: table-header-group; } ``` 此设置使得表格的标题行在每一页都会重复显示,提高了表格的可读性[^1]。 #### 3. 防止表格主体内容被分页符打断 为表格主体(`tbody`)设置 `page-break-inside: avoid;` 属性,确保表格主体内容不会被分页符打断: ```css tbody { page-break-inside: avoid; } ``` 此设置可以防止表格主体内容被分页符打断,从而避免表格内容被错误分页到第二页。 #### 4. 设置表格容器的分页行为 为表格容器(`table`)设置 `page-break-after: auto;` 和 `page-break-before: auto;` 属性,以优化表格整体的分页行为: ```css table { page-break-after: auto; page-break-before: auto; } ``` 此设置可以确保表格整体在打印时不会被强制分页,从而避免不必要的空白页。 ### 其他注意事项 - **浏览器兼容性**:不同浏览器对分页行为的处理方式可能略有差异,建议在多种浏览器中测试打印效果。 - **表格高度控制**:如果表格内容较多,可以考虑设置表格高度上限,并启用滚动条,以避免表格内容超出页面范围。 - **打印样式表**:建议使用专门的打印样式表(`@media print`)来优化打印效果,确保页面布局在打印时更加合理。 ### 示例代码 ```css @media print { table { page-break-after: auto; page-break-before: auto; } thead { display: table-header-group; } tbody { page-break-inside: avoid; } tr { page-break-inside: avoid; page-break-after: avoid; } } ``` 通过以上 CSS 设置,可以有效防止表格内容在打印时被错误分页,并优化打印效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端 贾公子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值