JS use drag slider to control the size of the picture _javascript tips

Source: Internet
Author: User

The example of this article tells JS to drag the slider to control the size of the image method. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<meta http-equiv=content-type content= "text/html;charset=gb2312" >
<title>js drag slider to control picture display size </title>
<style>
*{margin:0;padding:0;font-size:12px;}
. Btn{width:50px;height:15px;cursor:pointer;}
#picViewPanel {margin:5 0 50px; width:328px; height:248px;overflow:auto;text-align:center;border:solid 1px #cccccc;}
#slider {margin:0 50px;height:15px;width:328px;border:1px solid #000000;p osition:relative;}
#sliderLeft {height:13px; width:13px;float:left;border:1px solid #cccccc; cursor:pointer;}
#sliderBlock {height:13px;width:50px;border:1px solid #cccccc;p osition:absolute;top:0;left:113px;cursor:pointer; Background: #cccccc; text-align:center;}
#sliderRight {height:13px;width:13px;float:right;border:1px solid #cccccc; cursor:pointer;}
</style>
<body>
<div id= "Picviewpanel" ></div>
<div id= "Slider" >
<span id= "Sliderleft" ><<</span>
<span id= "Sliderright" >>></span>
<span id= "Sliderblock" >==</span>
</div>
</body>
<script>
Golbal
var PV = null;
var sd = null;
Window.onload=function () {
PV = new Picview ("/images/m01.jpg");
SD = new Slider (
Function (p) {
document.getElementById ("Sliderblock"). InnerHTML = 2*p + "%";
Pv.expand (2*p/100);
},function () {});
}
var Picview = 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 ();
}
var Slider = 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;
var O=this.block;
var me=this;
O.onmousedown=function (e) {
var e=window.event| | E
_SX = O.offsetleft;
_CX = E.CLIENTX-_SX;
Document.body.onmousemove=move;
Document.body.onmouseup=up;
};
function Move (e) {
var e=window.event| | E
var pos_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);
}
function up () {
Document.body.onmousemove=function () {};
Document.body.onmouseup=function () {};
}
}
</script>

I hope this article will help you with your JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.