js的图片裁切只支持移动和右下拉

html部分

<div id="box" class="box">
<img class="img1" src="1.jpg"/>
<div class="main" id="main">
<div id="left-up" class="minDiv left-up"></div>
<div id="left" class="minDiv left"></div>
<div id="left-down" class="minDiv left-down"></div>
<div id="up" class="minDiv top"></div>
<div id="right-up" class="minDiv right-up"></div>
<div id="right" class="minDiv right"></div>
<div id="right-down" class="minDiv right-down"></div>
<div id="down" class="minDiv bottom"></div>
</div>
</div>
<div id="preview">
<img id="img3" class="img3" src="1.jpg" style="top: 0px; left: 0px; clip: rect(0px 202px 202px 0px);" />
</div>

css部分

body{background: #333;margin:;padding:;}
.box{position: absolute;top:100px;left:200px;width:460px;height:360px;}
.main{position: absolute;width: 200px;height: 200px;border:1px solid #fff;z-index:;cursor: move;opacity: .3;background: #fff;}
.minDiv{width:8px;height:8px;background: #fff;font-size:;position: absolute;}
.minDiv.left-up{top:-4px;left: -4px;cursor:nw-resize;}
.minDiv.left{top:50%;margin-top:-4px;left: -4px;cursor:e-resize;}
.minDiv.left-down{bottom:-4px;left: -4px;cursor:sw-resize;}
.minDiv.top{top:-4px;left: 50%;margin-left:-4px;cursor:n-resize;}
.minDiv.right-up{top:-4px;right: -4px;cursor:ne-resize;}
.minDiv.right{top:50%;margin-top:-4px;right: -4px;cursor:e-resize;}
.minDiv.right-down{bottom:-4px;right: -4px;cursor:se-resize;}
.minDiv.bottom{bottom:-4px;left: 50%;margin-left:-4px;cursor:s-resize;}
img{position: absolute;z-index:}
.img2{clip:rect(0px,20px,100px,100px);}
#preview{position: absolute;top:100px;left:680px;width:460px;height:360px;}
#preview #img3{position: absolute;top:;left:;}

js部分

var main=document.getElementById('main');
var box=document.getElementById('box');
var rightdown=document.getElementById('right-down');
var leftup=document.getElementById('left-up');
var img3=document.getElementById('img3');
var cx='';
var cy='';
daer(main,main,'yd');//移动
daer(rightdown,main,'yx');//右下拉
function daer(obj,main,ws){
obj.onmousedown=function(e){
var e=window.event || e;//鼠标按下的坐标
cx=e.clientX-box.offsetLeft;
cy=e.clientY-box.offsetTop;
console.log(cx,cy);
document.onmousemove=function(e){
var e=window.event || e;//移动的坐标
if(ws=='yx'){
zcx=e.clientX-box.offsetLeft-main.offsetLeft;
zcy=e.clientY-box.offsetTop-main.offsetTop;
cx=main.offsetLeft;
cy=main.offsetTop;
main.style.width=zcx+'px';
main.style.height=zcy+'px';
img3.style.clip="rect("+cy+"px,"+parseInt(zcx+cx)+"px,"+parseInt(zcy+cy)+"px,"+cx+"px)";//clip裁切属性
}else if(ws=='yd'){
zcx=e.clientX-box.offsetLeft-main.offsetWidth/2;
zcy=e.clientY-box.offsetTop-main.offsetHeight/2;
cxw=main.offsetWidth;
cyh=main.offsetHeight;
main.style.top=zcy+'px';
main.style.left=zcx+'px';
img3.style.top=-zcy+'px';
img3.style.left=-zcx+'px';
img3.style.clip="rect("+zcy+"px,"+parseInt(cxw+zcx)+"px,"+parseInt(cyh+zcy)+"px,"+zcx+"px)";
}
}
document.onmouseup=function(e){
var e=window.event || e;//鼠标抬起的坐标
if(ws=='yx'){
cx=main.offsetLeft;
cy=main.offsetTop;
}else if(ws=='yd'){
cx=e.clientX-box.offsetLeft-main.offsetWidth/2;
cy=e.clientY-box.offsetTop-main.offsetHeight/2;
}
cxw=main.offsetWidth;
cyh=main.offsetHeight;
console.log(cx,cy,cxw,cyh);//cx是x坐标,cy是y坐标,cxw是宽,cyh是高
document.onmousemove=null;
}
stopEvt(e);
} }
//阻止冒泡
function stopEvt(e) {
e = e || window.event;
if(e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
}

效果图

js图片裁切的更多相关文章

  1. 兼容好的JS图片上传预览代码

    转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...

  2. JS图片自动和可控的轮播切换特效

    点击这里查看效果:http://hovertree.com/texiao/js/1.htm HTML文件代码如下: <!DOCTYPE html> <html xmlns=" ...

  3. Javascript图片裁切

    最近浏览了不少网站的图片裁切效果,大部分的做法如下图所示(借用一张脚本之家的图片),通过改变裁切框的大小来选取合适的位置. 但本文介绍的是另外一种裁切方式,裁切框由开发者决定,图片大小由用户决定,通过 ...

  4. Spring MVC程序中得到静态资源文件css,js,图片

    转载自:http://www.blogjava.net/fiele/archive/2014/08/24/417283.html 用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题, ...

  5. JS图片延迟加载分析及简单的demo

    JS图片延迟加载 图片延迟加载也称 "懒加载",通常应用于图片比较多的网页,比如 "美丽说首页","蘑菇街"等一些导购网站上用的比较多,或者 ...

  6. Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

    上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...

  7. Android实现图片裁切

    介绍 在应用开发中,如果涉及到个人信息,头像一般是不可避免的,类似这种情况,我们就需要用到图片裁切的功能,实现头像裁切,然后上传给服务器. 一般裁切的做法就是图层叠加选取框,然后根据坐标,计算裁切区域 ...

  8. 图片裁切插件jCrop的使用心得(三)

    在这一篇里,我来具体讲讲代码该如何写. 下面是jCrop的初始化代码 //图片裁剪插件Jcrop初始化 function initJcrop() { // 图片加载完成 document.getEle ...

  9. 图片裁切插件jCrop的使用心得(二)

    上一篇简单的介绍了一下开发的背景以及一些学习资料,下面开始介绍如何上手. 一.下载jCrop http://deepliquid.com/content/Jcrop_Download.html 直接去 ...

随机推荐

  1. HDU4004 二分答案

    第一道二分答案...今天看了大牛的博客,突然发现有个叫“二分枚举答案”的方法好像很牛,于是便搜了些资料..发现并不是很难,可能是我了解的只是冰山一脚罢了...加油ACMer!!!! #include& ...

  2. jeecms首页模板自定义

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qxy369/article/details/50387465我们在点击[查看首页]时,发现出现的并不 ...

  3. 使用ssh时报错:Service对象空指针异常

    有可能是spring容器不能自动生成service对象,导致空指针异常,常见的情况可能是在service前面加@Service注释

  4. Finalize什么时候被调用

    Finalize方法在垃圾回收结束时被调用,有五种一下情况会导致开始垃圾回收. 第0代已满  第0代满时,垃圾回收会自动开始.改时间是目前导致Finalize方法被调用的最常见的一种方式,因为随着应用 ...

  5. MATLAB---dir函数

    dir函数是最常用的转换路径的函数,可以获得指定文件夹下的所有子文件夹和文件,并存放在一个文件结构的数组中,这个数组各结构体内容如下: name    -- 文件名 date    -- 修改日期 b ...

  6. 【python之路28】模块python与excel

    一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表.xlwt(写excel)表.openpyxl(可读写excel表)等.xlrd读数据较大的excel表时效 ...

  7. php 该如何获取从百度搜索进入网站的关键词

    清源分享一个php获取从百度搜索进入网站的关键词的代码,有需要的朋友可以参考一下:https://blog.csdn.net/u012275531/article/details/17609065 代 ...

  8. java swing调试时线程显示名字

    一般有一个默认名字 但是具体运行到哪一个线程,需要猜 为了节约时间,提高效率 可以给线程写个中文名(因为默认就是英文,写中文,一眼就能挑出来) 以RTC定时器为例子 final TimerRtc ti ...

  9. windows 环境下搭建docker私有仓库

    windows 环境下搭建docker私有仓库 1.在公用仓库中pull仓库镜像 docker pull regitry 2.启动仓库镜像 //-d意思是后台运行,-p是做端口映射,这里是将本地的50 ...

  10. oracle习题集-高级查询

    1.问题:查询每个员工的部门名称,列出员工姓名和部门名称 select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno 2. 问题: ...