位置: IT常识 - 正文

vue3使用viewer(vue viewui)

编辑:rootadmin
vue3使用viewer 介绍

推荐整理分享vue3使用viewer(vue viewui),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue3使用ref获取元素,vue3使用viewui,vue3使用ref获取元素,vue3使用markdown,vue3使用vuex还是pinia,vue3使用ref,vue3使用viewui,vue3使用vuex,内容如对您有帮助,希望把文章链接给更多的朋友!

v-viewer是一款基于viewer.js的强大的插件,不但支持vue3版本,还支持vue2、JavaScript、jquery,有以下特点:

支持移动设备触摸事件支持响应式支持放大/缩小支持旋转(类似微博的图片旋转)支持水平/垂直翻转支持图片移动支持键盘支持全屏幻灯片模式(可做屏保)支持缩略图支持标题显示支持多种自定义事件官网

官方网站 网站里介绍了三种用法,基本用法写的很详细了,这边就不再赘述,主要讲讲我使用这个插件的心路历程

需求

接到任务要求写一个图片查看标注系统,主要功能就是能查看图片、缩放,并对图片进行添加、删除标签的操作,并且明确不要用element的图片查看器,说是不好用……随手一百度就能找到这款viewer.js,经过一番探索,发现居然还有支持vue2、支持vue3的版本,开心!就决定用你啦!(还是开心的太早了……)

系统功能

系统包含一下功能:

切换图片要获取到当前展示的哪一张图片点击图片缩略图可以重新排序并展示当前图片能给当前图片添加标签切换图片时获取当前图片的标签并展示标签构造页面

页面形式确定后,首先想到是用API形式使用,因为可以随时随地想用就用,但就发现问题了,那就是inline模式下,查看大图的窗口没有办法关闭,会一直罩着,于是就放弃API了,然后就不知道为啥,经过尝试之后选择directive,以指令形式使用,其实组件和指令的都是大同小异,大家看着来就行。

改善页面

inline模式下,查看大图会一直无法关闭,怎么办呢? 改下页面布局,上面部分是图片的缩略图 预想的是点击图片下方主页面部分会切换图片的展示, HTML部分代码如下

<div class="left"> <div class="imgs" v-viewer.rebuild="options" > <template > <div v-for="src in showimages" :key="src.name"> <img :src="src.url" class="img" > </div> </template> </div></div>

官方例子中,<template>中是没有<div>标签的,因为<template>标签上加key会报错,所以就再加了一层<div>包裹,也就是这个<div>,导致我走了不少弯路,为什么呢?在我们往绑定的showimages里面添加、删除元素时,整个页面看起来没有变化……也就是这个变动对viewer来说没有生效。

解决办法

经过研究,在viewer.js中,有一个判断image数组是否发生变化的函数imageDiff,在这个函数中,判断是否发生变化的条件是,获取指令元素的子元素中的<img>标签,而我用<div>包裹住了<img>会导致找到的<img>个数为0,所以导致无法更新image数组。所以解决的办法就是直接写<img>元素或者template下不要写<div>直接写<img> 就这个问题,看了一早上才看出来问题…呜呜呜我是菜鸡前端。解决这个问题之后,只需要改变数组顺序,对数组进行操作就可以实现切换展示图片。

切换图片的回调函数

当用户点击viewer切换图片时,我希望最上面的图片展示也能切换到当前展示的这一张,对viewer来说,有提供几个回调函数,其中就有view和viewed,所以我们只需要在options中定义好view函数,就可以获取到当前展示的数据的下标,或者e.detail.image对象下有src和currentSrc字段分别记录了图片文件名和图片路径,可以根据这个高亮显示当前选中的图片。 其实是有更好的方法,就是viewer自带view(index)方法可以指定查看图片的下标,但是这个我调用了没有生效,所以暂时用 平替方法代替,就是修改viewer展示数组顺序,这样会销毁重绘viewer,使用起来体验是没什么区别的。

选中的图片高亮显示

选中图片的高亮显示只需要动态获取当前选中图片的key或者id,动态添加css就可以了。 在viewer中,有一个图片切换完成的回调函数viewed,还有切换图片之前的回调函数view,可以根据需要在option中设置。当用户点击上一张或下一张的时候,回调函数的入参会有e.detail.index,这个是当前图片在数组中的下标,可以根据下标获取到当前图片的信息。

其他整理

这部分大都来自于viewer.js官方文档,v-viewer是基于viewer.js的

Options

用法:

Viewer.setDefaults(options)options = {inline :true,fullsreen: false}inlineType: BooleanDefault : false 是否启用inline模式—inline模式就是在区域内展示,不是全屏幕覆盖backdropType: Boolean or StringDefault : true

