今天产品提出了一个查看影像的功能需求。

在查看单据的列表中,有一列是影像字段,一开始根据单据号调用接口查看是否有图片附件,如果有则弹出一个全屏的弹出层,如果没有给出提示。而且,从列表进入详情之后,附件那边也会有一个查看影像的按钮。

所以,根据需求,多个组件需要用到查看影像的功能,所以考虑做一个公用组件,通过组件传值的方法将查看影像文件的入参传过去。

后来,产品要求图片可以旋转缩放。

废话不多说,贴上代码:

<template>
<div class="filePreview">
<el-dialog
class="imgList"
title="预览图片列表"
:visible.sync="imgListShow"
@close="$emit('remove')"
fullscreen>
<div class="allImg">
<div style="width:200px;height:100%;margin-top:50px;overflow-y: auto;margin: 0 auto;">
<img v-for="(item,index) in imgList" :key="item.fileid" :src='item.furl' :class="{ changeColor:changeColor == index}" @click="handlerImg(item,index)">
</div>
</div>
<div style="width:70%;float:left">
<el-pagination
style="margin-bottom:20px;"
background
@size-change="handleSizeChange"
@current-change="handleCurrentChange1"
:current-page.sync="currentImg"
:page-size="1"
layout="prev, pager, next, jumper"
:total="num">
</el-pagination>
<div style="width:50%;text-align:center;margin:20px 0">
<button @click="rotateL" icon="el-icon-arrow-left">
<i class="el-icon-arrow-left"></i>左旋转
</button>
<button @click="rotateR">右旋转
<i class="el-icon-arrow-right"></i>
</button> <button @click="scale">
<i class="el-icon-zoom-out"></i>缩小
</button>
<button @click="scale1">放大
<i class="el-icon-zoom-in"></i>
</button> </div>
<div id="test_3" @mousemove="move" @mouseup="stop">
<p @mousedown="start" >
<img :src="furl" ref="singleImg" class="originStyle">
</p>
</div>
</div>
</el-dialog>
</div>
</template> <script>
import {isgetFilePath}from 'api/public_api.js' export default {
data() {
return {
imgList:[],
imgListShow:false,
num:0,
furl:'',
currentImg:1,
changeColor:-1,
currentRotate: 0 ,
currentScale:1,
canDrag: false,
offset_x:0,
offset_y:0,
mouse_x:0,
mouse_y:0,
}
},
props:['filePreviewShow','FDJH'],
created() {
this.imgListShow = this.filePreviewShow
this.preview()
},
methods: {
//点击图片显示
handlerImg(obj,index){
this.currentRotate = 0
this.currentScale = 1
this.rotateScale()
this.$refs.singleImg.style.left = 0
this.$refs.singleImg.style.top = 0
this.furl = obj.furl
this.changeColor = index
this.currentImg = index+1
},
//影像
preview(){
let data = {
// FDJH:'000002'
FDJH:this.FDJH
}
isgetFilePath(data).then(res=>{
// console.log(res)
if(res.TYPE == "S"){
this.imgList = JSON.parse(res.MESSAGE)
this.num = this.imgList.length
if(this.imgList.length > 0){
this.furl = this.imgList[0].furl
this.changeColor = 0
}else{
this.$message.warning('影像文件为空')
}
}else{
this.$message.warning(res.MESSAGE)
} })
},
handleSizeChange(val) {
console.log(`每页 ${val} 条`);
},
handleCurrentChange1(val) {
this.currentRotate = 0
this.currentScale = 1
this.rotateScale()
this.$refs.singleImg.style.left = 0
this.$refs.singleImg.style.top = 0
this.furl = this.imgList[val-1].furl
this.changeColor = val-1
},
start(e){
//鼠标左键点击
e.preventDefault && e.preventDefault(); //去掉图片拖动响应
if(e.button==0){
this.canDrag=true;
//获取需要拖动节点的坐标
this.offset_x = document.getElementsByClassName('originStyle')[0].offsetLeft;//x坐标
this.offset_y = document.getElementsByClassName('originStyle')[0].offsetTop;//y坐标
//获取当前鼠标的坐标
this.mouse_x = e.pageX;
this.mouse_y = e.pageY;
}
},
move(e){
e.preventDefault && e.preventDefault()
if(this.canDrag==true){
let _x = e.pageX - this.mouse_x;
let _y = e.pageY - this.mouse_y;
//设置移动后的元素坐标
let now_x = (this.offset_x + _x ) + "px";
let now_y = (this.offset_y + _y ) + "px"; document.getElementsByClassName('originStyle')[0].style.top = now_y
document.getElementsByClassName('originStyle')[0].style.left = now_x
}
},
stop(e){
this.canDrag = false;
}, //旋转放大
rotateScale(){
this.$refs.singleImg.style.OTransform = 'rotate('+this.currentRotate+'deg)'+'scale('+this.currentScale+')'
this.$refs.singleImg.style.webkitTransform = 'rotate('+this.currentRotate+'deg)'+'scale('+this.currentScale+')'
this.$refs.singleImg.style.MozTransform = 'rotate('+this.currentRotate+'deg)'+'scale('+this.currentScale+')'
this.$refs.singleImg.style.msTransform = 'rotate('+this.currentRotate+'deg)'+'scale('+this.currentScale+')'
this.$refs.singleImg.style.transform = 'rotate('+this.currentRotate+'deg)'+'scale('+this.currentScale+')'
},
//旋转
rotateL(){
this.currentRotate += 15
this.rotateScale()
},
rotateR(){
this.currentRotate -= 15
this.rotateScale()
},
//缩放
scale(){
this.currentScale -= 0.1
if(this.currentScale <= 0.1){
this.currentScale = 0.1
this.rotateScale()
}else{
this.rotateScale()
}
},
scale1(){
this.currentScale += 0.1
this.rotateScale()
},
}
}
</script> <style rel="stylesheet/scss" lang="scss" slot-scope="scope">
.filePreview{
.imgList{
.el-dialog__headerbtn{
font-size:26px;
}
.el-dialog__body{
.allImg{
width:30%;
float:left;
height:600px;
img{
width: 150px;
height: 150px;
margin: 5px;
cursor: pointer;
}
.changeColor{
border:4px solid #409eff;
}
}
}
}
.originStyle{
position:absolute;
left:0;top:0;
cursor: pointer;
// transform-origin: 50% 50%;
}
#test_3{
position: relative;
width: 600px;
height: 400px;
overflow: hidden;
// overflow: scroll;
& > p{
position: absolute;
cursor: move;
transform-origin: center;
width: 100%;
height: 100%;
padding: 0;
-webkit-margin-before: 0;
-webkit-margin-after: 0;
left: 0;
top: 0;
& > img{
display: inline-block;
vertical-align: middle;
}
}
}
} </style>

