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. 【tf.keras】tf.keras加载AlexNet预训练模型

    目录 从 PyTorch 中导出模型参数 第 0 步:配置环境 第 1 步:安装 MMdnn 第 2 步:得到 PyTorch 保存完整结构和参数的模型(pth 文件) 第 3 步:导出 PyTorc ...

  2. js 独立命名空间,私有成员和静态成员

    独立的命名空间   1可以避免全局变量污染. 全局变量污染不是 说 被全局变量污染,而是说不会污染全局变量.   2实现私有成员. 在js中函数 就可以满足独立的命名空间的两点需求.   如:     ...

  3. JS文本框输入限制

    1上面的文本框只能输入数字代码(小数点也不能输入): CODE: <input onkeyup="this.value=this.value.replace(/\D/g,'')&quo ...

  4. blueterm蓝牙超级终端(源码)

    今天FQ访问外文网站砍看见的终端程序,貌似现在已经开源了,这个软件源码方便开发者开发开发蓝牙与相关的设备的通信,所以在此分享下,给需要的人 项目地址:点击打开,感谢作者,我已经实验过了,导入到ecli ...

  5. vue resource patch方法的传递数据 form data 为 [object Object]

    今天在测试 iblog 登录时,传送过去的数据总是 [object Object],以至于后台识别不出来. vue 使用了 vueResource 组件,登录方法为 patch. 经过探索,终于在官网 ...

  6. jQuery学习笔记(三)

    jQuery中的事件 页面加载 原生DOM中的事件具有页面加载的内容onload事件,在jQuery中同样提供了对应的内容ready()函数. ready与onload之间的区别: onload re ...

  7. android sqlite 递归删除一棵子树

    背景:android studio 3.0 GreenDao 目标:在android 中,如何做到递归删除某颗子树?? ======================================== ...

  8. Filter过滤器,xml配置与页面不乱码整理

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  9. Vue日历

    Vue生成日历,根据返回值将日期标红 HTML: <h1>CSS 日历</h1> <div id="calendar"> <div cla ...

  10. 卸载gitlab

    一.停止gitlab sudo gitlab-ctl stop 二.卸载gitlab sudo rpm -e gitlab-ce三.查看gitlab进程 杀掉第一个守护进程 kill -9 4473 ...