启用modal背景,为单击时不会关闭模态的背景指定静态

buttonType: BooleanDefault : true

是否显示右上角关闭按钮

navbar

是否显示缩略图导航

Type: Boolean or NumberDefault : trueOptions : 0 or false: 隐藏缩略图导航 1 or true: 显示缩略图导航 2: 仅当屏幕宽度大于768像素时显示导航栏 3: 仅当屏幕宽度大于992像素时显示导航栏 4: 仅当屏幕宽度大于1200像素时显示导航栏title

指定标题的可见性和内容

Type: Boolean or Number or Function or ArrayDefault: trueOption: 0 or false: 不显示标题 1 or true or Function or Array:显示标题 2: 仅当屏幕宽度大于768像素时显示标题 3: 仅当屏幕宽度大于992像素时显示标题 4: 仅当屏幕宽度大于1200像素时显示标题 Function: 自定义标题内容 [Number, Function]: Number指示可见性,Function自定义标题内容toolbar

指定工具栏及其按钮的可见性和布局。

Type: Boolean or Number or ObjectDefault: trueOptions: 0 or false: 隐藏工具栏. 1 or true: 显示工具栏. 2: 仅当屏幕宽度大于768像素时显示工具栏 3: 仅当屏幕宽度大于992像素时显示工具栏 4: 仅当屏幕宽度大于1200像素时显示工具栏 { key: Boolean | Number }: 显示或隐藏工具栏. { key: String }: 自定义工具栏大小(size). { key: Function }: 自定义工具栏按钮的单击处理函数. { key: { show: Boolean | Number, size: String, click: Function }: 自定义按钮的每个属性. Available built-in keys(key可选项): “zoomIn”, “zoomOut”, “oneToOne”, “reset”, “prev”, “play”, “next”, “rotateLeft”, “rotateRight”, “flipHorizontal”, “flipVertical”. Available built-in sizes(size可选项): “small”, “medium” (default) and “large”.className

要添加到viewer根元素的自定义类名。

Type: StringDefault: ''container

Document.querySelector的元素或有效选择器,用于将viewer置于modal模式的容器。只在inline:false时有效

Type: Element or StringDefault: 'body'

没用过,不知道具体能填哪些字段

filter

指定筛选图片的函数,会遍历每张图片,return true的图片展示,return false的图片隐藏,所以function应有返回值

Type: FunctionDefault: null

注意,默认情况下,没有src属性集的图像将被忽略

fullscreen

自动播放时是否全屏

Type: Boolean or FullscreenOptionsDefault: trueinheritedAttributes

定义要从原始图像继承的额外属性。

Type: ArrayDefault: ['crossOrigin', 'decoding', 'isMap', 'loading', 'referrerPolicy', 'sizes', 'srcset', 'useMap']

注意,基本属性src和alt将始终继承自原始图像。

initialCoverage

定义查看图像的初始覆盖范围。它必须是介于0(0%)和1(100%)之间的正数。

Type: NumberDefault: 0.9initialViewIndex

定义要查看的图像的初始索引。指定开始查看图像的下标

Type: NumberDefault: 0

也用作视图方法的默认参数值。

inline

启用内联模式

Type: BooleanDefault: falseinterval

播放时自动循环图像之间的间隔时间。单位:hms

Type: NumberDefault: 5000keyboard

是否允许用键盘操作(操作放大缩小、上一张下一张的功能)

Type: BooleanDefault: true focus

Focus the active item in the navbar when initialized.

Type: BooleanDefault: true

需要将keyboard设置为true

loading

加载图像时是否显示加载微调器。

Type: BooleanDefault: trueloop

是否启用循环查看(查看到最后一张,再点下一张跳转到第一张)

Type: BooleanDefault: trueminWidth

指定viewer的最小宽度

Type: NumberDefault: 200

只在inline是true时生效

minHeight

指定viewer的最小宽度

Type: NumberDefault: 100

只在inline是true时生效

movable

是否可以移动图片

Type: BooleanDefault: truerotatable

是否允许旋转图片

Type: BooleanDefault: truescalable

是否可以缩放图像

Type: BooleanDefault: truezoomable

是否可以缩放图像

Type: BooleanDefault: truezoomOnTouch

是否开启触摸缩放图像功能

Type: BooleanDefault: truezoomOnWheel

是否开启鼠标缩放图像功能

Type: BooleanDefault: trueslideOnTouch

通过在触摸屏上滑动,可以滑动到下一个或上一个图像

Type: BooleanDefault: truetoggleOnDblclick

是否在双击图像时在其自然大小和初始大小之间切换图像大小;双击图像时自动调用切换方法

Type: BooleanDefault: truetooltip

是否显示缩放百分比

Type: BooleanDefault: truetransition

是否使用CSS3过度

Type: BooleanDefault: truezIndex

图片查看器modal模式时z-index值

Type: NumberDefault: 2015zIndexInline

图片查看器inline模式时z-index值

Type: NumberDefault: 0zoomRatio

鼠标滚轮滚动时缩放比例

Type: NumberDefault: 0.1minZoomRatio

最小缩放比例

Type: NumberDefault: 0.01 Define the min ratio of the image when zooming out.maxZoomRatio

最大缩放比例

Type: NumberDefault: 100 Define the max ratio of the image when zooming in.url

设置查看图片时的图片地址来源 如果它是一个字符串,它应该是每个图像元素的属性之一。 如果它是一个函数,它应该返回一个有效的图像URL。

Type: String or FunctionDefault: 'src'ready

回调函数,就绪时调用

Type: FunctionDefault: nullshow

回调函数,加载展示图层前调用

Type: FunctionDefault: nullshown

回调函数,加载展示图层完成后调用

Type: FunctionDefault: nullhide

回调函数,点击关闭展示按钮时调用

Type: FunctionDefault: nullhidden

回调函数,展示图层关闭前调用

Type: FunctionDefault: nullview

回调函数,加载展示图片前调用

Type: FunctionDefault: nullviewedvue3使用viewer(vue viewui)

回调函数,加载展示图片后调用

Type: FunctionDefault: nullmove

回调函数,图片移动时调用

Type: FunctionDefault: nullmoved

回调函数,图片异动后调用

Type: FunctionDefault: nullrotate

回调函数,图片旋转前调用

Type: FunctionDefault: nullrotated

回调函数,图片旋转后调用

Type: FunctionDefault: nullscale

回调函数,图片缩放前调用

Type: FunctionDefault: nullscaled

回调函数,图片缩放后调用

Type: FunctionDefault: nullzoom

回调函数,图片缩放前调用

Type: FunctionDefault: nullzoomed

回调函数,图片缩放后调用

Type: FunctionDefault: nullplay

回调函数,图片开始自动播放时调用

Type: FunctionDefault: nullstop

回调函数,图片停止自动播放时调用

Type: FunctionDefault: nullmethod

用法:获取到实例

const viewer = this.$el.querySelector('.viewer').$viewer viewer.show()show([immediate])

是否立即显示查看器,只在modal模式下有效

immediate (optional):

Type: BooleanDefault: falseview([index])

使用viewer查看下标为index的图片。如果viewer被隐藏,改图片将首先显示

index (optional):

Type: NumberDefault: 0 (继承自 initialViewIndex )展示图片的下标viewer.view(1); // 展示下标是1的图片(第二张)prev([loop=false])

是否设置第一张图片的上一张是最后一张图片(头尾相接)

loop (optional):

Type: BooleanDefault: falsenext([loop=false])

是否设置最后一张图片的下一张是第一一张图片(头尾相接)

loop (optional):

Type: BooleanDefault: falsemove(x[, y = x])

使用相对偏移移动图像

x:

Type: Number水平方向上的移动距离

y (optional):

Type: Number竖直方向上的移动距离如果不存在,则其默认值为x

Move the image with relative offsets.

viewer.move(1);viewer.move(-1, 0); // 左移viewer.move(1, 0); // 右移viewer.move(0, -1); // 上移viewer.move(0, 1); // 下移moveTo(x[, y = x])

移动图像到指定位置

x:

Type: Number新位置的水平坐标

y (optional):

Type: Number指定位置的竖直坐标如不存在,默认和 x相同.rotate(degree)

在原来的基础上旋转图像

degree:

Type: Number向右旋转: 输入正数 (degree > 0)向左旋转: 输入负数 (degree < 0)viewer.rotate(90);viewer.rotate(-90);rotateTo(degree)

旋转图像至指定角度

degree:

Type: Numberviewer.rotateTo(0); // 转到0°viewer.rotateTo(360); // 转一圈scale(scaleX[, scaleY])

图像翻转

scaleX:

Type: NumberDefault: 1竖直方向翻转输入1时,不起作用

scaleY (optional):

Type: Number竖直方向翻转为空时,等于xviewer.scale(-1); // Flip both horizontal and verticalviewer.scale(-1, 1); // Flip horizontalviewer.scale(1, -1); // Flip verticalscaleX(scaleX)

图像水平方向翻转

scaleX:

Type: NumberDefault: 1图像水平方向翻转viewer.scaleX(-1); // Flip horizontalscaleY(scaleY)

图像竖直方向翻转

scaleY:

Type: NumberDefault: 1图像竖直方向翻转取1时不起作用,不会发生变化viewer.scaleY(-1); // Flip verticalzoom(ratio[, showTooltip[, pivot]])

以指定比例缩放图像

ratio:

Type: NumberZoom in: 放大:正数 (ratio > 0)Zoom out: 缩小:负数 (ratio < 0)

showTooltip (optional):

Type: BooleanDefault: false是否展示缩放比例等提示信息

pivot (optional):

Type: ObjectDefault: nullSchema: { x: Number, y: Number }缩放的轴心点坐标viewer.zoom(0.1);viewer.zoom(-0.1);zoomTo(ratio[, showTooltip[, pivot]])

缩放图像到指定比例

ratio:

Type: Number需要正数 (ratio > 0)

showTooltip (optional):

Type: BooleanDefault: false是否展示缩放比例等提示信息

pivot (optional):

Type: ObjectDefault: nullSchema: { x: Number, y: Number }缩放的轴心点坐标viewer.zoomTo(0); // Zoom to zero size (0%)viewer.zoomTo(1); // Zoom to natural size (100%)// Zoom to 50% from the center of the window.viewer.zoomTo(.5, { x: window.innerWidth / 2, y: viewer.innerHeight / 2,});play([fullscreen])fullscreen (optional):

Type: Boolean or FullscreenOptionsDefault: falseIndicate if request fullscreen or not.

Play the images.

stop()

停止播放

full()

inline模式下有效,播放时全屏

exit()

退出全屏 inline模式下有效

Events

所有事件都可以在其处理程序中使用this.viewe访问查看器实例。

Be careful to use these events with other components which have the same event names, e.g.: Bootstrap’s modal.

let viewer;image.addEventListener('viewed', function () { console.log(this.viewer === viewer); // > true});viewer = new Viewer(image);ready

viewer 准备好的时候会触发此事件,在modal模式下,只有点击查看了图片才会触发此事件

event.bubbles: trueevent.cancelable: trueevent.detail: nullshow

在viewer show的时候会触发此事件,只在modal 模式下有效

event.bubbles: trueevent.cancelable: trueevent.detail: nullshownevent.bubbles: trueevent.cancelable: trueevent.detail: null

This event fires when the viewer modal has shown.

Only available in modal mode.

hideevent.bubbles: trueevent.cancelable: trueevent.detail: null

This event fires when the viewer modal starts to hide.

Only available in modal mode.

hiddenevent.bubbles: trueevent.cancelable: falseevent.detail: null

This event fires when the viewer modal has hidden.

Only available in modal mode.

viewevent.bubbles: trueevent.cancelable: trueevent.detail.index:

Type: NumberThe index of the original image.event.detail.image:

Type: HTMLImageElementThe current image (a clone of the original image).event.detail.originalImage:

Type: HTMLImageElementThe original image.

This event fires when a viewer starts to show (view) an image.

viewedevent.bubbles: trueevent.cancelable: falseevent.detail: the same as the view event.

This event fires when a viewer has shown (viewed) an image.

moveevent.bubbles: trueevent.cancelable: trueevent.detail.x:

Type: NumberThe new position in the horizontal direction.event.detail.y:

Type: NumberThe new position in the vertical direction.event.detail.oldX:

Type: NumberThe old position in the horizontal direction.event.detail.oldY:

Type: NumberThe old position in the vertical direction.event.detail.originalEvent:

Type: Event or nullOptions: pointermove, touchmove, and mousemove.

This event fires when a viewer starts to move an image.

movedevent.bubbles: trueevent.cancelable: falseevent.detail: the same as the move event.

This event fires when a viewer has moved an image.

rotateevent.bubbles: trueevent.cancelable: trueevent.detail.degree:

Type: NumberThe new rotation degrees.event.detail.oldDegree:

Type: NumberThe old rotation degrees.

This event fires when a viewer starts to rotate an image.

rotatedevent.bubbles: trueevent.cancelable: falseevent.detail: the same as the rotate event.

This event fires when a viewer has rotated an image.

scaleevent.bubbles: trueevent.cancelable: trueevent.detail.scaleX:

Type: NumberThe new scaling factor in the horizontal direction.event.detail.scaleY:

Type: NumberThe new scaling factor in the vertical direction.event.detail.oldScaleX:

Type: NumberThe old scaling factor in the horizontal direction.event.detail.oldScaleY:

Type: NumberThe old scaling factor in the vertical direction.

This event fires when a viewer starts to scale an image.

scaledevent.bubbles: trueevent.cancelable: falseevent.detail: the same as the scale event.

This event fires when a viewer has scaled an image.

zoomevent.bubbles: trueevent.cancelable: trueevent.detail.ratio:

Type: NumberThe new (next) ratio of the image (imageData.width / imageData.naturalWidth).event.detail.oldRatio:

Type: NumberThe old (current) ratio of the image.event.detail.originalEvent:

Type: Event or nullOptions: wheel, pointermove, touchmove, and mousemove.

This event fires when a viewer starts to zoom (in or out) an image.

zoomedevent.bubbles: trueevent.cancelable: falseevent.detail: the same as the zoom event.

This event fires when a viewer has zoomed (in or out) an image.

playevent.bubbles: trueevent.cancelable: trueevent.detail: null

This event fires when the viewer starts to play.

You can abort the playing process by calling event.preventDefault().

stopevent.bubbles: trueevent.cancelable: trueevent.detail: null

This event fires when the viewer starts to stop.

You can abort the stopping process by calling event.preventDefault().

本文链接地址:https://www.jiuchutong.com/zhishi/298977.html 转载请保留说明!

上一篇:echarts的series——折线图,饼图,柱状图,散点图的配置(echarts series name)

下一篇:首个ChatGPT开发的应用上线;ChatMind思维导图工具;中文提示词大全;Copilot平替 | ShowMeAI日报

  • 亏损企业如何填报企业所得税
  • 退多缴纳的所得税 现金流如何处理
  • 小规模纳税人增值税减免账务处理
  • 交通补贴算工资收入吗
  • 子公司分红母公司要不要交税
  • 外包和离岸外包一样吗
  • 房地产企业的预收账款包括什么
  • 进料深加工境外付汇
  • 营改增后如何纳税
  • 收到工程施工进度款如何记账
  • 购置房产按揭应注意事项
  • 退货后发票还能拿去抵税吗
  • 一般纳税人可以不交增值税吗
  • 营改增租赁服务有哪些
  • 金融行业小规模纳税人税率
  • 购房时收取的卖方费用
  • 空调安装费开票属于什么类
  • w10系统怎么打印文件
  • 暂估成本跨年怎么算
  • 民间非营利组织有哪些
  • 会计如果做假账
  • 如何在 Windows 11 上打开 APK 文件
  • 商票 银行贴现
  • php单例模式demo
  • 比弗利山庄安全吗治安
  • 车子的保险费
  • php实现5分钟倒计时
  • php的序列化操作生成的哪种格式
  • js怎么实现倒计时
  • wordpress静态分离
  • 员工辞退补偿金需要交个税吗
  • 跨年专票红冲步骤
  • 分公司可以分配利润吗
  • 酒店低值易耗品怎么摊销
  • java数组合并变字符串
  • 将织梦dedecms转换到wordpress
  • 支付工程款的会计科目怎么写
  • 工程公司的材料员考试难吗
  • 直接人工费用属于产品成本吗
  • 企业应缴纳税款
  • 临时贷款属于什么会计科目
  • 固定资产投资额是指什么
  • sql 临时表格
  • 公司开发新产品的建议
  • 收员工伙食费会计分录
  • 民办非企业单位工会经费
  • 公司贷款可以转私户吗
  • 新成立的小规模纳税人什么时候报税
  • 跨年度发票的报表怎么填
  • 开办费新会计准则
  • 房产契税什么时候退税
  • 出口确认收入的时间
  • 接受捐赠可以确认为收入吗
  • 固定资产报废如何记账
  • 车票抵扣填在申请表哪里
  • 企业管理费的计算基础
  • 员工垫付的费用怎样记账
  • 上海残疾人就业保障金计算公式
  • 公对公转账必须签合同吗
  • 小企业持有的不良资产
  • sqlserver1053怎么解决
  • CentOs7.x安装Mysql的详细教程
  • linux查看硬件信息的命令
  • Centos 6.4 安装dnsmasq的方法
  • java.exe是指
  • 如何选择性地隐藏部分任务栏程序
  • toggle button什么作用
  • 比较两个字符串的值是否相等
  • 批处理set命令
  • js特殊字符有哪些
  • jquery左右选择框
  • jquery中加载文档的方法
  • 广西税务局客服电话时间
  • 临港税务局第四分局电话
  • 平原新区郑州外国语学校
  • 企业所得税忘记交了怎么办
  • 香皂需要换着用吗
  • 境外付款需要什么手续
  • 企业自有房屋需要交税吗
  • 国外工资个人所得税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设