js手写图片查看器(图片的缩放、旋转、拖拽)
一、关于图片查看器。
目前网络上能找到的图片查看器很多,谁便一搜就能出来。如:jquery.iviewer.js、Viewer.js这两个js文件,其中功能也足够满足大部分开发需求。但是单纯的就想实现图片的缩放、旋转、回位、拖拽。这些插件就有些多余,而且里面代码还没看。所以这里向大家介绍一种图片查看器的实现方法!
二、简单的Demo构造
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style> </style>
<script type="text/javascript"> function load() {
init();
} // 缩放图片
function imgToSize(oBool) {
var pic = document.getElementById("pic");
var text=document.getElementById("smw");
pic.style.zoom = parseInt(pic.style.zoom) + (oBool ? 2 : -2) + '%';
text.defaultValue=pic.style.zoom;
}
//还原尺寸
function restore() {
var pic = document.getElementById("pic");
var text=document.getElementById("smw");
pic.style.zoom = '100%';
pic.style.left = "0px";
pic.style.top = "0px";
text.defaultValue=pic.style.zoom;
}
// 旋转图片
var current = 0;
function imgRoll(direction) {
var pic = document.getElementById("pic");
if (direction == "left") {
current = (current - 90) % 360;
}
else if (direction == "right") {
current = (current + 90) % 360;
}
pic.style.transform = 'rotate(' + current + 'deg)';
}
//图片拖拽
drag = 0;
move = 0;
function mousedown() {
if (drag) {
X1 = window.event.x - parseInt(pic.style.left);
Y1 = window.event.y - parseInt(pic.style.top);
move = 1;
}
}
function onmouseover() {
drag = 1;
}
function mouseStop() {
window.event.returnValue = false;
}
function mousemove() {
if (move) {
pic.style.left = (window.event.x - X1) + "px";
pic.style.top = (window.event.y - Y1) + "px";
}
}
function mouseup() {
move = 0;
}
function init() {
var pic = document.getElementById("pic");
pic.style.zoom="100%";
pic.style.left="0px";
pic.style.top="0px";
pic.style.position="relative";
pic.style.cursor="move";
document.all.pic.onmouseover = onmouseover;
document.all.pic.onmousemove = mousemove;
document.all.pic.onmousedown = mousedown;
document.all.pic.onmouseup = mouseup;
document.all.pic.ondragstart = mouseStop;
}
</script>
</head>
<body onload="load()">
<div>
<img id="pic" src="data:image/Hydrangeas.jpg" />
</div> <br/>
<div class="btn">
<button class="btnleft" onclick="imgRoll('left');">左旋转90度</button>
<button class="btnright" onclick="imgRoll('right');">右旋转90度</button>
<button class="btnup" onclick="imgToSize(1);">放大</button>
<button class="btndown" onclick="imgToSize(0);">缩小</button>
<button class="btnreturn" onclick="restore();">还原</button>
<input type="text" value="100%" id="smw"/>
</div>
</body></html>
三、代码功能实现和说明
其实图片查看器,其中的原理就是实现图片的Zoom、Tranfrom、坐标的控制
1、缩放功能
// 缩放图片
function imgToSize(oBool) {
var pic = document.getElementById("pic");
var text=document.getElementById("smw");
pic.style.zoom = parseInt(pic.style.zoom) + (oBool ? 2 : -2) + '%';
text.defaultValue=pic.style.zoom;
}
这方法就是,每次点击一次控制图片的Zoom增减。其中oBool是用来区别是放大和缩小(就是ZooM的缩减)
2、图片的旋转
// 旋转图片
var current = 0;
function imgRoll(direction) {
var pic = document.getElementById("pic");
if (direction == "left") {
current = (current - 90) % 360;
}
else if (direction == "right") {
current = (current + 90) % 360;
}
pic.style.transform = 'rotate(' + current + 'deg)';
}
这方法就是,每点击一次图片按顺时针或逆时针旋转,direction是来区分顺(right)逆(left)旋转的方向。
3、图片的还原
function restore() {
var pic = document.getElementById("pic");
var text=document.getElementById("smw");
pic.style.zoom = '100%';
pic.style.left = "0px";
pic.style.top = "0px";
text.defaultValue=pic.style.zoom;
}
这方法就是,将图片从新初始化
4、图片的拖拽
//图片拖拽
drag = 0;
move = 0;
function mousedown() {
if (drag) {
X1 = window.event.x - parseInt(pic.style.left);
Y1 = window.event.y - parseInt(pic.style.top);
move = 1;
}
}
function onmouseover() {
drag = 1;
}
function mouseStop() {
window.event.returnValue = false;
}
function mousemove() {
if (move) {
pic.style.left = (window.event.x - X1) + "px";
pic.style.top = (window.event.y - Y1) + "px";
}
}
function mouseup() {
move = 0;
}
这个方法就是,实现这个首先对鼠标几个事件的了解mousedwon()鼠标的按下事件、onmouseover()鼠标在对应标签上方事件、mouseStop()鼠标停止运动事件、mousemove()鼠标移动事件、mouseup()松开鼠标键的事件。然后再看代码定义2个标准变量drag、move
5、初始化,也就是得注意事项
function init() {
var pic = document.getElementById("pic");
pic.style.zoom="100%";
pic.style.left="0px";
pic.style.top="0px";
pic.style.position="relative";
pic.style.cursor="move";
document.all.pic.onmouseover = onmouseover;
document.all.pic.onmousemove = mousemove;
document.all.pic.onmousedown = mousedown;
document.all.pic.onmouseup = mouseup;
document.all.pic.ondragstart = mouseStop;
}
初始化市将控制按钮绑定对应的事件,然后再将图片的样式属性,其中top、left得赋值,如果不赋值的化,默认值是无法获取然后不能参加后续的运算,就无法实现对应的效果!
js手写图片查看器(图片的缩放、旋转、拖拽)的更多相关文章
- flutter实现可缩放可拖拽双击放大的图片功能
flutter实现可缩放可拖拽双击放大的图片功能 可缩放可拖拽的功能,可实现图片或者其他widget的缩放已经拖拽并支持双击放大的功能 我们知道官方提供了双击缩放,但是不支持拖拽的功能,我们要实现向百 ...
- js实现图片查看器(图片的缩放、旋转、拖拽)
一.关于图片查看器. 目前网络上能找到的图片查看器很多,谁便一搜就能出来.如:jquery.iviewer.js.Viewer.js这两个js文件,其中功能也足够满足大部分开发需求.但是单纯的就想实现 ...
- 强大的jQuery图片查看器插件Viewer.js
简介 Viewer.js 是一款强大的图片查看器 Viewer.js 有以下特点: 支持移动设备触摸事件 支持响应式 支持放大/缩小 支持旋转(类似微博的图片旋转) 支持水平/垂直翻转 支持图片移动 ...
- Viewer.js – 强大的JS/jQuery图片查看器
简介 Viewer.js 是一款强大的图片查看器,像门户网站一般都会有各自的图片查看器,如果您正需要一款强大的图片查看器,也许 Viewer.js 是一个很好的选择.Viewer.js 有以下特点: ...
- 用JQ仿造礼德财富网的图片查看器
现在就职于一家P2P平台,自然也会关注同行其它网站的前端技术,今天要仿造的是礼德内页的一个图片查看器效果.不过说白了,无论人人贷也好礼德财富也好,很多地方的前端都做的不尽如人意,比如忽略细节.缺乏交互 ...
- angularjs1 自定义图片查看器(可旋转、放大、缩小、拖拽)
笔记: angularjs1 制作自定义图片查看器(可旋转.放大.缩小.拖拽) 2018-01-12 更新 可以在我的博客 查看我 已经封装好的 纯 js写的图片查看器插件 博客链接 懒得把 ...
- 发布两款JQ小插件(图片查看器 + 分类选择器),开源
图片查看器,github地址:https://github.com/VaJoy/imgViewer 效果如下: 这款当初大概写了2小时,有点匆忙地赶出来的,使用的接口很简单: $.bindViewer ...
- wpf图片查看器,支持鼠标滚动缩放拖拽
最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...
- 基于jQuery的一个简单的图片查看器
项目中自己diy了一个图片查看器.因为初始代码不是自己的,只是在上面改了一下也没有弄的很漂亮.等以后有时间了在重写一下样式和封装,作为备用的只是积累吧.如果有童鞋有用到,完全可以在此基础上改,比较容易 ...
随机推荐
- Flex移动皮肤开发(二)
范例文件 mobile-skinning-part2.zip 在这个讨论创建 Flex 移动 skin 的系列的 第 1 部分 中,我讨论了 Flex 团队在 Mobile 主题中所做的性能优化的原理 ...
- POJ1200(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27536 Accepted: 7692 Des ...
- page cache 与free
我们经常用free查看服务器的内存使用情况,而free中的输出却有些让人困惑,如下: 先看看各个数字的意义以及如何计算得到: free命令输出的第二行(Mem):这行分别显示了物理内存的总量(tota ...
- Ubuntu安装搜狗拼音
p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120% } p.western { font ...
- HTML css 格式布局
CSS(cascading style sheets,层叠样式表),作用是美化HTML网页. /*注释*/ 注释语法 2.1 样式表的基本概念 2.1.1样式表的分类 1.内联样式表 和HTML联 ...
- (@WhiteTaken)设计模式学习——工厂方法模式
这个工厂方法模式,是简单工厂的延伸,不同点在于,将某个具体的类继续细分,将核心部分抽象成一个接口.而简单工厂,把核心写在了一个类上,不利于拓展. 举个例子,简单工厂中有苹果类,香蕉类,我们创建了一个F ...
- maven 配置安装
1.下载maven http://maven.apache.org/ 2.windows安装maven 解压包后配置环境变量 PATH:%M2_HOME%\bin M2_HOME:D:\soft\ ...
- 前端必备技能之Photosh切图
切图:即从设计稿里面切出网页素材 一.使用Photoshop工具 工具的使用: 1.将文字与标尺的单位的设置为像素 2.打开这五个窗口,关闭其它窗口,保存工作区方便以后使用 3.工作区弄乱时,可以使用 ...
- 【Zookeeper】源码分析之请求处理链(一)
一.前言 前面已经分析了Watcher机制的主要代码,现在接着分析Zookeeper中的请求处理链,其是Zookeeper的主要特点之一. 二.总体框图 对于请求处理链而言,所有请求处理器的父接口为R ...
- 给ubuntu安装VNC远程桌面
(只有背景,没有菜单栏问题没有解决)Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作.服务 ...