• vue 滚动缩放反转图片功能


     核心代码,创建image对象,获取图片的大小,设置高宽比,监听滚轮控制图片比

    1. <style lang="less" scoped>
    2. .demo-spin-icon-load{
    3. animation: ani-demo-spin 1s linear infinite;
    4. }
    5. .img-max100{
    6. max-width: 337px;
    7. max-height: 217px;
    8. /* height: 160px;
    9. width: 165px; */
    10. cursor:pointer;
    11. }
    12. .flexcenter{
    13. display: -webkit-flex!important;
    14. display: flex!important;
    15. justify-content:center
    16. }
    17. </style>
    18. <template>
    19. <Modal v-model="showOriginImg" :width="width" :loading="loading">
    20. <div slot="footer">
    21. <ButtonGroup class="flexcenter">
    22. <Button @click="rotate(-90)" type="primary" size="small">
    23. <Icon :size="14" type="md-return-left"></Icon></Button>
    24. <Button @click="rotate(90)" type="primary" size="small">
    25. <Icon :size="14" type="md-return-right"></Icon>
    26. </Button>
    27. <Button @click="zoom(0.5)" type="primary" size="small">
    28. <Icon :size="14" type="md-add"></Icon>
    29. </Button>
    30. <Button @click="zoom(-0.5)" type="primary" size="small">
    31. <Icon :size="14" type="md-remove"></Icon>
    32. </Button>
    33. </ButtonGroup>
    34. </div>
    35. <!-- <div slot="header"></div> -->
    36. <div class="flexcenter">
    37. <img :id="imgId" :src="url">
    38. </div>
    39. </Modal>
    40. </template>
    41. <script>
    42. import imgerror from "@/images/main/imgerror.png"
    43. import util from "@/libs/util.js"
    44. import $ from 'jquery';
    45. export default {
    46. props:{
    47. src:String
    48. }, // 父组件传过来所需的url
    49. data() {
    50. return {
    51. newimgerror:imgerror,
    52. loading:true,
    53. showOriginImg:false,
    54. width:'280px',
    55. url: imgerror, // 先加载loading.gif
    56. imgId: 'image'+Math.ceil(Math.random()*1000000),
    57. imgProp:{
    58. imgwidth:240,
    59. imgheight:200,
    60. zoom:0,
    61. deg:0,
    62. },
    63. newurl:imgerror, // 先加载loading.gif,
    64. }
    65. },
    66. methods:{
    67. loadImg(){
    68. var _this = this;
    69. var newImg = new Image();
    70. if(_this.src){
    71. //存在多张图片时,pop显示第一张,点击打开弹窗显示第二张
    72. if(_this.src.indexOf(';')>=0){
    73. let newsrc = _this.src.split(';')
    74. newImg.src = newsrc[1];
    75. _this.newurl = newsrc[0];
    76. if(newsrc[1] && newsrc[1] != ''){
    77. newImg.onload = () => { // 图片加载成功后把地址给原来的img
    78. _this.loading=false;
    79. _this.url = newsrc[1];
    80. _this.imgProp.imgwidth = 500;
    81. _this.imgProp.imgheight = (500*newImg.height)/newImg.width;
    82. $('#'+_this.imgId).width(_this.imgProp.imgwidth);
    83. $('#'+_this.imgId).height(_this.imgProp.imgheight);
    84. _this.initWidth(_this.imgProp.imgwidth,_this);
    85. }
    86. newImg.onerror = () => { // 图片加载错误时的替换图片
    87. _this.loading = false;
    88. _this.newurl = imgerror;
    89. _this.url = imgerror;
    90. _this.imgProp.imgwidth = 240;
    91. _this.imgProp.imgheight = 200;
    92. _this.initWidth(240,_this);
    93. };
    94. }else{
    95. newImg.onload = () => { // 图片加载成功后把地址给原来的img
    96. _this.loading=false;
    97. _this.url = newsrc[0];
    98. _this.imgProp.imgwidth = 500;
    99. _this.imgProp.imgheight = (500*newImg.height)/newImg.width;
    100. $('#'+_this.imgId).width(_this.imgProp.imgwidth);
    101. $('#'+_this.imgId).height(_this.imgProp.imgheight);
    102. _this.initWidth(_this.imgProp.imgwidth,_this);
    103. }
    104. }
    105. }else{
    106. _this.newurl = _this.src;
    107. _this.url = _this.src;
    108. newImg.src = _this.src;
    109. newImg.onerror = () => { // 图片加载错误时的替换图片
    110. _this.loading = false;
    111. _this.newurl = imgerror;
    112. _this.url = imgerror;
    113. _this.imgProp.imgwidth = 240;
    114. _this.imgProp.imgheight = 200;
    115. $('#'+_this.imgId).width(_this.imgProp.imgwidth);
    116. $('#'+_this.imgId).height(_this.imgProp.imgheight);
    117. _this.initWidth(240,_this);
    118. };
    119. newImg.onload = () => { // 图片加载成功后把地址给原来的img
    120. _this.loading=false;
    121. _this.url = _this.src;
    122. _this.imgProp.imgwidth = 500;
    123. _this.imgProp.imgheight = (500*newImg.height)/newImg.width;
    124. $('#'+_this.imgId).width(_this.imgProp.imgwidth);
    125. $('#'+_this.imgId).height(_this.imgProp.imgheight);
    126. _this.initWidth(_this.imgProp.imgwidth,_this);
    127. }
    128. }
    129. }else{
    130. _this.newurl = imgerror;
    131. _this.url = imgerror;
    132. newImg.src = imgerror;
    133. newImg.onerror = () => { // 图片加载错误时的替换图片
    134. _this.loading = false;
    135. _this.imgProp.imgwidth = 240;
    136. _this.imgProp.imgheight = 200;
    137. $('#'+_this.imgId).width(_this.imgProp.imgwidth);
    138. $('#'+_this.imgId).height(_this.imgProp.imgheight);
    139. _this.initWidth(240,_this);
    140. };
    141. }
    142. },
    143. initWidth(widthN,self){
    144. if(widthN<=170){
    145. self.width = "200px";
    146. }else{
    147. self.width = (widthN+40)+"px";
    148. }
    149. },
    150. showImageFunc(){
    151. //点击打开弹窗
    152. // if(this.url === this.src)
    153. // {
    154. this.showOriginImg = true;
    155. // }
    156. },
    157. rotate(v){
    158. var deg = this.imgProp.deg =this.imgProp.deg+v;
    159. $('#'+this.imgId).css({
    160. '-webkit-transform': 'rotate(' + deg + 'deg)',
    161. '-moz-transform': 'rotate(' + deg + 'deg)',
    162. '-o-transform': 'rotate(' + deg + 'deg)',
    163. '-ms-transform': 'rotate(' + deg + 'deg)',
    164. 'transform': 'rotate(' + deg + 'deg)'
    165. });
    166. // $('#'+this.imgId).rotate(v);
    167. },
    168. //放大缩小
    169. zoom(v){
    170. var self = this;
    171. // 限制最大最小
    172. if (self.imgProp.zoom+v > 15 || self.imgProp.zoom+v<0.2){
    173. return
    174. }
    175. self.imgProp.zoom = self.imgProp.zoom+v;
    176. var width = self.imgProp.imgwidth+self.imgProp.imgwidth*self.imgProp.zoom;
    177. var height = self.imgProp.imgheight+self.imgProp.imgheight*self.imgProp.zoom;
    178. self.initWidth(width,self);
    179. $('#'+this.imgId).width(width);
    180. $('#'+this.imgId).height(height);
    181. },
    182. handleScroll:function(e){
    183. console.log(e)
    184. if (e.deltaY > 0){
    185. this.zoom(-0.2);
    186. } else {
    187. this.zoom(0.2);
    188. }
    189. }
    190. },
    191. watch:{
    192. src(data){
    193. this.url = imgerror;
    194. this.newurl = imgerror;
    195. this.loadImg();
    196. }
    197. },
    198. mounted() {
    199. let _this = this;
    200. _this.url = imgerror;
    201. _this.newurl = imgerror;
    202. _this.loadImg();
    203. _this.$nextTick(() =>{
    204. window.addEventListener('mousewheel',_this.handleScroll,false)
    205. })
    206. },
    207. destroyed() {
    208. let _this = this;
    209. window.removeEventListener('mousewheel',_this.handleScroll,false)
    210. }
    211. }
    212. </script>

  • 相关阅读:
    webpack 解决:Cannot use import statement outside a module 的问题
    要使用API接口获取淘宝电商平台的数据,您需要遵循以下步骤:
    Python自动操作 GUI 神器:PyAutoGUI
    sys文件系统
    云存储空间的动态分配技术
    基于Java的微小企业人事管理系统的设计与实现 毕业设计-附源码231012
    操作系统备考学习 day9 (3.1.1 ~ 3.1.11)
    抓包工具charles修改请求和返回数据
    Nginx(七) root和alias的区别及详细测试
    制作本地SCLo-scl镜像仓库(reposync下载rpm包、createrepo制作镜像仓库、httpd发布服务)
  • 原文地址:https://blog.csdn.net/sinat_24713805/article/details/128180687