js用拖動(dòng)滑塊來控制圖片大小的方法
更新時(shí)間:2015年02月27日 10:07:52 作者:代碼家園
這篇文章主要介紹了js用拖動(dòng)滑塊來控制圖片大小的方法,實(shí)例分析了javascript使用鼠標(biāo)事件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了js用拖動(dòng)滑塊來控制圖片大小的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=gb2312">
<title>js拖動(dòng)滑塊控制圖片顯示大小</title>
<style>
*{margin:0;padding:0;font-size:12px;}
.btn{width:50px;height:15px;cursor:pointer;}
#picViewPanel{margin:5 50 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;position:relative;}
#sliderLeft{height:13px; width:13px;float:left;border:1px solid #cccccc;cursor:pointer;}
#sliderBlock{height:13px;width:50px;border:1px solid #cccccc;position: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>
</head>
<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>
</html>
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=gb2312">
<title>js拖動(dòng)滑塊控制圖片顯示大小</title>
<style>
*{margin:0;padding:0;font-size:12px;}
.btn{width:50px;height:15px;cursor:pointer;}
#picViewPanel{margin:5 50 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;position:relative;}
#sliderLeft{height:13px; width:13px;float:left;border:1px solid #cccccc;cursor:pointer;}
#sliderBlock{height:13px;width:50px;border:1px solid #cccccc;position: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>
</head>
<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>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Vue 實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(只有css+js沒有后臺(tái)驗(yàn)證步驟)
- 基于JavaScript實(shí)現(xiàn)拖動(dòng)滑塊效果
- 基于JS組件實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(代碼分享)
- Javascript實(shí)現(xiàn)滑塊滑動(dòng)改變值的實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)兼容PC端和移動(dòng)端滑塊拖動(dòng)選擇數(shù)字效果
- JS響應(yīng)鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)兩個(gè)滑塊區(qū)間拖動(dòng)效果
- js實(shí)現(xiàn)滑動(dòng)滑塊驗(yàn)證登錄
- 原生js實(shí)現(xiàn)可兼容PC和移動(dòng)端的拖動(dòng)滑塊功能詳解【測試可用】
- Javascript 鼠標(biāo)移動(dòng)上去 滑塊跟隨效果代碼分享
- 如何基于JS實(shí)現(xiàn)Ajax并發(fā)請(qǐng)求的控制詳解
相關(guān)文章
JS中如何判斷傳過來的JSON數(shù)據(jù)中是否存在某字段
這篇文章主要介紹了JS中如何判斷傳過來的JSON數(shù)據(jù)中是否存在某字段,需要的朋友可以參考下2014-08-08
詳解JavaScript設(shè)計(jì)模式中的享元模式
享元模式是一種用于性能優(yōu)化的模式。享元模式的核心是運(yùn)用共享技術(shù)來有效支持大量細(xì)粒度的對(duì)象.如果系統(tǒng)中創(chuàng)建了大量類似的對(duì)象而導(dǎo)致內(nèi)存占用過高,本文通過介紹書中文件上傳的優(yōu)化案例來說明享元模式的使用方式和作用,需要的朋友可以參考下2023-06-06
JavaScript toDataURL圖片轉(zhuǎn)換問題解讀
這篇文章主要介紹了JavaScript toDataURL圖片轉(zhuǎn)換問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Flexigrid在IE下不顯示數(shù)據(jù)的處理的解決方法
Flexigrid在IE下不顯示數(shù)據(jù)的情況,想必大家都有遇到過吧,下面有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下2013-10-10
JavaScript基于activexobject連接遠(yuǎn)程數(shù)據(jù)庫SQL Server 2014的方法
這篇文章主要介紹了JavaScript基于activexobject連接遠(yuǎn)程數(shù)據(jù)庫SQL Server 2014的方法,結(jié)合實(shí)例形式分析了javascript使用activexobject遠(yuǎn)程連接數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07

