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. Vue源码学习之数据初始化

    首发地址:CJWbiu's Blog 在这里思考一个问题,使用Vue的时候需要在创建Vue实例时传入一个option,这里包含了我们定义的props.methods.data等.而在methods的方 ...

  2. OpenStack创建实例错误解决方法

    实例执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Build of instance beaeb5e0-26eb-4044-ae14-bb87d509886d aborted: Fa ...

  3. Angular8稳定版修改概述

    在今天早些时候Angular团队发布了8.0.0稳定版.其实早在NgConf 2019大会上,演讲者就已经提及了从工具到差分加载的许多内容以及更多令人敬畏的功能.下面是我对8.0.0一些新功能的简单介 ...

  4. Spring Cloud Ribbon负载均衡

    目录 一.简介 二.客户端负载均衡 三.RestTemplate详解 GET请求 POST请求 PUT请求 DELETE请求 一.简介 ​ Spring Cloud Ribbon是一个基于HTTP 和 ...

  5. android 开发-Toast控件的实现

    Toast吐司: Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司.内容简单可见代码,详见API.A toast provides simple feedback about ...

  6. Kettle-Data Integration 简介

    Pentaho系列产品介绍   Pentaho公司下面有一堆关于数据处理(数据整合.数据挖掘.报表等)的开源项目即社区版,入口网站: http://community.pentaho.com/ 数据整 ...

  7. SpringBoot | 第四章:日志配置(转)

    前言 介于平时工作中,对于日志这块没有过多的接触,也就未有过多的了解.故在编写本文时,上官网查看了相关资料,奈何每个字母我都认识,但合起来就有点晕了,英文阅读水平还是有待大大的提高呀.最后觉得还是转载 ...

  8. WPF Virtualization

    WPF虚拟化技术分为UI 虚拟化和数据虚拟化 第一种方法被称为"UI 虚拟化".支持虚拟化用户界面的控件是足够聪明来创建只显示的是实际在屏幕上可见的数据项目所需的 UI 元素.例如 ...

  9. 安装flask-mysqldb的时候,提示 mysql_config not found 的解决方法

    解决办法: sudo apt-get install libmysqlclient-dev sudo updatedb locate mysql_config 然后进入mysql_config的路径( ...

  10. JQuery基础知识==认识JQuery

    jQuery API 中文文档:https://www.jquery123.com/ jQuery Mobile 菜鸟教程:http://www.runoob.com/jquerymobile/jqu ...