vue实现简易图片左右旋转,上一张,下一张组件案例


Posted in Javascript onJuly 31, 2020

项目需求,嵌到elementUi里的小组件,写得不好,不喜勿喷,谢谢

父组件代码

<template>
 <div>
 <see-attachment :filesLists='files' :file='imgFile' v-if="showmask" @hideMask='showmask=false'></see-attachment>
 </div>
</template>
<script>
 import seeAttachment from "./seeAttachment.vue";
 export default {
 data() {
 return {
 showmask: false,
 imgFile: {}
 };
 },
 components: {
 seeAttachment
 },
 methods: {
 lookImg(f) {
 this.showmask = true;
 this.imgFile = f;
 },
 }
 };
</script>

子组件代码

<template>
 <div>
 <div class="proview_box" @click="hideMask">
 <div class="img_box">
 <img :src="imgPath" :width="width" :height="height" @click="stopEvent" id='img' />
 </div>
 </div>
 <div class="handleImg_box">
 <div @click="prevOne"><img src="../../../../static/img/prev.png" /></div>
 <div @click="rotate(0)"><img src="../../../../static/img/turn_left.png" /></div>
 <div @click="rotate(1)"><img src="../../../../static/img/turn_right.png" /></div>
 <div @click="nextOne"><img src="../../../../static/img/next.png" /></div>
 </div>
 </div>
</template>
<script>
 export default {
 name: 'seeAttachment',
 props: ['filesLists', 'file'],
 data: function() {
 return {
 imgPath: '',
 width: 0,
 height: 0,
 imgIndex: 0
 }
 },
 mounted() {
 this.getImgIndex();
 this.seeAttachment(this.imgIndex);
 },
 computed: {
 //去掉不是图片的附件
 imgList() {
 const ARR = ["png", "PNG", "jpeg", "JPEG", "bmp", "BMP", "jpg", "JPG", "gif", "GIF"];
 let arrs = '';
 let suffix = '';
 let type = '';
 let newList = [];
 this.filesLists.forEach((item) => {
  arrs = item.name.split('.');
  suffix = arrs[arrs.length - 1];
  type = item.type ? item.type : item.raw ? item.raw.type : suffix;
  ARR.some(items => {
  if (type.includes(items)) {
  newList.push(item)
  }
  })
 })
 return newList;
 }
 },
 methods: {
 //通过父组件传入的file获取当前查看的图片index
 getImgIndex() {
 let that = this;
 that.imgList.forEach((item, index) => {
  if(that.file.id == item.id){
  that.imgIndex = index;
  }
 })
 },
 //?藏查看?D片
 hideMask() {
 this.$emit('hideMask', false);
 },
 stopEvent(event) {
 //阻止冒泡事件
 //取消事件冒泡
 let e = event; //若省略此句,下面的e改为event,IE运行可以,但是其他浏览器就不兼容
 if (e && e.stopPropagation) {
  e.stopPropagation();
 } else if (window.event) {
  window.event.cancelBubble = true;
 }
 },
 //上一??
 prevOne() {
 if (this.imgIndex == 0) {
  return false;
 }
 let img = document.getElementById('img')
 img.style.transform = 'rotate(0deg)';
 this.imgIndex = this.imgIndex - 1;
 this.seeAttachment(this.imgIndex);
 },
 //下一??
 nextOne() {
 let listLength = this.imgList.length - 1;
 if (this.imgIndex >= listLength) {
  return false;
 }
 let img = document.getElementById('img')
 img.style.transform = 'rotate(0deg)';
 this.imgIndex = this.imgIndex + 1;
 this.seeAttachment(this.imgIndex);
 },
 //右转
 rotate(t) {
 let img = document.getElementById('img')
 var reg = /(rotate\([\-\+]?((\d+)(deg))\))/i;
 var wt = img.style['-webkit-transform'],
  wts = wt.match(reg);
 var $3 = RegExp.$3;
 var current = $3 ? parseInt($3) : 0;
 if (t == 0) {
  current = current == 0 ? 360 : current;
  current = (current - 90) % 360;
 } else {
  current = (current + 90) % 360;
 }
 img.style.transform = 'rotate(' + current + 'deg)';
 },
 seeAttachment(index = 0) {
 if (this.imgList.length == 0) {
  return false;
 }
 let that = this;
 let basePath = "http://" + (process.env.OSS_PATH.indexOf("test") == -1 ? "opyx-mtds-pro" :
  "opyx-mtds-test") + ".oss-cn-shenzhen.aliyuncs.com/";
 that.imgPath = basePath + that.imgList[index]['path'];
 let img_url = basePath + that.imgList[index]['path'];
 // 创建对象
 var img = new Image();
 // 改变图片的src
 img.src = img_url;
 // 定时执行获取宽高
 var check = function() {
  // 只要任何一方大于0 
  // 表示已经服务器已经返回宽高 
  if (img.width > 0 || img.height > 0) {
  let wdt = document.body.offsetWidth;
  let hgt = document.body.offsetHeight;
  let ratio = 1;
  if (img.width > img.height && img.width > wdt * ratio) {
  img.height = img.height * wdt * ratio / img.width;
  img.width = wdt * ratio;
  console.log('宽大于高', img)
  } else if (img.height > img.width && img.height > hgt * ratio) {
  img.width = img.width * hgt * ratio / img.height;
  if (img.width > wdt) {
  img.width = wdt;
  }
  img.height = hgt * ratio;
  console.log('高大于宽', img)
  } else {
  img.height = img.height * wdt * ratio / img.width
  img.width = wdt * ratio
  console.log('正常', img)
  }
  that.width = img.width;
  that.height = img.height;
  clearInterval(set);
  }
 };
 var set = setInterval(check, 40);
 },
 }
 }
