程序说明图片切割那部分那里有说明了啊就是利用clip
clip的作用是“检索或设置对象的可视区域。可视区域外的部分是透明的。”
依据上-右-下-左的顺序提供自对象左上角为(0,0)坐标计算的四个偏移数值来剪切。
例如:div { position:absolute; width:60px; height:60px; clip:rect(0 20 50 10); }
注意position:absolute的设置是必须的(详细看手册)。下面说说具体实现原理:
首先需要一个容器,拖放对象,图片地址,显示宽度和高度,
还要插入三个层:
底图层:那个半透明的图片,
显示层:拖放对象中正常显示的那个部分,
拖动层:就是拖放对象,
其中为了底图层和显示层能完美的重叠,我两个层都用了绝对定位,定在左上角。
clip的作用是“检索或设置对象的可视区域。可视区域外的部分是透明的。”
依据上-右-下-左的顺序提供自对象左上角为(0,0)坐标计算的四个偏移数值来剪切。
例如:div { position:absolute; width:60px; height:60px; clip:rect(0 20 50 10); }
注意position:absolute的设置是必须的(详细看手册)。下面说说具体实现原理:
首先需要一个容器,拖放对象,图片地址,显示宽度和高度,
还要插入三个层:
底图层:那个半透明的图片,
显示层:拖放对象中正常显示的那个部分,
拖动层:就是拖放对象,
其中为了底图层和显示层能完美的重叠,我两个层都用了绝对定位,定在左上角。
解决方案 »
- javascript问题,求教告诉
- 如何把字符串"123,abc,xy,hi"转换成数组或者jQuery数组?
- javascript代码不运行
- 获取table下控件的值
- 求一段JS替换后排序并显示出来的问题
- 对于一个链接 ,如何添加鼠标点击事件
- 我想实现当用户点击alert确定按钮后系统自动另外打开一个网页窗口怎样实现呢用其它的方法也行啊,请给出示例代码?
- 连接用新窗口(target="_blank")打开一个网页,但这个窗口默认不是最大化,我要最大化,如何做
- 要想正常运行var objdbConn = new ActiveXObject("ADODB.Connection");浏览器哪些地方的哪个选项需要做哪些改动, 详见内
- 关于浏览器的标签与窗口的区别
- 高手帮忙看看
- 如何一个JS控制多图片大小?
this._pic = this.Container.appendChild(document.createElement("img"));//图片对象
appendChild就是插入容器了这部分设置的半透明
if(isIE){
this._pic.style.filter = "alpha(opacity:" + this.Opacity + ")";
} else {
this._pic.style.opacity = this.Opacity / 100;
}
用clip来“设置对象的可视区域”
this._odiv = document.createElement("div");
this.oDrag.style.overflow = "hidden";
(function(style){
style.width = style.height = "100%"; style.position = "absolute";style.backgroundColor = "#FFFFFF";style.zIndex = 300;
})(this._odiv.style) this.oDrag.appendChild(this._odiv)
不是同一个div来的啊拖动层仅仅是那个拖动的div跟图片无关的关键是显示层
显示层是显示的图片,本来是透明的,要根据拖动层的数据来设置clip来“设置对象的可视区域”
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<style>
#oContent{
border: 3px solid #000033;
height: 600px;
width: 402px;
position:relative;
}
#oDiv{
width: 100px;
color: #C0C0C0;
border: 1 solid #000000;
height: 100px;
font-size:9pt;
text-align:center;
z-index: 100;
position: absolute; }
</style>
<script language="JavaScript">
<!--
var $ = function(id){
return 'string' == typeof id ? document.getElementById(id) : id;
} function addEventHandler(oTarget, sEventType, fnHandler){
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType, fnHandler, false);
}else if(oTarget.attachEvent){
oTarget.attachEvent("on" + sEventType, fnHandler);
}else{
oTarget["on" + sEventType] = fnHandler;
}
} function removeEventHandler(oTarget, sEventType, fnHandler){
if(oTarget.removeEventListener){
oTarget.removeEventListener(sEventType, fnHandler, false);
}else if(oTarget.detachEvent){
oTarget.detachEvent("on" + sEventType, fnHandler);
}else{
oTarget["on" + sEventType] = null;
}
} var isIE = (document.all) ? true : false; var Class = {
create : function(){
return function(){
this.initialize.apply(this, arguments);
}
}
} Object.extend = function(distination, source){
for(var property in source){
distination[property] = source[property];
}
return distination;
} var Drag = Class.create();
Drag.prototype = {
initialize : function(idContent, idDrag, options){
var oThis = this;
this._fM = function(e){oThis.mouseMove(e || window.event)};
this._fU = function(e){oThis.mouseUp(e || window.event)};
this._x = 0;this.y = 0;
this.oDrag = $(idDrag);this.oContent = $(idContent);
this.setOptions(options);
addEventHandler(this.oDrag, "mousedown", function(e){oThis.mouseDown(e || window.event)})
}, setOptions : function(options){
this.options = {
Limit : true //true: 限制范围 ,false: 不限制范围
};
Object.extend(this.options, options || {});
}, mouseDown : function(oEvent){
this._x = oEvent.clientX - this.oDrag.offsetLeft;
this._y = oEvent.clientY - this.oDrag.offsetTop;
if(isIE){
addEventHandler(this.oDrag, "losecapture", this._fU);
this.oDrag.setCapture();
}else{
window.getSelection && window.getSelection().removeAllRanges();
}
addEventHandler(document, "mousemove",this._fM);
addEventHandler(document, "mouseup",this._fU);
}, mouseMove : function(oEvent){
var iLeft = oEvent.clientX - this._x;
var iTop = oEvent.clientY - this._y;
// 限制范围时候的坐标判断
if(this.options.Limit){
var iMaxLeft = iMaxTop = 0;
var iMaxRight = this.oContent.clientWidth;
var iMaxBottom = this.oContent.clientHeight;
var bLock = false; // 获取超出长度
var iRight = this.oDrag.offsetWidth + iLeft - iMaxRight;
var iBottom = this.oDrag.offsetHeight + iTop - iMaxBottom; if(iRight > 0){iLeft -= iRight;}
if(iBottom > 0){iTop -= iBottom;}
if(iMaxLeft > iLeft) {iLeft = iMaxLeft;}
if(iMaxTop > iTop){iTop = iMaxTop;}
}
this.oDrag.style.left = iLeft;
this.oDrag.style.top = iTop; }, mouseUp : function(oEvent){
if(isIE){
removeEventHandler(this.oDrag, "losecapture", this._fU);
this.oDrag.releaseCapture();
}
removeEventHandler(document, "mousemove",this._fM );
removeEventHandler(document, "mouseup",this._fU );
}
}
var ImageClip = Class.create(); ImageClip.prototype = {
initialize : function(oContent, oDrag, sUrl){
var oThis = this;
this.oContent = $(oContent);this.oDrag = $(oDrag);
this.oContent.style.overflow = "hidden";
this.sUrl = sUrl; // 1.添加背景图片并设置为半透明
this._ipc = document.createElement("img");
(function(obj){
obj.src = oThis.sUrl;obj.style.width = obj.style.height = "100%";
obj.style.zIndex = 200;obj.style.position = "absolute";obj.style.filter = "alpha(opacity:50)";
})(this._ipc)
this.oContent.appendChild(this._ipc); // 2.将拖拽层填充透明层
this.oDrag.style.overflow = "hidden";
this._odiv = document.createElement("div");
(function(style){
style.width = style.height = "100%"; style.backgroundColor = "#FFFFFF";style.zIndex = 300;
style.filter = "alpha(opacity:0)";
})(this._odiv.style) this.oDrag.appendChild(this._odiv)
// 3. 设置层次
this.oContent.style.zIndex = 0;
this._ipc.style.zIndex = 100;
this.oDrag.style.zIndex = 200;
}
}
//-->
</script>
</head> <body>
<div id="oContent">
<div id="oDiv"></div>
</div>
<script>
new Drag("oContent","oDiv",{limit : true});
new ImageClip("oContent", "oDiv", "http://ent.gz163.cn/images_ent/2008/7/30/20087303CB81946E3BC4296AB22A5B84FCDA8EB.jpg");
</script>
</body>
</html>现在是这样的效果
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<style>
#oContent{
border: 3px solid #000033;
height: 600px;
width: 402px;
position:relative;
}
#oDiv{
width: 100px;
color: #C0C0C0;
border: 1 solid #000000;
height: 100px;
font-size:9pt;
text-align:center;
z-index: 100;
position: absolute; }
</style>
<script language="JavaScript">
<!--
var $ = function(id){
return 'string' == typeof id ? document.getElementById(id) : id;
} function addEventHandler(oTarget, sEventType, fnHandler){
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType, fnHandler, false);
}else if(oTarget.attachEvent){
oTarget.attachEvent("on" + sEventType, fnHandler);
}else{
oTarget["on" + sEventType] = fnHandler;
}
} function removeEventHandler(oTarget, sEventType, fnHandler){
if(oTarget.removeEventListener){
oTarget.removeEventListener(sEventType, fnHandler, false);
}else if(oTarget.detachEvent){
oTarget.detachEvent("on" + sEventType, fnHandler);
}else{
oTarget["on" + sEventType] = null;
}
} var isIE = (document.all) ? true : false; var Class = {
create : function(){
return function(){
this.initialize.apply(this, arguments);
}
}
} Object.extend = function(distination, source){
for(var property in source){
distination[property] = source[property];
}
return distination;
} var Drag = Class.create();
Drag.prototype = {
initialize : function(idContent, idDrag, options){
var oThis = this;
this._fM = function(e){oThis.mouseMove(e || window.event)};
this._fU = function(e){oThis.mouseUp(e || window.event)};
this._x = 0;this.y = 0;
this.oDrag = $(idDrag);this.oContent = $(idContent);
this.setOptions(options);
addEventHandler(this.oDrag, "mousedown", function(e){oThis.mouseDown(e || window.event)})
this.onMove = function(){}
}, setOptions : function(options){
this.options = {
Limit : true //true: 限制范围 ,false: 不限制范围
};
Object.extend(this.options, options || {});
}, mouseDown : function(oEvent){
this._x = oEvent.clientX - this.oDrag.offsetLeft;
this._y = oEvent.clientY - this.oDrag.offsetTop;
if(isIE){
addEventHandler(this.oDrag, "losecapture", this._fU);
this.oDrag.setCapture();
}else{
window.getSelection && window.getSelection().removeAllRanges();
}
addEventHandler(document, "mousemove",this._fM);
addEventHandler(document, "mouseup",this._fU);
}, mouseMove : function(oEvent){
var iLeft = oEvent.clientX - this._x;
var iTop = oEvent.clientY - this._y;
// 限制范围时候的坐标判断
if(this.options.Limit){
var iMaxLeft = iMaxTop = 0;
var iMaxRight = this.oContent.clientWidth;
var iMaxBottom = this.oContent.clientHeight;
var bLock = false; // 获取超出长度
var iRight = this.oDrag.offsetWidth + iLeft - iMaxRight;
var iBottom = this.oDrag.offsetHeight + iTop - iMaxBottom; if(iRight > 0){iLeft -= iRight;}
if(iBottom > 0){iTop -= iBottom;}
if(iMaxLeft > iLeft) {iLeft = iMaxLeft;}
if(iMaxTop > iTop){iTop = iMaxTop;}
}
this.oDrag.style.left = iLeft;
this.oDrag.style.top = iTop;
this.onMove(); }, mouseUp : function(oEvent){
if(isIE){
removeEventHandler(this.oDrag, "losecapture", this._fU);
this.oDrag.releaseCapture();
}
removeEventHandler(document, "mousemove",this._fM );
removeEventHandler(document, "mouseup",this._fU );
}
}
var ImageClip = Class.create(); ImageClip.prototype = {
initialize : function(oContent, oDrag, sUrl){
var oThis = this;
this.oContent = $(oContent);this.oDrag = $(oDrag);
this.oContent.style.overflow = "hidden";
this.sUrl = sUrl; // 1.添加背景图片并设置为半透明
this._ipc = document.createElement("img");
(function(obj){
obj.src = oThis.sUrl;obj.style.width = obj.style.height = "100%";
obj.style.zIndex = 200;obj.style.position = "absolute";obj.style.filter = "alpha(opacity:50)";
})(this._ipc)
this.oContent.appendChild(this._ipc);
// 2.将拖拽层填充透明层
this.oDrag.style.overflow = "hidden";
this._odiv = document.createElement("div");
(function(style){
style.width = style.height = "100%"; style.backgroundColor = "#FFFFFF";style.zIndex = 300;
style.filter = "alpha(opacity:0)";
})(this._odiv.style)//显示层
this._showpic = document.createElement("img");
(function(obj){
obj.src = oThis.sUrl;obj.style.width = obj.style.height = "100%";
obj.style.zIndex = 150;obj.style.position = "absolute";
})(this._showpic)
this.oContent.appendChild(this._showpic);
var od = new Drag("oContent","oDiv",{limit : true});
od.onMove = function(){
var o = oThis.oDrag;
//按拖放对象的参数进行切割
oThis._showpic.style.clip = "rect(" + o.offsetTop + "px " + (o.offsetLeft + o.offsetWidth) + "px " + (o.offsetTop + o.offsetHeight) + "px " + o.offsetLeft + "px)";
}
od.onMove();
//设置overflow解决ie6的渲染问题(缩放时填充对象高度的问题)
this.oDrag.style.overflow = "hidden";
//ie下用一个透明的层填充拖放对象 不填充的话onmousedown会失效(未知原因)
(function(style){
style.width = style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
})(this.oDrag.appendChild(document.createElement("div")).style)
// 3. 设置层次
this.oContent.style.zIndex = 0;
this._ipc.style.zIndex = 100;
this.oDrag.style.zIndex = 200;
}
}
//-->
</script>
</head> <body>
<div id="oContent">
<div id="oDiv"></div>
</div>
<script> new ImageClip("oContent", "oDiv", "http://ent.gz163.cn/images_ent/2008/7/30/20087303CB81946E3BC4296AB22A5B84FCDA8EB.jpg");
</script>
</body>
</html>
帮你改好了