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. 使用freemarker生成html、doc文件【原创】

    语言:java 功能:使用freemarker生成html.doc 1.生成html public void updateuseFreemarker2html(String abdkId) { // ...

  2. Docker:安装部署RabbitMQ

    前言 今天原本想讲解SpringBoot集成RabbitMQ的,临近开始写时才发现家里的电脑根本没有安装RabbitMQ呀.这下只好利用已有的阿里云服务器,直接Docker安装一下了,顺道记录下,算是 ...

  3. Aspose.cell生成表格

     public void ExportQueryPrj(HttpContext context)         {              //接受前端传递参数和数据             st ...

  4. Ecshop如何解决Deprecated: preg_replace()报错

    今天安装Ecshop后,运行出现各种问题,其中 Deprecated: preg_replace() 之类的报错最多,下面贴出解决方案: 错误原因: preg_replace() 函数中用到的修饰符 ...

  5. Android入门:Activity生命周期

    一.Activity生命周期介绍 我们在学Java Web时会学到Servlet的生命周期,因此对生命周期的概念已经有一定了解,简单地说就是某个事物从出生到死亡的过程. Activity也存在声明周期 ...

  6. 《大话设计模式》num01---简单工厂模式

    2017年12月10日 20:13:57 独行侠的守望 阅读数:128更多个人分类: 设计模式编辑版权声明:本文为博主原创文章,转载请注明文章链接. https://blog.csdn.net/xia ...

  7. 学习笔记:《JavaScript高级程序设计》

    第1章 JavaScript简介 1.一个完整的JavaScript实现应该由三部分组成:核心(ECMAScript),文档对象模型(DOM)和浏览器对象模型(BOM). 2.Web浏览器只是ECMA ...

  8. CF1152C Neko does Maths

    思路: 假设a <= b,lcm(a + k, b + k) = (a + k) * (b + k) / gcd(a + k, b + k) = (a + k) * (b + k) / gcd( ...

  9. NOIP2013Day1T3 表示只能过一个点

    •A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的 ...

  10. C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载

    编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...