后来出现一个问题,有一类的单据的图片存储在数据库中,之前的图片都是存储在服务器中,只需要传入单据号查询返回给我图片路径即可。

而存储在数据库当中不一样,需要拼接路径,一下是解决方法:

preview(){

		if(this.imgList.length > 0){
this.imgList.map(item=>{
item.furl = process.env.APP_EXCEL_PATH+'portal/gys/querydownloadPurchaFile?fileid='+ item.FILEID +'&gysdh='+item.CREATENAME //接口加入参
})
}
this.num = this.imgList.length
this.furl = this.imgList[0].furl
this.changeColor = 0
},

一般情况下,图片的预览,图片存储在服务器中,数据库中一般只存储路径。

我们后端我也是醉了,尽给我找事情,说起来都是泪。

vue项目中编写一个图片预览的公用组件的更多相关文章

  1. 在 vue 中使用 vieiwer 图片预览插件

    https://blog.csdn.net/WestLonly/article/details/79801800?utm_source=blogxgwz0 首先,感谢原作者 官网链接 github地址 ...

  2. angular中封装fancyBox(图片预览)

    首先在官网下载最新版的fancyBox(一定要去最新网站,以前依赖的jquery版本偏低),附上链接:http://fancyapps.com/fancybox/3/ 然后在项目中引用jquery,然 ...

  3. 在vue项目中添加一个html页面,开启本地服务器

    在vue项目里新增一个不需要登录的页面,那么我只能新增一个html页面了,不经过路由,直接在浏览器输入路径打开,那么就需要用到本地服务器, 1.vue里面的html页面最好放过在public文件夹里面 ...

  4. vue项目中设置全局引入scss,使每个组件都可以使用变量

    在Vue项目中使用scss,如果写了一套完整的有变量的scss文件.那么就需要全局引入,这样在每个组件中使用. 可以在mian.js全局引入,下面是使用方法. 1: 安装node-sass.sass- ...

  5. vue 项目文件流数据格式转blob图片预览展示

    为了图片安全性,有时候上传图片后后台不会直接返回图片地址,会返回文件流的数据格式,这种格式需要处理下才能展示在页面上   // 使用axios请求上传接口 axios({ method: 'get', ...

  6. vue项目中图片预览旋转功能

    最近项目中需要在图片预览时,可以旋转图片预览,在网上找了下,发现有一款功能强大的图片组件:viewerjs. git-hup: https://github.com/fengyuanchen/view ...

  7. Vue PC端图片预览插件

    *手上的项目刚刚搞完了,记录一下项目中遇到的问题,留做笔记: 需求: 在项目中,需要展示用户上传的一些图片,我从后台接口拿到图片url后放在页面上展示,因为被图片我设置了宽度限制(150px),所以图 ...

  8. 实现QQ空间图片预览效果

    今天项目遇到需求 要求 实现图片预览效果 .  类似于扣扣空间那种,本人也到网上找过 代码量太大了   ,类多到处是注释看的有点恶心 .然后自己写了一个图片预览的效果,其实很简单的 . 首先我们来分析 ...

  9. js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

