js用拖动滑块来控制图片大小的方法
本文实例讲述了js用拖动滑块来控制图片大小的方法。分享给大家供大家参考。具体实现方法如下:
<!doctypehtmlpublic"-//W3C//DTDHTML4.0Transitional//EN"> <html> <head> <metahttp-equiv=Content-Typecontent="text/html;charset=gb2312"> <title>js拖动滑块控制图片显示大小</title> <style> *{margin:0;padding:0;font-size:12px;} .btn{width:50px;height:15px;cursor:pointer;} #picViewPanel{margin:550050px;width:328px;height:248px;overflow:auto;text-align:center;border:solid1px#cccccc;} #slider{margin:050px;height:15px;width:328px;border:1pxsolid#000000;position:relative;} #sliderLeft{height:13px;width:13px;float:left;border:1pxsolid#cccccc;cursor:pointer;} #sliderBlock{height:13px;width:50px;border:1pxsolid#cccccc;position:absolute;top:0;left:113px;cursor:pointer;background:#cccccc;text-align:center;} #sliderRight{height:13px;width:13px;float:right;border:1pxsolid#cccccc;cursor:pointer;} </style> </head> <body> <divid="picViewPanel"></div> <divid="slider"> <spanid="sliderLeft"><<</span> <spanid="sliderRight">>></span> <spanid="sliderBlock">==</span> </div> </body> <script> //golbal varpv=null; varsd=null; window.onload=function(){ pv=newPicView("/images/m01.jpg"); sd=newSlider( function(p){ document.getElementById("sliderBlock").innerHTML=2*p+"%"; pv.expand(2*p/100); },function(){}); } varPicView=function(url,alt){ this.url=url; this.obj=null; this.alt=alt?alt:""; this.realWidth=null; this.realHeight=null; this.zoom=1; this.init(); } PicView.prototype.init=function(){ var_img=document.createElement("img"); _img.src=this.url; _img.alt=this.alt; _img.style.zoom=this.zoom; document.getElementById("picViewPanel").appendChild(_img); this.obj=_img; this.realWidth=_img.offsetWidth; this.realHeight=_img.offsetHeight; } PicView.prototype.reBind=function(){ this.obj.style.width= this.realWidth*this.zoom+"px"; this.obj.style.height=this.realHeight*this.zoom+"px"; //this.obj.style.zoom=this.zoom; } PicView.prototype.expand=function(n){ this.zoom=n; this.reBind(); } varSlider=function(ing,ed){ this.block=document.getElementById("sliderBlock"); this.percent=0; this.value=0; this.ing=ing; this.ed=ed; this.init(); } Slider.prototype.init=function(){ var_sx=0; var_cx=0; varo=this.block; varme=this; o.onmousedown=function(e){ vare=window.event||e; _sx=o.offsetLeft; _cx=e.clientX-_sx; document.body.onmousemove=move; document.body.onmouseup=up; }; functionmove(e){ vare=window.event||e; varpos_x=e.clientX-_cx; pos_x=pos_x<13?13:pos_x; pos_x=pos_x>248+15-50?248+15-50:pos_x; o.style.left= pos_x+"px"; me.percent=(pos_x-13)/2; me.ing(me.percent); } functionup(){ document.body.onmousemove=function(){}; document.body.onmouseup=function(){}; } } </script> </html>
希望本文所述对大家的javascript程序设计有所帮助。