Home > Web Front-end > JS Tutorial > js method to control image size by dragging slider_javascript skills

js method to control image size by dragging slider_javascript skills

WBOY
Release: 2016-05-16 16:12:42
Original
1501 people have browsed it

The example in this article describes how to use js to drag the slider to control the size of the image. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:




js drag the slider to control the image display size





<<
>>
==


<script><br> //golbal<br> var pv = null;<br> var sd = null;<br> window.onload=function(){<br> pv = new PicView("/images/m01.jpg");<br> sd = new Slider(<br> function(p){<br> document.getElementById("sliderBlock").innerHTML = 2*p "%";<br> pv.expand(2*p/100);<br> },function(){});<br> }<br> var PicView = function(url,alt){<br> this.url=url;<br> this.obj=null;<br> this.alt=alt?alt:"";<br> this.realWidth=null;<br> this.realHeight=null;<br> this.zoom=1;<br> this.init();<br> }<br> PicView.prototype.init=function(){<br> var _img=document.createElement("img");<br> _img.src = this.url;<br> _img.alt = this.alt;<br> _img.style.zoom = this.zoom;<br> document.getElementById("picViewPanel").appendChild(_img);<br> this.obj=_img;<br> this.realWidth=_img.offsetWidth;<br> this.realHeight=_img.offsetHeight;<br> }<br> PicView.prototype.reBind=function(){<br> this.obj.style.width = this.realWidth*this.zoom "px";<br> this.obj.style.height = this.realHeight*this.zoom "px";<br> //this.obj.style.zoom = this.zoom;<br> }<br> PicView.prototype.expand=function(n){<br> this.zoom=n;<br> this.reBind();<br> }<br> var Slider = function(ing,ed){<br> this.block=document.getElementById("sliderBlock");<br> this.percent = 0;<br> this.value = 0;<br> this.ing = ing;<br> this.ed = ed;<br> this.init();<br> }<br> Slider.prototype.init=function(){<br> var _sx=0;<br> var _cx=0;<br> var o=this.block;<br> var me=this;<br> o.onmousedown=function(e){<br> var e=window.event||e;<br> _sx = o.offsetLeft;<br> _cx = e.clientX-_sx;<br> document.body.onmousemove=move;<br> document.body.onmouseup=up;<br> };<br> function move(e){<br> var e=window.event||e;<br> var pos_x = e.clientX - _cx;<br> pos_x=pos_x<13?13:pos_x;<br /> pos_x=pos_x>248 15-50?248 15-50:pos_x;<br> o.style.left = pos_x "px";<br> me.percent=(pos_x-13)/2;<br> me.ing(me.percent);<br> }<br> function up(){<br> document.body.onmousemove=function(){};<br> document.body.onmouseup=function(){};<br> }<br> }<br> </script>

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template