小程序顯示彈窗時禁止底層頁面滾動

當彈框內容有滾動的時候,採用第一種方式有效
① 第一種方式
利用position:fixed. 禁止頁面滾動.

一. 頁面結構html

<view class="indexPage {{proInfoWindow?'indexFixed':''}}">
    -----------此處爲整個頁面的結構內容
    <button catchTap="_proInfoWindowShow">點擊顯示彈窗</button>
</view>
// 當proInfoWindow爲true的時候顯示彈窗
<view wx:if="{{proInfoWindow}}">此處爲彈窗內容</view>

二. CSS部分

//添加一個類名, 把彈窗的下層內容定位爲fixed.實現禁止滾動的效果
.indexFixed{
  position: fixed;
  top:0;//top:0可不寫,否則顯示彈窗的同時會使底層滾動到頂部.
  left:0;
  bottom:0;
  right:0;
}

三. JS部分

Page({
  data: {  
    proInfoWindow:false,//控制彈窗是否顯示
      
  },
    // 點擊彈窗事件, 設置proInfoWindow爲true, 顯示彈窗.
    // 設置proInfoWindow爲true的同時, 給頁面添加了一個class名爲indexFixed的類.顯示彈窗時下層就禁止滾動,關掉彈窗時就可以滾動.
  _proInfoWindowShow(){
      this.setData({
        proInfoWindow:true
    })
  }
})

②第二種方式
用 catchtouchmove=“return”

//此處爲彈窗內容

<view  catchtouchmove="return"> //外層加 catchtouchmove="return"僅觸摸背景區域時不穿透底部.
    <view  catchtouchmove="return"></view> //在每個小的區域內加 catchtouchmove="return"
    <view> // 有需要滾動的列表區域位置不要加 catchtouchmove="return", 否則列表無法滾動
        <view>滾動列表1</view>
        <view>滾動列表2</view>
        <view>滾動列表3</view>
        <view>滾動列表4</view>
    </view>
</view>

原文鏈接:https://blog.csdn.net/Sophia0326/article/details/81779696

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章