标签:
该插件是在窗口拖动效果的基础上实现的
回头再写详细点,时间比较急。先贴上代码
插件源码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="renderer" content="webkit"/>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>登录</title>
</head>
<style>
body{
background-color:#DCDCDC;
}
.container{
position:absolute;
height:400px;
width:300px;
border:1px solid black;
border-radius:3px;
background:url("http://oospace.sinaapp.com/img/cut.jpg") no-repeat;
background-color:black;
opacity:0.2;
filter:alpha(opacity=20);
top:100px;
left:100px
}
.imgLayer{
position:absolute;
background:url("http://oospace.sinaapp.com/img/cut.jpg") no-repeat;
height:400px;
width:300px;
clip:rect(20px 280px 300px 30px);
top:100px;
left:100px
}
.controlLayer{
position:absolute;
height:280px;
width:250px;
zoom:1;
border:1px dashed #DCDCDC;
top:120px;
left:130px;
/*cursor:move;*/
}
.mark{
border:1px solid #000000;
background-color:#ffffff;
opacity:0.8;
filter:alpha(opacity=80);
padding:2px;
height:3px;
width:3px;
}
.leftTop{
position:absolute;
top:-3px;
left:-3px;
cursor: se-resize;
}
.leftCenter{
position:absolute;
top:50%;
left:-3px;
cursor: w-resize;
}
.topCenter{
position:absolute;
top:-3px;
left:50%;
cursor: ns-resize;
}
.rightTop{
position:absolute;
top:-3px;
right:-3px;
cursor: ne-resize;
}
.rightCenter{
position:absolute;
top:50%;
right:-3px;
cursor: e-resize;
}
.leftBottom{
position:absolute;
bottom:-3px;
left:-3px;
cursor: ne-resize;
}
.bottomCenter{
position:absolute;
bottom:-3px;
left:50%;
cursor: s-resize;
}
.rightBottom{
position:absolute;
bottom:-3px;
right:-3px;
cursor: se-resize;
}
</style>
<body>
<div class="container">
</div>
<div class="imgLayer">
</div>
<div class="controlLayer">
<div class="leftTop mark"></div>
<div class="topCenter mark"></div>
<div class="rightTop mark"></div>
<div class="leftCenter mark"></div>
<div class="rightCenter mark"></div>
<div class="leftBottom mark"></div>
<div class="bottomCenter mark"></div>
<div class="rightBottom mark"></div>
</div>
</body>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script>
//拖动效果代码
//var _z=9999;
$(document).ready(function(){
var _move=false;//移动标记
var _x,_y;//鼠标离控件左上角的相对位置
var wd;//窗口
var img=$(".imgLayer");//图片
var ctain=$(".container");
$(".controlLayer").click(function(){
//alert("click");//点击(松开后触发)
this.style.cursor = "default";//鼠标形状
//this.style.zIndex = 999;
}).mousedown(function(e){
_move=true;
wd=$(this);
this.style.cursor = "move";//鼠标形状
//this.style.zIndex = _z;//窗口层次
//_z++;
_x=e.pageX-parseInt(wd.css("left"));
_y=e.pageY-parseInt(wd.css("top"));
//_ximg=e.pageX-parseInt(img.css("left"));
//_yimg=e.pageX-parseInt(img.css("top"));
/* wd.fadeTo(20, 0.25); *///点击后开始拖动并透明显示
$(document).mousemove(function(e){
if(_move){
var x=e.pageX-_x;//移动时根据鼠标位置计算控件左上角的绝对位置
var y=e.pageY-_y;
//设置controlLayer的范围
var w_ctn=parseInt(ctain.css("width"));
var h_ctn=parseInt(ctain.css("height"));
var l_ctn=parseInt(ctain.css("left"));
var t_ctn=parseInt(ctain.css("top"));
var w_wd=parseInt(wd.css("width"));
var h_wd=parseInt(wd.css("height"));
//设置imgLayer的范围
var top=y-t_ctn+"px";
var right=parseInt(wd.css("width"))+x-l_ctn+"px";
var bottom=parseInt(wd.css("height"))+y-t_ctn+"px";
var left=x-l_ctn+"px";
var rect="rect( "+top+" "+right+" "+bottom+" "+left+" )";
//根据container控制移动范围
if(w_ctn>=(x+w_wd-l_ctn)&&h_ctn>=(y+h_wd-t_ctn)&&(y>=t_ctn)&&(x>=l_ctn)){
wd.css({top:y,left:x});//控件新位置
img.css("clip",rect);//切割位置
//img.css({top:ctain.css("top"),left:ctain.css("left")});//切割显示图片新位置
}
//var ximg=e.pageX-_ximg;//移动时根据鼠标位置计算控件左上角的绝对位置
//var yimg=e.pageY-_yimg;
//img.css({top:yimg,left:ximg});//图片新位置
}
}).mouseup(function(){
_move=false;
/* wd.fadeTo("fast", 1); *///松开鼠标后停止移动并恢复成不透明
});
});
});
</script>
</html>
效果:http://oospace.sinaapp.com/cut.php
标签:
原文地址:http://my.oschina.net/oospace/blog/360994