整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

超高效 Vue 模态弹出框组件Vue-Modal

天给大家推荐一款超美观的pc端vue.js弹窗组件VueJsModal。

vue-js-modal 基于Vue构建的Modal对话框组件。单独组件,方便使用。支持拖拽、缩放、异步调用组件。

安装

$ npm i vue-js-modal -S

引入组件

// 在main.js中引入
import Vue from 'vue'
import VModal from 'vue-js-modal'
import 'vue-js-modal/dist/styles.css'
Vue.use(VModal)

调用方式

<template>
  <modal name="example"
    :width="300"
    :height="300"
    :resizable="true"
    :draggable="true"
    :adaptive="true"
    @before-open="beforeOpen"
    @opened="Opened"
    @before-close="beforeClose"
    @closed="Closed"
  >
    Hello, VueModal!
  </modal>
</template>
<script>
export default {
  data() {
    return {}
  },
  mounted() {
    this.$modal.show('example')
  },
  methods: {
    beforeOpen(event) {
      console.log('Opening...')
    },
    Opened(event) {
      console.log('Opened...')
    },
    beforeClose(event) {
      console.log('Closing...')
    },
    Closed(event) {
      console.log('Closed...')
    }
  }
}
</script>

调用内部 show、hide 方法显示和隐藏弹窗

<template>
  <modal name="my-first-modal">
    This is my first modal
  </modal>
</template>

<script>
export default {
  mounted () {
    this.show()
  },
  methods: {
    show () {
      this.$modal.show('my-first-modal');
    },
    hide () {
      this.$modal.hide('my-first-modal');
    }
  }
}
</script>

另外还支持Modal动态调用组件

import MyComponent from './MyComponent.vue'
this.$modal.show(
  MyComponent,
  { text: 'This text is passed as a property' },
  { draggable: true }
)

// or

this.$modal.show(
  {
    template: `
      <div>
        <h1>This is created inline</h1>
        <p>{{ text }}</p>
      </div>
    `,
    props: ['text']
  },
  { text: 'This text is passed as a property' },
  { draggable: true, resizable: true },
  { 'before-close': event => {} }
)


附上模态框示例及仓库地址

# demo地址
http://vue-js-modal.yev.io/

# 项目地址
https://github.com/euvl/vue-js-modal

ok,这次就分享到这里。如果大家有其它Vue弹窗组件,欢迎留言讨论~

、teleport 介绍

teleport 传送门组件,提供一种简洁的方式,可以指定它里面的内容的父元素。通俗易懂地讲,就是 teleport 中的内容允许我们控制在任意的 DOM 中,使用简单。

使用语法:

<teleport to="body">
 <div>
  需要创建的内容
 </div>  
</teleport>

to 属性是指定 teleport 中的内容加入的 DOM 元素。可以是标签名,也可以是 id 或类名。

//标签名  。上述实例就是加入body元素内,使用的是标签名。
<teleport to="body"></teleport>

//类名。如:to=".className"
<teleport to=".className"></teleport>

//id名
<teleport to="#idName"></teleport>

1.1、多个 teleport 使用

多个 teleport 传送门组件可以将内容都挂载到一个目标上,多个 teleport 组件内容就是兄弟节点,先挂载的在前面,后挂载的在后面。

使用如下:

<teleport to="body">
 <div class="first">
  第一个挂载元素
 </div>  
</teleport>
<teleport to="body">
 <div class="second">
  第二个挂载元素
 </div>  
</teleport>

运行结果如图:

上边的实例等价于:

<teleport to="body">
 <div class="first">
  第一个挂载元素
 </div>
 <div class="second">
  第二个挂载元素
 </div>
</teleport>

二、为什么使用 teleport

使用 vue 开发时,都是多个组件之间不断地嵌套,处理元素的样式或者层级的时候就会变得困难。如我们需要添加一个 modal 模态框或 toast 提示框,如果我们把这样的框可以从 vue 组件中剥离出来,我们样式和层级设置起来会更加简便。

有些同学会想,这直接放到 index.html 中不就好了吗?另外 modal 、toast 元素需要使用 vue 组件的状态值,通过状态控制 modal、toast 的隐藏显示。如果直接放入 index.html 则状态控制就复杂了。

所以 teleport 传送门组件就派上用场了。有点像“哆啦A梦”的任意门,可以把元素传送到任意的元素内。同时还可以使用 vue 组件内的状态值控制它。

三、teleport 应用

