本篇介绍Bootstrap5 UI的Modal(弹出互动视窗)呼叫方式
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
先介绍一般官网写法
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
Open modal
</button>
<!-- The Modal -->
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
这样就完成啦!
那其实我看了一下也可以这样写
<button id="myModal" class="btn btn-primary" type="button">点我打开视窗1</button>
<!-- Model模板 -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
let myModal = document.getElementById("myModal");
let exampleModal = new bootstrap.Modal(document.getElementById('exampleModal'));
myModal.addEventListener("click", function(e){
exampleModal.show();
});
这样也可以!
那我们加一点难度,使用Vue3 + 将Model写成元试看看
<main id="app">
<button class="btn btn-primary" type="button" @click="showModal">点开视窗1</button>
<modal ref="exampleModal" :parent-title="h1Title" parent-txt="我是讯息视窗内容" v-on:emit-hide="hideModal"></model>
</main>
为什麽我们要刚要先介绍 new bootstrap.Modal的写法呢?因为我们在vue会用到
vue范例有用到的观念有
1.想要去取得某个 Dom 的资讯,可以使用 ref 这个 Attr,父层元件则可以透过 this.$refs来取得子元件
2.Props 父传子
3.$emit 子传父
const app = Vue.createApp({
data(){
return {
h1Title:"快搭上姐姐的`微`前端便车",
isActive:true,
modal: null
}
},
mounted() {
this.modal = new bootstrap.Modal(this.$refs.exampleModal.$el)
},
methods: {
showModal(){
this.modal.show();
},
hideModal(){
this.modal.hide();
}
}
});
app.component("modal",{
props:["parentTitle","parentTxt"],
methods:{
hidemodelInner(){
console.log("内层事件被触发");
this.$emit("emit-hide");
}
},
template:`
<div class="modal fade" id="exampleModal" ref="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> {{ parentTitle }}</h5>
<button type="button" class="btn-close" @click="hidemodelInner()" aria-label="Close"></button>
</div>
<div class="modal-body">
{{ parentTxt }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @click="hidemodelInner()">关闭</button>
</div>
</div>
</div>
</div>`
})
app.mount("#app")
这样就完成啦!
附上程序码
YA!看完这篇,本系列文章就倒数3天惹!
本篇参考文章:
>>: Day 27 - 利用 WireGuard 建立点对点隧道
前言 初步推算出大盘与台积电有很大的正相关,然後看看资金雄厚,影响市场非常大的三大法人,哪些东西可以...
参考网站:Keras官方指南 根据上面的参考网站,可以做出基本的类神经网路。 首先要先有基本...
实作 所谓读了原始码却不实作与验证, 那不过就是对原始码的意淫而已, 所以在这个系列文的结尾总是还要...
大家好,我是西瓜,你现在看到的是 2021 iThome 铁人赛『如何在网页中绘制 3D 场景?从 ...