Vue實現(xiàn)簡單彈窗效果
更新時間:2022年09月20日 14:25:58 作者:y-zk
這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡單彈窗效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue實現(xiàn)簡單彈窗效果的具體代碼,供大家參考,具體內(nèi)容如下
選中input 彈出選項框
顯示彈窗
首先要在components中新建兩個組件 要實現(xiàn)子組件向父組件傳值
selest.vue 里面的內(nèi)容
<template> ? <div> ? ? <h1>選擇管理員</h1> ? ? <div class="sel" @click="show">{{ str }}</div> ? ? <template v-if="bol"> ? ? ? <alt @ok="getData($event)" @cancel="close"></alt> ? ? </template> ? </div> </template> <script> import Alt from "./alt.vue"; export default { ? name: "Select", ? data() { ? ? return { ? ? ? bol: false, ? ? ? str: '', ? ? }; ? }, ? components: { ? ? Alt, ? }, ? methods: { ? ? show() { ? ? ? this.bol = true; ? ? }, ? ? // 獲取數(shù)據(jù)方法 ? ? getData(data) { ? ? ? // 關閉彈出層 ? ? ? this.bol = false; ? ? ? console.log(data); ? ? ? this.str = data; ? ? }, ? ? // 關閉彈窗 ? ? close() { ? ? ? // 關閉彈出層 ? ? ? this.bol = false; ? ? }, ? }, }; </script> <style scoped> .sel { ? width: 200px; ? height: 40px; ? border: 1px solid #ccc; ? line-height: 40px; ? text-align: center; ? cursor: pointer; ? margin: 0 auto; } </style>
alt.vue 里面的內(nèi)容
<template> ? <div class="mark"> ? ? <div class="alert"> ? ? ? <!-- 內(nèi)容 --> ? ? ? <div class="cont"> ? ? ? ? <div> ? ? ? ? ? <!-- 左邊的列表 --> ? ? ? ? ? <dl class="left"> ? ? ? ? ? ? <dt> ? ? ? ? ? ? ? <label><input type="checkbox" v-model="allBol" :checke="allBol" @change="all" />全選</label> ? ? ? ? ? ? </dt> ? ? ? ? ? ? <dd v-for="(item,index) in arrL" :key="index"> ? ? ? ? ? ? ? <label> ? ? ? ? ? ? ? ? <input type="checkbox" :checked="item.bol" v-model="item.bol" @change="only(index)" /> ? ? ? ? ? ? ? ? <img :src="item.headurl" alt=""> ? ? ? ? ? ? ? ? <span>{{item.name}}</span> ? ? ? ? ? ? ? </label> ? ? ? ? ? ? </dd> ? ? ? ? ? </dl> ? ? ? ? </div> ? ? ? ? <div> ? ? ? ? ?<dl class="left"> ? ? ? ? ? ? <dd v-for="(item,index) in arrR" :key="index"> ? ? ? ? ? ? ? <label> ? ? ? ? ? ? ? ? <img :src="item.headurl" alt=""> ? ? ? ? ? ? ? ? <span>{{item.name}}</span> ? ? ? ? ? ? ? </label> ? ? ? ? ? ? ? <em @click="del(item,index)">×</em> ? ? ? ? ? ? </dd> ? ? ? ? ? </dl> ? ? ? ? </div> ? ? ? </div> ? ? ? <!-- 按鈕 --> ? ? ? <div class="btns"> ? ? ? ? <button @click="sure">確定</button> ? ? ? ? <button @click="cancel">取消</button> ? ? ? </div> ? ? </div> ? </div> </template> <script> export default { ? data() { ? ? return { ? ? ? allBol: false, ? ? ? arrL: [ ? ? ? ? { name: '花木蘭', id: 1, headurl: require('../assets/logo.png'), bol: false }, ? ? ? ? { name: '拓跋燾', id: 2, headurl: require('../assets/logo.png'), bol: false }, ? ? ? ? { name: '宇文泰', id: 1, headurl: require('../assets/logo.png'), bol: false }, ? ? ? ? { name: '宇文成都', id: 1, headurl: require('../assets/logo.png'), bol: false }, ? ? ? ? { name: '宇文護', id: 1, headurl: require('../assets/logo.png'), bol: false }, ? ? ? ? { name: '宇文拓', id: 1, headurl: require('../assets/logo.png'), bol: false } ? ? ? ], ? ? ? arrR: [] ? ? } ? }, ? methods: { ? ? // 點擊全選 ? ? all() { ? ? ? // 清空一個數(shù)組 ? ? ? this.arrR = []; // 第一種 ? ? ? // this.arrR.length = 0; // 第二種 ? ? ? this.arrL.map(item => { ? ? ? ? item.bol = this.allBol; ? ? ? ? if(this.allBol) { ? ? ? ? ? this.arrR.push(item); ? ? ? ? } ? ? ? }) ? ? }, ? ? // 點擊單選 ? ? only(n) { ? ? ?if (this.arrL[n].bol) { ? ? ? ?this.arrR.push(this.arrL[n]) ? ? ?}else { ? ? ? ?let index = this.arrR.indexOf(this.arrL[n]); ? ? ? ?this.arrR.splice(index,1); ? ? ? ?this.allBol = false ? ? ?} ? ? }, ? ? // 刪除 ? ? del(obj, index) { ? ? ? let n = this.arrL.indexOf(obj); ? ? ? this.arrL[n].bol = false; ? ? ? this.arrR.splice(index, 1) ? ? }, ? ? // 點擊確定 ? ? sure() { ? ? ? let arr = []; ? ? ? this.arrR.map(item => { ? ? ? ? arr.push(item.name) ? ? ? }) ? ? ? this.$emit('ok', arr.join(',')); ? ? }, ? ? // 點擊取消 ? ? cancel() { ? ? ? this.$emit('cancel') ? ? }, ? } }; </script> <style scoped> .mark { ? width: 100%; ? height: 100%; ? background: rgba(0, 0, 0, 0.5); ? position: fixed; ? left: 0; ? top: 0; ? z-index: 1000; ? display: flex; ? align-items: center; ? justify-content: center; } .alert{ ? background: #fff; ? width: 800px; ? height: 600px; } .cont { ? display: flex; ? height: 500px; } .cont>div { ? flex: 1; } .cont>div:nth-child(1) { ? border-right: 1px #ccc solid; } .btns { ? height: 100px; ? text-align: center; } .left dd { ? height: 50px; ? margin-bottom: 10px; ? position: relative; } .left dd label { ? display: flex; ? height: 50px; ? align-items: center; ? cursor: pointer; } .left dd label:hover { ? background: #f0f0f0; } .left dd label img { ? width: 50px; ? height: 50px; ? border-radius: 50%; ? margin: 0 5px; } .left dd em { ? cursor: pointer; ? position: absolute; ? width: 50px; ? height: 50px; ? font-size: 30px; ? text-align: center; ? line-height: 50px; ? right: 0; ? top: 0; ? transition: all 0.8s; } .left dd em:hover{ ? transform: rotate(360deg); } </style>
在App.vue 里面寫
<template> ? <div id="app"> ? ? <div id="nav"> ? ? ? <router-link to="/select">選擇管理員</router-link> ? ? </div> ? ? <router-view/> ? </div> </template>
在router 下面的index.js 里面添加路由
import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' Vue.use(VueRouter) const routes = [ ? { ? ? path: '/', ? ? name: 'Home', ? ? component: Home ? }, ? { ? ? path: '/about', ? ? name: 'About', ? ? component: function () { ? ? ? return import('../views/About.vue') ? ? } ? }, ? // 創(chuàng)建的路由 ? { ? ? path: '/select', ? ? name: 'Select', ? ? component: () => import('../components/selest.vue') ? } ] const router = new VueRouter({ ? mode: 'history', ? base: process.env.BASE_URL, ? routes }) export default router
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解vue axios用post提交的數(shù)據(jù)格式
這篇文章主要介紹了詳解vue axios用post提交的數(shù)據(jù)格式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08解決VUE 在IE下出現(xiàn)ReferenceError: Promise未定義的問題
這篇文章主要介紹了解決VUE 在IE下出現(xiàn)ReferenceError: Promise未定義的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11基于elementUI實現(xiàn)圖片預覽組件的示例代碼
這篇文章主要介紹了基于elementUI實現(xiàn)圖片預覽組件的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03