Angel丶守护&雪之源 2024-01-15 18:05 采纳率: 22%
浏览 5

uv-pick-color 拉起弹窗时拖动选择颜色时如何阻止遮罩层下的页面滚动

代码如下:

<view class="select-color">
    <input type="hidden" v-model="item.value" style="height: 0px; line-height: 0px;min-height:0px">
    <view
        :title="colorTitle"
        style="{'background-color': (item.value !=='' || item.valur !== null) ? item.value : 'transparent','width':'40px','height':'40px','border':'1px solid #ec 
                ecec','borderRadius':'3px'}"
        @click="openColorPicker(val,index)"
    >
    </view>
</view>
<uv-pick-color ref="ColorPicker" :color="color" @confirm.stop="(e)=>confirm(e)" :prefab-color="[]" @touchmove.native.prevent.stop></uv-pick-color>
<script>
        // 调出颜色选择器
    const openColorPicker = (val,index) =>{
            state.idx = index
        currentInstance.ctx._.refs.ColorPicker.open();
    }
        // 颜色选择器保留rgba形式
    const confirm = (e) => {
        props.options.items[state.idx].value = 'rgba(' + e.rgba.r + ',' + e.rgba.g + ',' + e.rgba.b + ',' + e.rgba.a + ')'
    }
</script>
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2024-01-15 20:37
    关注

    【以下回答由 GPT 生成】

    要实现阻止遮罩层下的页面滚动,在拖动选择颜色时,可以通过以下步骤来实现:

    1. 首先,给弹窗添加一个事件监听器,监听弹窗打开事件。 javascript const openColorPicker = (val, index) => { state.idx = index; currentInstance.ctx._.refs.ColorPicker.open(); window.addEventListener('touchmove', preventScroll, { passive: false }); };

    2. 创建一个事件处理函数 preventScroll,用于阻止默认的滚动行为。 javascript const preventScroll = (event) => { event.preventDefault(); };

    3. 给弹窗添加一个事件监听器,监听弹窗关闭事件。 javascript const confirm = (e) => { props.options.items[state.idx].value = 'rgba(' + e.rgba.r + ',' + e.rgba.g + ',' + e.rgba.b + ',' + e.rgba.a + ')'; window.removeEventListener('touchmove', preventScroll); };

    4. 当弹窗关闭时,移除滚动的事件监听器。

    下面是修改后的代码:

    <view class="select-color">
      <input type="hidden" v-model="item.value" style="height: 0px; line-height: 0px;min-height:0px">
      <view :title="colorTitle" style="{{ {'background-color': (item.value !== '' || item.value !== null) ? item.value : 'transparent','width':'40px','height':'40px','border':'1px solid #ececec','borderRadius':'3px'} }}" @click="openColorPicker(val,index)"></view>
    </view>
    <uv-pick-color ref="ColorPicker" :color="color" @confirm.stop="(e) => confirm(e)" :prefab-color="[]" @touchmove.native.prevent.stop></uv-pick-color>
    <script>
      // 调出颜色选择器
      const openColorPicker = (val, index) => {
        state.idx = index;
        currentInstance.ctx._.refs.ColorPicker.open();
        window.addEventListener('touchmove', preventScroll, { passive: false });
      };
    
      // 颜色选择器保留rgba形式
      const confirm = (e) => {
        props.options.items[state.idx].value = 'rgba(' + e.rgba.r + ',' + e.rgba.g + ',' + e.rgba.b + ',' + e.rgba.a + ')';
        window.removeEventListener('touchmove', preventScroll);
      };
    
      // 阻止默认滚动行为
      const preventScroll = (event) => {
        event.preventDefault();
      };
    </script>
    

    这样,在弹窗打开时,会添加一个滚动事件监听器,阻止页面的滚动行为;当弹窗关闭时,会移除该事件监听器,恢复页面的滚动行为。这样就可以实现阻止遮罩层下的页面滚动的功能。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 1月15日

悬赏问题

  • ¥20 python爬虫遇到空文本取不出来
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入