随机推荐

  1. java 连接SQL Server

    1.确认服务器的连通性,并且使用账户密码模式登陆有效. 1).登陆服务器 2).查看安全性 2.新建数据库用于测试 3.下载jdbc安装并配置 进入微软官网主页--> 搜索JDBC-->找 ...

  2. 关于Hibernate和Strtus2的xml提示问题

    话不多说,上图 1.Windom 2.preferences 3.搜索框搜索xml catalog 点击Add 4.导入约束(具体操作图上1.2.3)

  3. sed的替换元字符的语法

    \(和\)用于保存正则表达式的一部分,而\1和\2用于保存回调的部分. 例如: 将sample.txt,内容如下 1...........55...........1010..........1010 ...

  4. (转)flutter 新状态管理方案 Provide (一)-使用

    flutter 新状态管理方案 Provide (一)-使用     版权声明:本文为博主原创文章,基于CC4.0协议,首发于https://kikt.top ,同步发于csdn,转载必须注明出处! ...

  5. JS里的<!-- //--> 注释有什么作用

    早期浏览器有很多种(目前很少了),对HTML的解释也不同.有种纯文本浏览器,只“翻译”文本内容,并只支持少量HTML标签.对交互式的代码视同纯文本.因此,我们称其为不支持javascript的浏览器( ...

  6. VB6 Collection实现百万文本去重

    上一篇数组的去重说到,对于千次计算以上的去重基本上特别的吃力,这里就介绍一种方法,通过Collection集合对象来过滤重复. Option Explicit '//By: InkHin '// 参考 ...

  7. jquery中的 deferred之 when (三)

    先来看使用案例: var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); var def4 = $.Def ...

  8. java二分法搜索

    二分法就是要将数据每次都分成两份然后再去找到你想要的数据 在二分法查找时要求传入的数据必须已经有序,假设现在为升序,然后每次将所寻找的值与中间值(数组左边界+(右边界-左边界)/2)作比较,大了则去寻 ...

  9. flex-grow,flex-shrink,flex-basis及flex

    flex-grow:默认值0:分配剩余空间的扩张比例: flex-basis:默认值auto:倘若设置了此属性,那么计算剩余空间之前要优先减去此属性,且它的层级比width高,会将width覆盖. 有 ...

  10. Servlet的三大作用域

    Servlet的三大作用域 一.request  请求对象 共享的数据:请求共享 特点:同一次请求中,共享数据可以获取(请求一旦结束,请求共享清除站)(请求转发能共享参数,重定向不行) 代码:req. ...