使用 vite + vue 3 创建的项目,具体如何创建项目请查看《什么,你还使用 webpack?别人都在用 vite 搭建项目了》文章。

vue 3 的项目创建完成之后,找到 index.htm 文件,添加:

<div id="newModal"></div>	

组件文件内,添加 teleport 组件:

<button @click="showModal" class="btn">打开 modal </button>
<!-- to 属性就是目标位置 -->
<teleport to="#newModal">
 <div v-if="visible">
  <div >我是一个 Modal 框</div>
  </div>
</teleport>

运行结果,我们发现使用的 teleport 组件,通过 to 属性,将内容传送到<div id="newModal"></div>内,该元素与<div id="app"></div>同级。此时 teleport 中的元素隐藏显示完全由 vue 组件内的状态值决定。

四、初学者容易遇到的坑

有些同学在自己的项目内,直接引入了 teleport 传送门组件,运行以后发现该组件原样输出了,并没有被解析,同时还会报错。

错误信息如下:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <teleport> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

然后就在网上各种查解决办法,最后发现压根找不到!

根本原因是你使用的还是 vue2,不是 vue3。有些同学会把 脚手架 vue-cli 3 创建的项目,当作是 vue3 。vue-cli 2 和 vue-cli 3 创建项目与是否是 vue3 没有必然联系的。

为大家介绍如何使用 CSS 来布局图片。


圆角图片

实例

圆角图片:

img {

border-radius: 8px;

}

实例

椭圆形图片:

img {

border-radius: 50%;

}

尝试一下 »


缩略图

我们使用 border 属性来创建缩略图。

实例

img {

border: 1px solid #ddd;

border-radius: 4px;

padding: 5px;

}

<img src="paris.jpg" alt="Paris">

尝试一下 »

实例

a {

display: inline-block;

border: 1px solid #ddd;

border-radius: 4px;

padding: 5px;

transition: 0.3s;

}

a:hover {

box-shadow: 0 0 2px 1px rgba

(0, 140, 186, 0.5);

}

<a href="paris.jpg">

<img src="paris.jpg" alt="Paris">

</a>


响应式图片

响应式图片会自动适配各种尺寸的屏幕。

实例中,你可以通过重置浏览器大小查看效果:

如果你需要自由缩放图片,且图片放大的尺寸不大于其原始的最大值,则可使用以下代码:

实例

img {

max-width: 100%;

height: auto;

}

尝试一下 »

提示: Web 响应式设计更多内容可以参考 CSS 响应式设计教程。


图片文本

如何定位图片文本:

实例

左下角左上角右上角右下角居中

尝试一下:

左上角 » 右上角 » 左下角 » 右下角 » 居中 »


卡片式图片

实例

div.polaroid {

width: 80%;

background-color: white;

box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);

}

img {width: 100%}

div.container {

text-align: center;

padding: 10px 20px;

}


图片滤镜

CSS filter 属性用为元素添加可视效果 (例如:模糊与饱和度) 。

注意: Internet Explorer 或 Safari 5.1 (及更早版本) 不支持该属性。

实例

修改所有图片的颜色为黑白 (100% 灰度):

img {

-webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */

filter: grayscale(100%);

}

提示: 访问 CSS 滤镜参考手册 查看更多内容。


响应式图片相册

实例

.responsive {

padding: 0 6px;

float: left;

width: 24.99999%;

}

@media only screen and (max-width: 700px){

.responsive {

width: 49.99999%;

margin: 6px 0;

}

}

@media only screen and (max-width: 500px){

.responsive {

width: 100%;

}

}

尝试一下 »


图片 Modal(模态)

本实例演示了如何结合 CSS 和 JavaScript 来一起渲染图片。

首先,我们使用 CSS 来创建 modal 窗口 (对话框), 默认是隐藏的。

然后,我们使用 JavaScript 来显示模态窗口,当我们点击图片时,图片会在弹出的窗口中显示:

实例

// 获取模态窗口

var modal = document.getElementById('myModal');

// 获取图片模态框,alt 属性作为图片弹出中文本描述

var img = document.getElementById('myImg');

var modalImg = document.getElementById("img01");

var captionText = document.getElementById("caption");

img.onclick = function(){

modal.style.display = "block";

modalImg.src = this.src;

modalImg.alt = this.alt;

captionText.innerHTML = this.alt;

}

// Get the <span> element that closes the modal

var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal

span.onclick = function() {

modal.style.display = "none";

}

如您还有不明白的可以在下面与我留言或是与我探讨QQ群308855039,我们一起飞!