Vue.js的图片预览的插件还是不少,但是找了半天还是没找到跟现在项目里能用得很顺手的,其实项目里图片预览功能很简单,点击放大,能双指缩放就可以了。部分vue.js的图片预览库都需要把图片资源单独拿出来进行预加载,这个项目比较特殊,图片属于数据内容的一部分,数据内容都是从服务器异步获取的HTML内容,异步加载完成html内容完成后,才能去给每个图片加上预览功能。

所以就只能自己根据需要添加图片预览功能了。

当然不是从零开始,可以借助一些移动端的触控库帮我们解决一些基础的触控事件,这里选用的是harmmer.js,选用这个库原因很简单,文档相对来说比较全面。

window.iv = {
init: function () {
var ivRoot = document.getElementById('iv-root') if (ivRoot) {
} else {
ivRoot = document.createElement('div')
ivRoot.style.zIndex =
ivRoot.style.backgroundColor = 'rgba(0, 0, 0, 1)'
ivRoot.style.position = 'fixed'
ivRoot.style.top = ''
ivRoot.style.left = ''
ivRoot.style.right = ''
ivRoot.style.bottom = ''
ivRoot.style.display = 'none'
ivRoot.id = 'iv-root' let ivImg = document.createElement('img')
ivImg.id = 'iv-image'
ivImg.style.position = 'absolute'
ivRoot.appendChild(ivImg) document.body.appendChild(ivRoot) ivRoot.onclick = function () {
this.style.display = 'none'
}
}
},
bind: function (node) {
var _this = this
var ivImg = document.getElementById('iv-image')
var ivRoot = document.getElementById('iv-root') if (typeof (node) !== 'object') return node.onclick = function () {
var nodeWidth = this.width
var nodeHeight = this.height
var position = _this.getPosition(nodeWidth, nodeHeight)
var src = this.getAttribute('src')
ivImg.style.width = position.width + 'px'
ivImg.style.height = position.height + 'px'
ivImg.style.top = position.top + 'px'
ivImg.style.left = position.left + 'px'
ivImg.src = src
ivRoot.style.display = '' _this.bindSlide()
_this.bindEnlarge()
}
},
bindSlide: function () {
var imageViewNode = document.getElementById('iv-image')
var hammertime = new Hammer(imageViewNode)
var _this = this // 滑动坐标
var startPosition = {}
var endPosition = {}
let currentPosition = {} hammertime.on('panstart', function (ev) {
startPosition = ev.center
currentPosition = {
left: _this.getStyleValue(imageViewNode, 'left'),
top: _this.getStyleValue(imageViewNode, 'top')
}
}) hammertime.on('panmove', function (ev) {
let offset = { x: , y: }
let newPositon = {} endPosition = ev.center
offset.x = endPosition.x - startPosition.x
offset.y = endPosition.y - startPosition.y
newPositon.left = currentPosition.left + offset.x
newPositon.top = currentPosition.top + offset.y
// vueThis.debugMsg = imageViewNode.style.left; imageViewNode.style.left = newPositon.left + 'px'
imageViewNode.style.top = newPositon.top + 'px'
})
},
bindEnlarge: function () {
var imageViewNode = document.getElementById('iv-image')
var hammertime = new Hammer(imageViewNode)
var _this = this // 为该dom元素指定触屏移动事件
hammertime.add(new Hammer.Pinch())
var currentScale =
hammertime.on('pinchstart', function (e) {
// vueThis.debugMsg = JSON.stringify(e); currentScale = _this.getStyleValue(
imageViewNode,
'transform'
) if (currentScale < ) {
currentScale =
}
}) // 添加放大事件
hammertime.on('pinchout', function (e) {
var newScale = currentScale * e.scale
imageViewNode.style.transform = `scale(${newScale})`
}) // 添加缩小事件
hammertime.on('pinchin', function (e) {
var newScale = currentScale * e.scale
if (newScale < ) {
newScale =
}
imageViewNode.style.transform = `scale(${newScale})`
})
},
getPosition: function (width, height) {
let screenHeight = screen.height // 可视区域高
let screenWidth = screen.width // 可是区域宽 let displayHeight = height
let displayWidth = width
let displayLeft =
let displayTop =
// 适应宽度,调整高度
displayWidth = screenWidth
displayHeight = screenWidth * (height / width)
displayTop = (screenHeight - displayHeight) /
// 超过屏幕高度
if (displayHeight > screenHeight) {
// console.log("适应宽度时,超过可视高度!")
displayHeight = screenHeight
displayWidth = screenHeight * (width / height)
displayTop =
displayLeft = (screenWidth - displayWidth) /
} console.log(`计算之后的宽高:width${displayWidth}, height:${displayHeight}`)
console.log(`计算后的位置, left:${displayLeft}, top:${displayTop}`) return {
top: displayTop,
left: displayLeft,
width: displayWidth,
height: displayHeight
}
},
getStyleValue: function (node, name) {
let value = node.style[name] if (value) {
if (value.indexOf('px') >= ) {
value = value.replace('px', '')
} else if (value.indexOf('scale') >= ) {
value = value.replace('scale', '')
value = value.replace('(', '')
value = value.replace(')', '')
}
value = parseFloat(value) return value
} return
}
} window.iv.init()

