欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue实现图片按比例缩放问题操作

程序员文章站 2024-01-02 10:34:10
如下所示:getimg(src){ var img_url =src var img = new image() img.src=img_url this.pictureheight.height =...

如下所示:

getimg(src){
 var img_url =src
 var img = new image()
 img.src=img_url
 this.pictureheight.height = math.ceil(img.height/img.width * 460)+'px'
},
//首先通过这个方法算出图片的高度/宽度比,460是我设置的宽度,计算得出需要的高度,然后修改容器的高
//度,图片通过height:100%;width:100%撑开,这样图片就不会失真了

vue里面还有一个问题,如果容器只是div的话,修改容器高度,非常简单,如果容器是一个element的插件的话,一般容器的样式都可以通过:style="stylemodel"来绑定一个data中的属性stylemodel:{height:100px;}这样的方式来修改,

当然如果遇到一些比较复杂的样式调整,也可以通过$refs来修改样式,,但是这样又会出现一个问题,就是$refs定位到的ref属性必须要组件完全加载完成后才能显示出来,所以一般会用this.$nexttick(function(){})的包装起来。

这个方法包装起来后的好处是,会在dom更新完成后执行这里面的方法,这样就不用担心$refs获取不到的问题了。

  this.$nexttick(function(){
  // this.$refs.test.$el.childnodes[0].style.height=this.pictureheight.height
   document.getelementsbyclassname('el-carousel__container')[0].style.height=this.pictureheight.height
  })
 
//现在就是通过这两种比较通用的js方式来操作属性了

补充知识:vue实现图片放大的方法

一、v-viewer插件

首先,用命令行安装v-viewer插件:

npm install v-viewer --save

然后,在main.js中注册v-viewer插件,代码如下:

// 实现图片点击放大
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" }
});

注册完成后,就可以在组件中使用v-viewer插件了:

<template>
  <!-- imgarr是图片地址的数组,例: ['1.png','2.png'] -->
 <viewer :images="imgarr">
 <img v-for="src in imgarr" :src="src" :key="src" width="200">
 </viewer>
</template>

二、vue-directive-image-previewer插件

用命令行安装vue-directive-image-previewer插件:

npm install vue-directive-image-previewer -d

在main.js中注册:

import vuedirectiveimagepreviewer from 'vue-directive-image-previewer'
import 'vue-directive-image-previewer/dist/assets/style.css'
vue.use(vuedirectiveimagepreviewer)

在组件中使用vue-directive-image-previewer插件:

<template>
 <div>
  <img v-image-preview src="123.png"/>
 </div>
</template>

以上这篇vue实现图片按比例缩放问题操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: