_catchKey: function (e) {
var img = shiftzoom._shiftzoom = this; var t, ox, oy, ex, ey, px = 0, py = 0; e = e ? e : window.event;
function findPosXY(ele) { var t, d = { x: ele.offsetLeft, y: ele.offsetTop }; if (ele.offsetParent) { t = findPosXY(ele.offsetParent); d.x += t.x; d.y += t.y; } return d; }
t = findPosXY(img.parentNode); ex = e.clientX; ey = e.clientY; if (e.pageX || e.pageY) { px = e.pageX; py = e.pageY; }
if (self.pageXOffset || self.pageYOffset) { ox = self.pageXOffset; if (ox > 0 && px == ex) { ex -= ox; } oy = self.pageYOffset; if (oy > 0 && py == ey) { ey -= oy; } } else
if (document.documentElement) { ox = document.documentElement.scrollLeft; oy = document.documentElement.scrollTop; } else
if (document.body) { ox = document.body.scrollLeft; oy = document.body.scrollTop; }
img.mouseX = Math.min(Math.max(ex + ox - t.x, 0), img.minwidth); img.mouseY = Math.min(Math.max(ey + oy - t.y, 0), img.minheight); if (((e.shiftkey || !e.altkey) || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-out"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-out"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-out.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomout) { butt = document.getElementById(img.zoutid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomout = true; }
sw = img.width; ew = (img.width - img.minwidth) * -1; sh = img.height; eh = (img.height - img.minheight) * -1; sx = parseInt(img.style.left); ex = sx * -1; sy = parseInt(img.style.top); ey = sy * -1;
st = Math.max(1, Math.round((img.width / img.minwidth) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomOut('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (((e.shiftkey || !e.altkey) || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-in"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-in"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-in.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomin) { butt = document.getElementById(img.zoinid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomin = true; }
sw = img.width; ew = img.maxwidth - img.width; sh = img.height; eh = img.maxheight - img.height; sx = parseInt(img.style.left); sy = parseInt(img.style.top);
ex = Math.max(0, Math.min(ew, Math.round(((img.mouseX - sx) * (img.maxwidth / img.width)) - (img.minwidth * 0.5) + sx))) * -1;
ey = Math.max(0, Math.min(eh, Math.round(((img.mouseY - sy) * (img.maxheight / img.height)) - (img.minheight * 0.5) + sy))) * -1;
st = Math.max(1, Math.round((img.maxwidth / img.width) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomIn('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (img.width > img.minwidth || img.height > img.minheight) {
if (img.gecko) { img.style.cursor = "-moz-grabbing"; } else if (img.trident) { img.style.cursor = "url('" + img.curpath + "grabbing.cur'),move"; } else { img.style.cursor = "move"; }
var x = parseInt(img.style.left), y = parseInt(img.style.top); img.mouseX = e.clientX; img.mouseY = e.clientY;
document.onmousemove = shiftzoom._whilePan; document.onmouseup = shiftzoom._stopPan;
} return false;
},(e.shiftkey || !e.altkey) 这个条件如何改成 由鼠标滑轮滚动 来改变图片放大缩小呢?
var img = shiftzoom._shiftzoom = this; var t, ox, oy, ex, ey, px = 0, py = 0; e = e ? e : window.event;
function findPosXY(ele) { var t, d = { x: ele.offsetLeft, y: ele.offsetTop }; if (ele.offsetParent) { t = findPosXY(ele.offsetParent); d.x += t.x; d.y += t.y; } return d; }
t = findPosXY(img.parentNode); ex = e.clientX; ey = e.clientY; if (e.pageX || e.pageY) { px = e.pageX; py = e.pageY; }
if (self.pageXOffset || self.pageYOffset) { ox = self.pageXOffset; if (ox > 0 && px == ex) { ex -= ox; } oy = self.pageYOffset; if (oy > 0 && py == ey) { ey -= oy; } } else
if (document.documentElement) { ox = document.documentElement.scrollLeft; oy = document.documentElement.scrollTop; } else
if (document.body) { ox = document.body.scrollLeft; oy = document.body.scrollTop; }
img.mouseX = Math.min(Math.max(ex + ox - t.x, 0), img.minwidth); img.mouseY = Math.min(Math.max(ey + oy - t.y, 0), img.minheight); if (((e.shiftkey || !e.altkey) || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-out"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-out"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-out.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomout) { butt = document.getElementById(img.zoutid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomout = true; }
sw = img.width; ew = (img.width - img.minwidth) * -1; sh = img.height; eh = (img.height - img.minheight) * -1; sx = parseInt(img.style.left); ex = sx * -1; sy = parseInt(img.style.top); ey = sy * -1;
st = Math.max(1, Math.round((img.width / img.minwidth) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomOut('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (((e.shiftkey || !e.altkey) || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-in"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-in"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-in.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomin) { butt = document.getElementById(img.zoinid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomin = true; }
sw = img.width; ew = img.maxwidth - img.width; sh = img.height; eh = img.maxheight - img.height; sx = parseInt(img.style.left); sy = parseInt(img.style.top);
ex = Math.max(0, Math.min(ew, Math.round(((img.mouseX - sx) * (img.maxwidth / img.width)) - (img.minwidth * 0.5) + sx))) * -1;
ey = Math.max(0, Math.min(eh, Math.round(((img.mouseY - sy) * (img.maxheight / img.height)) - (img.minheight * 0.5) + sy))) * -1;
st = Math.max(1, Math.round((img.maxwidth / img.width) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomIn('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (img.width > img.minwidth || img.height > img.minheight) {
if (img.gecko) { img.style.cursor = "-moz-grabbing"; } else if (img.trident) { img.style.cursor = "url('" + img.curpath + "grabbing.cur'),move"; } else { img.style.cursor = "move"; }
var x = parseInt(img.style.left), y = parseInt(img.style.top); img.mouseX = e.clientX; img.mouseY = e.clientY;
document.onmousemove = shiftzoom._whilePan; document.onmouseup = shiftzoom._stopPan;
} return false;
},(e.shiftkey || !e.altkey) 这个条件如何改成 由鼠标滑轮滚动 来改变图片放大缩小呢?
<script type="text/javaScript">
var mywheel=function(e){
e=e || window.event;
var flag=e.wheelDelta/*IE/Chrome */||e.detail/*Firefox*/;
var enlarge=flag==120||flag==3 ? true : false; if(enlarge){
//放大图片的代码
}
else{
//缩小图片的代码
}
}if(document.addEventListener){
document.addEventListener('DOMMouseScroll',mywheel,false);
}
window.onmousewheel=document.onmousewheel=mywheel;
</script>
如果你的上述JS代码是保存在一个.js文件里让页面调用的话,就放在.js文件的最底部就可以了
_catchKey: function (e) {
if(document.addEventListener){
document.addEventListener('DOMMouseScroll',mywheel,false);
}
window.onmousewheel=document.onmousewheel=mywheel;
var img = shiftzoom._shiftzoom = this; var t, ox, oy, ex, ey, px = 0, py = 0; e = e ? e : window.event;
=============
mywheel()函数放在外面<script></script>标签中
这个里面的阿,把mywheel()函数放在外面<script></script>标签中
,这个放大缩小代码怎么放??
<script type="text/javaScript">
function aaaaaaaaaaaaaaaaa(){//假设这是你原来的封装函数名
//开始监听滚轮事件
if(document.addEventListener){
document.addEventListener('DOMMouseScroll',this.mywheel,false);
}
window.onmousewheel=document.onmousewheel=this.mywheel;
//定义滚轮事件的处理函数
var mywheel=function(e){
e=e || window.event;
var flag=e.wheelDelta/*IE/Chrome */||e.detail/*Firefox*/;
var enlarge=flag==120||flag==3 ? true : false;
/*不要这个判断,直接找你原来下面的判断
if(enlarge){
//放大图片的代码
}
else{
//缩小图片的代码
}
*/
}; //你原来的函数和变量
var img = shiftzoom._shiftzoom = this;
var t, ox, oy, ex, ey, px = 0, py = 0; e = e ? e : window.event;
function findPosXY(ele) { var t, d = { x: ele.offsetLeft, y: ele.offsetTop }; if (ele.offsetParent) { t = findPosXY(ele.offsetParent); d.x += t.x; d.y += t.y; } return d; }
t = findPosXY(img.parentNode); ex = e.clientX; ey = e.clientY;
if (e.pageX || e.pageY) { px = e.pageX; py = e.pageY; }
if (self.pageXOffset || self.pageYOffset) { ox = self.pageXOffset; if (ox > 0 && px == ex) { ex -= ox; } oy = self.pageYOffset; if (oy > 0 && py == ey) { ey -= oy; } }
else if (document.documentElement) { ox = document.documentElement.scrollLeft; oy = document.documentElement.scrollTop; }
else if (document.body) { ox = document.body.scrollLeft; oy = document.body.scrollTop; } img.mouseX = Math.min(Math.max(ex + ox - t.x, 0), img.minwidth);
img.mouseY = Math.min(Math.max(ey + oy - t.y, 0), img.minheight);
/***********************************************************************/
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是enlarge,缩小就是!enlarge*/
if ((elarge || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
// if (((e.shiftkey || !e.altkey) || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-out"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-out"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-out.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomout) { butt = document.getElementById(img.zoutid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomout = true; }
sw = img.width; ew = (img.width - img.minwidth) * -1; sh = img.height; eh = (img.height - img.minheight) * -1; sx = parseInt(img.style.left); ex = sx * -1; sy = parseInt(img.style.top); ey = sy * -1;
st = Math.max(1, Math.round((img.width / img.minwidth) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomOut('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
}
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是enlarge,缩小就是!enlarge*/
else if ((!enlarge || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
///////////else if (((e.shiftkey || !e.altkey) || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-in"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-in"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-in.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomin) { butt = document.getElementById(img.zoinid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomin = true; }
sw = img.width; ew = img.maxwidth - img.width; sh = img.height; eh = img.maxheight - img.height; sx = parseInt(img.style.left); sy = parseInt(img.style.top);
ex = Math.max(0, Math.min(ew, Math.round(((img.mouseX - sx) * (img.maxwidth / img.width)) - (img.minwidth * 0.5) + sx))) * -1;
ey = Math.max(0, Math.min(eh, Math.round(((img.mouseY - sy) * (img.maxheight / img.height)) - (img.minheight * 0.5) + sy))) * -1;
st = Math.max(1, Math.round((img.maxwidth / img.width) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomIn('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (img.width > img.minwidth || img.height > img.minheight) {
if (img.gecko) { img.style.cursor = "-moz-grabbing"; } else if (img.trident) { img.style.cursor = "url('" + img.curpath + "grabbing.cur'),move"; } else { img.style.cursor = "move"; }
var x = parseInt(img.style.left), y = parseInt(img.style.top); img.mouseX = e.clientX; img.mouseY = e.clientY;
document.onmousemove = shiftzoom._whilePan; document.onmouseup = shiftzoom._stopPan;
} return false;
},
。。
}//假设这是调用你原来的封装函数
aaaaaaaaaaaaaaaaa();
</script>
<script type="text/javaScript">
function aaaaaaaaaaaaaaaaa(){//假设这是你原来的封装函数名
//开始监听滚轮事件
if(document.addEventListener){
document.addEventListener('DOMMouseScroll',this.mywheel,false);
}
window.onmousewheel=document.onmousewheel=this.mywheel;
var enlarge=-1;//定义放大缩小标志 0:什么都不做 -1:缩小 +1:放大
var mywheel=function(e){//定义滚轮事件的处理函数
e=e || window.event;
var flag=e.wheelDelta/*IE/Chrome */||e.detail/*Firefox*/;
enlarge=flag==120||flag==3 ? 1 : -1;/*不要这个判断,直接找你原来下面的判断
if(enlarge){
//放大图片的代码
}
else{
//缩小图片的代码
}
*/
}; //你原来的函数和变量
var img = shiftzoom._shiftzoom = this;
var t, ox, oy, ex, ey, px = 0, py = 0; e = e ? e : window.event;
function findPosXY(ele) { var t, d = { x: ele.offsetLeft, y: ele.offsetTop }; if (ele.offsetParent) { t = findPosXY(ele.offsetParent); d.x += t.x; d.y += t.y; } return d; }
t = findPosXY(img.parentNode); ex = e.clientX; ey = e.clientY;
if (e.pageX || e.pageY) { px = e.pageX; py = e.pageY; }
if (self.pageXOffset || self.pageYOffset) { ox = self.pageXOffset; if (ox > 0 && px == ex) { ex -= ox; } oy = self.pageYOffset; if (oy > 0 && py == ey) { ey -= oy; } }
else if (document.documentElement) { ox = document.documentElement.scrollLeft; oy = document.documentElement.scrollTop; }
else if (document.body) { ox = document.body.scrollLeft; oy = document.body.scrollTop; } img.mouseX = Math.min(Math.max(ex + ox - t.x, 0), img.minwidth);
img.mouseY = Math.min(Math.max(ey + oy - t.y, 0), img.minheight);
/***********************************************************************/
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是enlarge==1,缩小就是!enlarge==-1 */
if ((elarge==1 || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
// if (((e.shiftkey || !e.altkey) || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-out"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-out"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-out.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomout) { butt = document.getElementById(img.zoutid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomout = true; }
sw = img.width; ew = (img.width - img.minwidth) * -1; sh = img.height; eh = (img.height - img.minheight) * -1; sx = parseInt(img.style.left); ex = sx * -1; sy = parseInt(img.style.top); ey = sy * -1;
st = Math.max(1, Math.round((img.width / img.minwidth) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomOut('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
}
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是enlarge==1,缩小就是!enlarge==-1 */
else if ((enlarge==-1 || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
///////////else if (((e.shiftkey || !e.altkey) || img.zoomin) && (img.width < img.maxwidth || img.height < img.maxheight)) {
var butt, sw, ew, sh, eh, sx, ex, sy, ey, st; if (img.gecko) { img.style.cursor = "-moz-zoom-in"; } else if (img.webkit) { img.style.cursor = "-webkit-zoom-in"; }
else if (img.trident) { img.style.cursor = "url('" + img.curpath + "zoom-in.cur'),crosshair"; } else { img.style.cursor = "crosshair"; } img.pointer = img.style.cursor;
if (!img.zoomin) { butt = document.getElementById(img.zoinid).style; butt.border = img.bc; butt.borderLeft = img.dc; butt.borderTop = img.dc; img.zoomin = true; }
sw = img.width; ew = img.maxwidth - img.width; sh = img.height; eh = img.maxheight - img.height; sx = parseInt(img.style.left); sy = parseInt(img.style.top);
ex = Math.max(0, Math.min(ew, Math.round(((img.mouseX - sx) * (img.maxwidth / img.width)) - (img.minwidth * 0.5) + sx))) * -1;
ey = Math.max(0, Math.min(eh, Math.round(((img.mouseY - sy) * (img.maxheight / img.height)) - (img.minheight * 0.5) + sy))) * -1;
st = Math.max(1, Math.round((img.maxwidth / img.width) * 3)); document.onmouseup = shiftzoom._stopZoom;
cvi_sztimer = setInterval("shiftzoom._zoomIn('" + img.id + "',0," + st + "," + sw + "," + ew + "," + sh + "," + eh + "," + sx + "," + ex + "," + sy + "," + ey + ")", img.millisec);
} else if (img.width > img.minwidth || img.height > img.minheight) {
if (img.gecko) { img.style.cursor = "-moz-grabbing"; } else if (img.trident) { img.style.cursor = "url('" + img.curpath + "grabbing.cur'),move"; } else { img.style.cursor = "move"; }
var x = parseInt(img.style.left), y = parseInt(img.style.top); img.mouseX = e.clientX; img.mouseY = e.clientY;
document.onmousemove = shiftzoom._whilePan; document.onmouseup = shiftzoom._stopPan;
} return false;
},}//假设这是调用你原来的封装函数
aaaaaaaaaaaaaaaaa();
</script>
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是enlarge==1,缩小就是!enlarge==-1 */
if ((elarge==1 || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {
----------->改为
/*这里做替换,自己摸索一下哪里是放大,哪里是缩小;放大就是this.enlarge==1,缩小就是this.enlarge==-1 */
if ((this.enlarge==1 || img.zoomout) && (img.width > img.minwidth || img.height > img.minheight)) {下面那个也要改
<img id=img1 src="http://c.csdn.net/bbs/t/5/i/pic_logo.gif" alt="test">
<script type="text/javaScript">
var aaaaaaaaaaaaaaaaa={//假设这是你原来的封装对象名
_catchKey: function (e,enlarge) {
var img = document.getElementById("img1");
var w=img.offsetWidth, y=img.offsetHeight;
if(enlarge){
img.style.width=w*1.1; img.style.height=w*1.1;
}
else if(!enlarge){
img.style.width=w/1.1; img.style.height=w/1.1;
}
},
xxx:"asadasdasdasd",//假设这是你原来的其他属性
yyy:"yyyyyyyyyyyyyyyyy"//假设这是你原来的其他属性
};/***************************************************************/
//定义滚轮事件的处理函数
function mywheel(e){
e=e || window.event;
var flag=e.wheelDelta/*IE/Chrome */||e.detail/*Firefox*/;
var enlarge=flag==120||flag==3 ? true : false;
try{
aaaaaaaaaaaaaaaaa._catchKey(e,enlarge);
}
catch(err){
alert(err.message)
}
} //监听滚轮事件
if(document.addEventListener){
document.addEventListener('DOMMouseScroll',mywheel(event),false);
}
window.onmousewheel=document.onmousewheel=mywheel;
</script>
给你个例子。其他的判断条件你自己对照原来的代码改吧。