这里没有使用其他库,范例暂时只列举vue的环境

<template>
<div class="imgs">
<img src="../assets/images/001.jpg" class="testImage" />
<img src="../assets/images/002.jpg" class="testImage" />
</div>
</template>
<script>
require("../assets/js/hammer.min.js");
require("../assets/js/image-view"); export default {
name: "Index",
data() {
return {};
},
mounted() {
let nodes = document.getElementsByClassName("testImage"); for (var index in nodes) {
let currentNode = nodes[index]; iv.bind(currentNode);
}
}
};
</script> <style scoped>
.imgs {
width: %;
} img {
width: %;
}
</style>

其他非vue的环境也可以直接引用harmmer.js后直接使用,功能比较简单。

下载源代码,http://download.csdn.net/download/speedupnow/10169409

vue.js 图片预览的更多相关文章

  1. Vue.js图片预览插件

    vue-picture-preview-extend vue-picture-preview的扩展版本,本文中插件是由其他大神开发,我做了一些扩展,原文链接:https://segmentfault. ...

  2. 适用于移动端、PC 端 Vue.js 图片预览插件

    1.安装:npm install --save vue-picture-preview 2.使用: (1)入口文件中main.js中全局引入: import Vue from 'vue' import ...

  3. 【VUE】图片预览放大缩小插件

    From: https://www.jianshu.com/p/e3350aa1b0d0 在看项目时,突然看到预览图片的弹窗,感觉好僵硬,不能放大,不能切换,于是便在网上找下关于图片预览的插件,有找到 ...

  4. js图片预览插件,不涉及上传

    小小的几十行代码,很牛逼,很实用. 支持多个图片的预览,只要new多个对象就行了. html如下 <!-- zhouxiang www.zhou-xiang.com --> <!DO ...

  5. previewImage.js图片预览缩放保存插件

    previewImage.js好用的图片预览缩放保存插件

  6. js图片预览带进度条

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 实现一个vue的图片预览插件

    vue-image-swipe 基于photoswipe实现的vue图片预览组件 安装 1 第一步 npm install vue-image-swipe -D 2 第二步 vue 入口文件引入 im ...

  8. js 图片预览

    图片预览 $('#pac_recipe').change(function() { var imgsrc = ''; ]) { //chrome firefox imgsrc = window.URL ...

  9. js图片预览(一张图片预览)

    核心思想:无论是一张图片上传还是多图上传,首先我们都需要先获得图片对象. 栗子: <inuput type="file" id="myfile" onch ...

随机推荐

  1. JAVA内存模型(Java Memory Model ,JMM)

    http://blog.csdn.net/hxpjava1/article/details/55189077 JVM有主内存(Main Memory)和工作内存(Working Memory),主内存 ...

  2. JAVA多线程之Semaphore

    Semaphore:动态增减信号量,用于控制对某资源访问的同一时间的并发量.类似于令牌,谁拿到令牌(acquire)就可以去执行了,如果没有令牌则需要等待. [如何获取]:semaphore.tryA ...

  3. arch安装问题总结

    安装 archLinux 的时候遇到的一些问题,记录下来方便以后安装. 1.fcitx 在设置/etc/locale.conf文件时,中文不能写成zh_CN.utf-8,而是要写成zh_CN.utf8 ...

  4. 解决gradle下载慢的问题

    解决方法要做两部 一 打开用户主目录 linux平台/home/用户名/.gradle windows平台c:\Users\用户名\.gradle macos平台/Users/用户名/.gradle ...

  5. kickstart2019 round_C B. Circuit Board

    思路: 这题应该不止一种解法,其中的一种可以看作是leetcode85https://www.cnblogs.com/wangyiming/p/11059176.html的加强版: 首先对于每一行,分 ...

  6. oracle 查询之前的表数据

    SELECT * FROM Student  AS OF TIMESTAMP SYSDATE - 3/1440 对SQL的解释说明: SYSDATE :当前时间 1440 :24h*60m=1440m ...

  7. mui的ajax例子3

    mui.get() 前端页面: <!DOCTYPE html><html><head> <meta charset="utf-8"> ...

  8. Eucalyptus-利用镜像启动一个Centos实例

    1.前言 使用kvm制作Eucalyptus镜像(Centos6.5为例)——http://www.cnblogs.com/gis-luq/p/3990795.html 上一篇我们讲述了如何利用kvm ...

  9. 观察者模式(Observe Pattern)

    观察者模式: 当对象存在一对多关系时,使用观察者模式(Observe Pattern).例如:当一个对象被修改时,会通知它的依赖对象. 介绍: 1.意图:定义对象的一种一对多的依赖关系,当一个对象的状 ...

  10. No module named 'revoscalepy'问题解决

    SqlServer2017开始支持Python,前段时间体验了下,按照微软的入门例子操作的:https://microsoft.github.io/sql-ml-tutorials/python/re ...