日期:2014-05-16  浏览次数:20345 次

JavaScript 图片缩放 响应鼠标滚动 和 上下方向键
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   
    <title>查看图片</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
   
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script language="JavaScript">
function zoomimg(img,upOrDown){
   //img.style.zoom获取img对象的缩放比例,并转为十进制整数
   var zoom = parseInt(img.style.zoom,10);
   if (isNaN(zoom)){ //当zoom非数字时zoom默认为100%
   zoom = 100;
   }
   //event.wheelDelta滚轮移动量上移+120,下移-120或者按上下方向键,显示比例每次增减10%
   if(event.wheelDelta != 0){
    zoom += event.wheelDelta / 12;
   }else if("up" == upOrDown){
    zoom += 10;
   }else if("down" == upOrDown){
    zoom += -10;
   }
   //当zoom大于10%时重新设置显示比例
   if (zoom>10) img.style.zoom = zoom + "%";
   return false;
}
</script>
<style   type="text/css">  
.drag{position:relative;cursor:hand}  
</style>

<script   language="javascript">  
//初始化页面图片
function init()
{
var url=location.href;
var num=url.indexOf("?")
var picUrl = url.substr(num+1);
document.getElementById("pic").src = picUrl;
listenKey();
}
//注册事件监听键盘上下方向键
function getKey(e){
e = e || window.event;
var keycode = e.which ? e.which : e.keyCode;
var pic = document.getElementById("pic");
if(keycode == 38){ //如果按的是上方向键
   zoomimg(pic,"up");
}else if(keycode == 40){//如果按的是下方向键
   zoomimg(pic,"down");
}
}
// 把keydown事件绑定到document中
function listenKey() {
if (document.addEventListener) { //FF
   document.addEventListener("keydown",getKey,false);
} else if (document.attachEvent) { //IE
   document.attachEvent("onkeydown",getKey);
} else {
   document.onkeydown = getKey;
}
}

var   dragapproved   =   false;  
var   z,x,y  
function   move()  
{  
if(event.button==1&&dragapproved)  
{  
z.style.pixelLeft=temp1+event.clientX-x  
z.style.pixelTop   =   temp2+event.clientY-y  
return   false;  
}  
}  
function   drags()  
{  
if(!document.all)  
return  
if(event.srcElement.className=="drag")  
{  
dragapproved   =   true  
z=event.srcElement;  
temp1=z.style.pixelLeft  
temp2=z.style.pixelTop  
x=event.clientX  
y=event.clientY  
document.onmousemove=move  
}  
}  
document.onmousedown=drags  
document.onmouseup=new   Function("dragapproved=false")  
</scr