</script>
<style lang="scss" scoped>
 .handleImg_box {
 position: fixed;
 bottom: 0;
 left: 50%;
 z-index: 100;
 margin-left: -150px;
 width: 300px;
 padding: 1rem 0;
 display: flex;
 align-items: center;
 justify-content: space-around;
 }
 
 .handleImg_box div {
 font-size: 0;
 }
 
 .handleImg_box div img {
 width: 3rem;
 }
</style>

补充知识:vue图片放大、缩小、旋转等。仅需要两行代码!!!

效果图

vue实现简易图片左右旋转,上一张,下一张组件案例

实现步骤:

1.下载Viewer组件

npm install v-viewer --save

2.在图片显示的页面引用 viewer组件(一个js文件,一个css样式文件)

import Viewer from "@/assets/js/viewer.min.js";

import '@/assets/css/viewer.min.css';

3.再需要点击图片的标签添加点击事件@click

<img :id ="item.publicFileURL" :src="item.publicFileURL" @click="aaa(item)" >

vue实现简易图片左右旋转,上一张,下一张组件案例

说明:因为我的图片是在集合中存的需要动态的点击放大(点哪个放大哪个)----id很重要 aaa()方法中要用

4.@click="aaa(item)"方法

aaa(item) {
   var viewer = new Viewer(document.getElementById(item.publicFileURL), {
    url: item.publicFileURL,
  });
 },

vue实现简易图片左右旋转,上一张,下一张组件案例

以上这篇vue实现简易图片左右旋转,上一张,下一张组件案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
自定义的一个简单时尚js下拉选择框
Nov 20 Javascript
javascript制作的网页侧边弹出框思路及实现代码
May 21 Javascript
js实现鼠标点击文本框自动选中内容的方法
Aug 20 Javascript
跟我学习javascript的作用域与作用域链
Nov 19 Javascript
探寻JavaScript中this指针指向
Apr 23 Javascript
vue构建单页面应用实战
Apr 10 Javascript
React应用中使用Bootstrap的方法
Aug 15 Javascript
vue2.0 实现导航守卫的具体用法(路由守卫)
May 17 Javascript
jQuery实现的中英文切换功能示例
Jan 11 jQuery
微信小程序结合Storage实现搜索历史效果
May 18 Javascript
extjs4图表绘制之折线图实现方法分析
Mar 06 Javascript
解决echarts echarts数据动态更新和dataZoom被重置问题
Jul 20 Javascript
vue实现给div绑定keyup的enter事件
Jul 31 #Javascript
简单了解JavaScript作用域
Jul 31 #Javascript
基于vue--key值的特殊用处详解
Jul 31 #Javascript
javascript开发实现贪吃蛇游戏
Jul 31 #Javascript
vue 解决无法对未定义的值,空值或基元值设置反应属性报错问题
Jul 31 #Javascript
vscode中Vue别名路径提示的实现
Jul 31 #Javascript
Vue记住滚动条和实现下拉加载的完美方法
Jul 31 #Javascript
You might like
php中文字符串截取方法实例总结
2014/09/30 PHP
PHP开发中AJAX技术的简单应用
2015/12/11 PHP
thinkphp5.1框架实现格式化mysql时间戳为日期的方式小结
2019/10/10 PHP
JS去除字符串的空格增强版(可以去除中间的空格)
2009/08/26 Javascript
javascript 类定义的4种方法
2009/09/12 Javascript
jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
2010/10/15 Javascript
在JavaScript中正确引用bind方法的应用
2015/05/11 Javascript
简介JavaScript中的sub()方法的使用
2015/06/08 Javascript
深入理解jquery自定义动画animate()
2016/05/24 Javascript
把普通对象转换成json格式的对象的简单实例
2016/07/04 Javascript
Javascript中内建函数reduce的应用详解
2016/10/20 Javascript
自学实现angularjs依赖注入
2016/12/20 Javascript
JS实现的按钮点击颜色切换功能示例
2017/10/19 Javascript
浅谈React的最大亮点之虚拟DOM
2018/05/29 Javascript
在Bootstrap开发框架中使用dataTable直接录入表格行数据的方法
2018/10/25 Javascript
[01:05:29]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Aster BO3 第二场 1月24日
2021/03/11 DOTA
Python实现的简单文件传输服务器和客户端
2015/04/08 Python
Python3使用requests包抓取并保存网页源码的方法
2016/03/15 Python
从零开始学Python第八周:详解网络编程基础(socket)
2016/12/14 Python
python解决pandas处理缺失值为空字符串的问题
2018/04/08 Python
浅述python2与python3的简单区别
2018/09/19 Python
使用python获取电脑的磁盘信息方法
2018/11/01 Python
python自定义函数实现最大值的输出方法
2019/07/09 Python
浅谈Python3识别判断图片主要颜色并和颜色库进行对比的方法
2019/10/25 Python
canvas实现烟花的示例代码
2020/01/16 HTML / CSS
Keds官方网站:购买帆布运动鞋和经典皮鞋
2016/11/12 全球购物
Crabtree & Evelyn欧盟:豪华洗浴、身体和护发
2021/03/09 全球购物
大学在校生求职信范文
2013/11/21 职场文书
养殖项目策划书范文
2014/01/13 职场文书
个人安全生产承诺书
2014/05/22 职场文书
机关职员工作检讨书
2014/10/23 职场文书
晋江市人民政府党组群众路线教育实践活动整改方案
2014/10/25 职场文书
先进员工事迹材料
2014/12/20 职场文书
2015年法院工作总结范文
2015/04/28 职场文书
小程序实现悬浮按钮的全过程记录
2021/10/16 HTML / CSS
python使用BeautifulSoup 解析HTML
2022/04/24 Python