vue2使用ant Design时需要图片预览

我看了ant Design 1.x版本中没有图片预览功能,别的都有,歧视vue2是吧!!!

如果使用

window.open(url, "_blank");

的话,会打开新页面去查看图片,好像有点low。。。

 于是呢,我就找呀找呀找到viewer,发现还挺好用的。

1.首先需要安装

npm install v-viewer --save

  2.在main.js注册

import "viewerjs/dist/viewer.css";
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)
Viewer.setDefaults({
    Options: { 'inline': true, 'button': true, 'navbar': true, 'title': true, 'toolbar': true, 'tooltip': true, 'movable': true, 'zoomable': true, 'rotatable': true, 'scalable': true, 'transition': true, 'fullscreen': true, 'keyboard': true, 'url': 'data-source' }
})

 3.只需要在图片外面套一个viewer标签就可以

 然后配置一个images

<viewer :images="text?.split(',')" class="photos">
    <img
        v-for="(src, index) in text?.split(',')"
        :src="src"
        :key="index"
     />
</viewer>

来吧展示O(∩_∩)O~

嗯,就这么简单。。。。 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Ant Design Vue提供了一个名为"Upload"的组件,可以用来上传图片。在上传图片,可以设置"showUploadList"属性为false,这样就不会显示上传列表。同,可以设置"previewFile"属性为true,这样在上传完成后,可以点击图片预览预览功能是通过"preview"方法实现的,该方法需要传入一个图片地址作为参数。 ### 回答2: Ant Design Vue提供了图片预览组件(ImageViewer)来实现在页面中预览图片。ImageViewer支持图片的放大缩小、旋转、以及位移等操作。 使用ImageViewer组件需要引入对应的CSS样式和JavaScript文件,可以在Vue文件的script标签中import语句中引入。在template中可以使用ImageViewer组件。 ImageViewer组件中需要传入图片的url和宽高信息,如下所示: ```html <template> <a-row> <a-col v-for="(item, index) in cardList" :key="index"> <a-card hoverable :style="{ width: '240px' }" @click="showImage(item.url, item.width, item.height)" > <a-card-meta /> <div style="height: 100px; overflow: hidden"> <img :src="item.url" alt="" style="max-height: 100%" /> </div> </a-card> </a-col> <a-modal v-model="imageVisible" width="80%" :title="imageName"> <img :src="imageUrl" style="max-width: 100%" /> <a-divider /> <div style="margin-top: 10px"> <a-icon type="zoom-in" :style="{marginRight: '10px'}" @click="zoomIn" /> <a-icon type="zoom-out" :style="{marginRight: '10px'}" @click="zoomOut" /> <a-icon type="reload" :style="{marginRight: '10px'}" @click="rotate" /> </div> </a-modal> </a-row> </template> <script> import 'viewerjs/dist/viewer.css'; import Viewer from 'v-viewer'; export default { name: 'Demo', data() { return { cardList: [{ url: 'https://DummyUrl1.jpeg', width: 200, height: 150, }, { url: 'https://DummyUrl2.jpeg', width: 200, height: 150, }, { url: 'https://DummyUrl3.jpeg', width: 200, height: 150, }, ], imageUrl: '', imageName: '', imageVisible: false, viewer: null, }; }, mounted() { this.viewer = new Viewer(this.$refs.image, { navbar: false, toolbar: false, title: false, movable: false, transition: false, }); }, methods: { showImage(url, width, height) { this.imageUrl = url; this.imageName = ''; this.imageVisible = true; this.viewer.zoomTo(1); this.viewer.moveTo(0, 0); this.viewer.rotateTo(0); this.viewer.update(); if (width && height) { const aspectRatio = width / height; let imageWidth = 800; let imageHeight = 800 / aspectRatio; if (imageHeight > 600) { imageHeight = 600; imageWidth = 600 * aspectRatio; } this.viewer.setOptions({ inline: true, container: document.querySelector('.ant-modal-body'), inheritStyles: false, zoomRatio: 0.1, zoomable: true, rotatable: true, scalable: false, navbar: false, title: false, toolbar: false, viewed() { this.viewer.zoomTo(1); }, }); this.viewer.setViewOptions({ inline: true, container: document.querySelector('.ant-modal-body'), minWidth: imageWidth, minHeight: imageHeight, maxWidth: imageWidth, maxHeight: imageHeight, }); } }, zoomIn() { this.viewer.zoom(0.1); }, zoomOut() { this.viewer.zoom(-0.1); }, rotate() { this.viewer.rotate(90); }, }, }; </script> ``` 在上面的示例中,通过iv-viewer依赖引入了ImageViewer组件。在mounted中使用new Viewer()方法来初始化对象。在showImage方法中设置图片的url、宽高、名称等信息,并且设置viewer对象的参数。在模态框中使用ant-design组件的Modal实现图片预览功能。在模态框中实现了图片的放大、缩小、旋转等操作。 总之,使用Ant Design Vue图片预览组件,可以方便、快捷的实现图片预览的功能,同满足了网页开发中对于用户体验的要求。 ### 回答3: Ant Design Vue是一个开源的UI组件库,它提供了许多实用的组件和功能,其中包括图片预览图片预览是一个非常常见的需求,在网页中展示图片,用户可能会需要放大、缩小、旋转等操作,这候就可以使用Ant Design Vue图片预览组件来实现。 Ant Design Vue图片预览组件实现了以下功能: 1. 点击图片后,弹出预览框,显示当前图片的大图。 2. 预览框中可以进行放大、缩小、旋转等操作,同也可以对图片进行下载、分享等操作。 3. 支持多张图片预览,可以轮换查看图片。 4. 可以自定义预览框的样式,包括预览框的大小、背景色、边框颜色、文字样式等。 使用Ant Design Vue图片预览组件非常简单,只需要引入相应的组件即可。可以通过以下步骤实现图片预览: 1. 在vue脚本中,引入ant-design-vue组件库: ```javascript import { Image, Modal } from 'ant-design-vue'; ``` 2. 在当前页面中,使用Image组件展示需要预览图片。 ```html <image src={"图片URL地址"} /> ``` 3. 在Image组件中,使用Modal组件包裹,实现图片预览。 ```html <Modal> <Image src={"图片URL地址"} /> </Modal> ``` 4. 在Modal组件中,可以添加一些配置选项,例如:modal-title(模态框标题)、modal-footer(弹出框底部区域)、mask-closable(是否允许点击遮罩关闭模态框)等。 ```html <Modal title="预览图片" :footer="null" :maskClosable="true" width="80%"> <Image src={"图片URL地址"} /> </Modal> ``` 通过以上步骤,就可以实现简单的图片预览功能。当用户点击图片,弹出预览框,用户可以进行放大、缩小等操作。如果想要实现更多功能,可以通过添加更多配置选项的方式来实现。Ant Design Vue图片预览组件提供方便实用的功能,在构建网页非常实用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值