js用拖动滑块来控制图片大小的方法


Posted in Javascript onFebruary 27, 2015

本文实例讲述了js用拖动滑块来控制图片大小的方法。分享给大家供大家参考。具体实现方法如下:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<meta http-equiv=Content-Type content="text/html;charset=gb2312">

<title>js拖动滑块控制图片显示大小</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>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
JavaScript中“+=”的应用
Feb 02 Javascript
JavaScript null和undefined区别分析
Oct 14 Javascript
最新28个很棒的jQuery 教程
May 28 Javascript
jQuery最佳实践完整篇
Aug 20 Javascript
js arguments,jcallee caller用法总结
Nov 30 Javascript
探讨jQuery的ajax使用场景(c#)
Dec 03 Javascript
Jquery选择器中使用变量实现动态选择例子
Jul 25 Javascript
jquery Easyui快速开发总结
Aug 20 Javascript
Javascript编程之继承实例汇总
Nov 28 Javascript
使用jQuery判断Div是否在可视区域的方法 判断div是否可见
Feb 17 Javascript
vue+elementUI(el-upload)图片压缩,默认同比例压缩操作
Aug 10 Javascript
如何通过JS实现日历简单算法
Oct 14 Javascript
javascript中局部变量和全局变量的区别详解
Feb 27 #Javascript
对比分析AngularJS中的$http.post与jQuery.post的区别
Feb 27 #Javascript
JavaScript中Function详解
Feb 27 #Javascript
JS实现图片产生波纹一样flash效果的方法
Feb 27 #Javascript
js实现鼠标触发图片抖动效果的方法
Feb 27 #Javascript
javascript实现当前页导航激活的方法
Feb 27 #Javascript
jquery 根据name名获取元素的value值
Feb 27 #Javascript
You might like
用PHP和ACCESS写聊天室(九)
2006/10/09 PHP
解决php中Cannot send session cache limiter 的问题的方法
2007/04/27 PHP
PHP使用Face++接口开发微信公众平台人脸识别系统的方法
2015/04/17 PHP
php文件上传类的分享
2017/07/06 PHP
PHP获取二叉树镜像的方法
2018/01/17 PHP
初学prototype,发个JS接受URL参数的代码
2006/09/25 Javascript
javascript与webservice的通信实现代码
2010/12/25 Javascript
JS下拉缓冲菜单示例代码
2013/08/30 Javascript
轻松创建nodejs服务器(10):处理上传图片
2014/12/18 NodeJs
javascript的日期对象、数组对象、二维数组使用说明
2014/12/22 Javascript
Bootstrap被封装的弹层
2016/07/20 Javascript
react.js 翻页插件实例代码
2017/01/19 Javascript
HTML5开发Kinect体感游戏的实例应用
2017/09/18 Javascript
JS中的JSON对象的定义和取值实现代码
2018/05/09 Javascript
用WebStorm进行Angularjs 2开发(环境篇:Windows 10,Angular-cli方式)
2018/12/05 Javascript
使用Vue开发自己的Chrome扩展程序过程详解
2019/06/21 Javascript
Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义
2019/08/20 Javascript
用vite搭建vue3应用的实现方法
2021/02/22 Vue.js
Python线程创建和终止实例代码
2018/01/20 Python
python3利用tcp实现文件夹远程传输
2018/07/28 Python
python opencv判断图像是否为空的实例
2019/01/26 Python
Python3.5多进程原理与用法实例分析
2019/04/05 Python
Python获取命令实时输出-原样彩色输出并返回输出结果的示例
2019/07/11 Python
Python3 itchat实现微信定时发送群消息的实例代码
2019/07/12 Python
详解如何减少python内存的消耗
2019/08/09 Python
Python count函数使用方法实例解析
2020/03/23 Python
Python实现Word文档转换Markdown的示例
2020/12/22 Python
美国网上书店:Barnes & Noble
2018/08/15 全球购物
prAna官网:瑜伽、旅行和冒险服装
2019/03/10 全球购物
研究生自荐信
2013/10/09 职场文书
请假条标准格式规范
2014/04/10 职场文书
负责人任命书范本
2014/06/04 职场文书
2015中秋节晚会开场白
2015/07/30 职场文书
Golang 语言控制并发 Goroutine的方法
2021/06/30 Golang
关于Vue中的options选项
2022/03/22 Vue.js
选购到合适的激光打印机
2022/04/21 数码科技