twitter-bootstrap - alpineJS 如何在引导模式对话框之外隐藏内容?
问题描述
在 Bootstrap 4.5/jquery 3.3 / alpinejs 2.2 应用程序中,我显示具有大小限制的图像并单击图像,我使用 aplineJS 方法打开模式对话框
x-on:click="show_hostel_image_modal = true"
它打开正常,但我不喜欢对话框后面的所有内容都是可见的。如果有一种方法可以隐藏它,而无需调用引导 js 方法,因为我从 jquery 方法移动?
我有 :
@if(!empty($currentHostelImage))
<div
x-data="{ show_hostel_image_modal: false, hostelImages: <?php print prepareEncodeArray($hostelImages) ?>, current_image_id:
{{$currentHostelImage->id }}, currentImage : {{$currentHostelImage }} } "
class="hostel_images_block_wrapper">
<div
class="modal_editor_container p-2"
x-show="show_hostel_image_modal"
@click.away="show_hostel_image_modal = false"
>
<div
class="modal_editor_title"
>
<h4 class="modal-title p-2">
{!! $viewFuncs->showAppIcon('image') !!}Image view : <span x-html="currentImage.filenameData.image"></span>
<button class="close" type="button" x-on:click="show_hostel_image_modal= false">
<i class="test-device p-2 pl-4 pr-4 m-0"></i>
<span aria-hidden="true">×</span>
</button>
</h4>
</div> <!-- modal_editor_title -->
<div class="modal_editor_fields" :style="'max-height: ' + ( modalHeight() - 20 ) +'px; '">
<div x-show="typeof currentImage.filenameData != 'undefined'">
<img :src="currentImage.filenameData.image_url" class="hostel_dialog_image m-1 p-1"
:class="{ 'hostel_image' : true }"
>
<div class="alert alert-info" role="alert">
{!! $viewFuncs->showAppIcon('info') !!}
<span x-html="currentImage.filenameData.file_info"></span>
</div>
</div> <!-- <div> -->
</div> <!-- modal_editor_fields-->
</div> <!-- <div class="modal_editor_container"> -->
<template x-for="nextHostelImage in hostelImages">
<div class="hostel_view_current_image_wrapper" x-show="current_image_id === nextHostelImage.id ">
<img :src="nextHostelImage.filenameData.image_url" class="hostel_view_current_image"
:alt="nextHostelImage.filenameData.file_info" x-on:click="show_hostel_image_modal = true" data-toggle="tooltip"
data-placement="top" title="Click to view in full size">
</div>
</template>
</div>
@endif
和风格:
.modal_editor_container {
width: 90%;// !important;
top: 20px;
left: 50px;
position: absolute;
z-index: 900;
flex: 1;
flex-direction: column;
justify-items: flex-start;
padding: 0;
margin: 0;// !important;
}
已修改: 我尝试按照您的步骤操作我的导航菜单。
你能看看https://jsfiddle.net/z83m7fnc/
我只需要注意 hostel_images_block_wrapper - 这是所有图像块的包装类和模态对话框的 modal_editor_container 类。
谢谢!
解决方案
您可以将透明背景应用于您的 div.hostel_images_block_wrapper
.hostel_images_block_wrapper {
background-color: rgba(0,0,0,0.5);
position: absolute; (could also be fixed)
top: 0;
left: 0;
height: 100vh;
width: 100%;
}
确保检查此元素的 z-index 以及它在您的页面上下文中包含的元素。
overflow-y: hidden
使用 javascript,您可以通过添加到正文来阻止页面在模式后面滚动。
在您的 div.hostel_images_block_wrapper 添加以下监听器
@body-scroll="document.body.style.overflowY = open ? 'hidden' : ''"
然后从任何具有点击甚至更新您的show_hostel_image_modal
属性的元素中,您可以调度以下自定义事件:
@click="$dispatch('body-scroll', {})"
顺便说一句,因为 AlpineJS 包含得很好,所以你不需要对变量名如此具体。例如,show_hostel_image_modal
可能只是show
或open
。您可以通过这种方式清理大量冗余命名,并使您的标记更加清晰。
推荐阅读
- c++ - 推回功能的容量问题
- user-interface - 是否可以在没有对象大小的情况下计算每度视角的点数?
- reactjs - 使用 ReactRouter nas PHP 托管进行反应
- performance - Spring JPA 在 postgres 数据库中插入数据需要太多时间和内存
- python - 使用用户的输入在 excel 文件中搜索特定行(python)
- javascript - React Native中VirtualzedList更新慢时返回的对象含义
- php - 以逗号分隔的字符串拆分并在 MySQL 和 PHP 中显示
- python - 具有多个迭代器的 Python 多处理部分池
- python - 限制 Windows 中的 python 脚本 RAM 使用
- ruby-on-rails - 更好的替代尝试方法