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 相关文章推荐
jQuery学习笔记之jQuery选择器的使用
Dec 22 Javascript
关于递归运算的顺序测试代码
Nov 30 Javascript
JavaScript高级程序设计(第3版)学习笔记9 js函数(下)
Oct 11 Javascript
JS 去除Array中的null值示例代码
Nov 20 Javascript
javascript设计模式之解释器模式详解
Jun 05 Javascript
借助JavaScript脚本判断浏览器Flash Player信息的方法
Jul 09 Javascript
javascript解析json实例详解
Nov 05 Javascript
Lua表达式和控制结构学习笔记
Dec 15 Javascript
bootstrap模态框嵌套、tabindex属性、去除阴影的示例代码
Oct 17 Javascript
pm2启动ssr失败的解决方法
Jun 29 Javascript
百度小程序之间的页面通信过程详解
Jul 18 Javascript
微信小程序8种数据通信的方式小结
Feb 03 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脚本代码
2011/02/19 PHP
php数据库抽象层 PDO
2011/05/07 PHP
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
Symfony2安装第三方Bundles实例详解
2016/02/04 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
2019/03/29 PHP
laravel通过a标签从视图向控制器实现传值
2019/10/15 PHP
Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
2010/08/27 Javascript
js获取input长度并根据页面宽度设置其大小及居中对齐
2014/08/22 Javascript
使用Sticker.js实现贴纸效果
2015/01/28 Javascript
JS实现固定在右下角可展开收缩DIV层的方法
2015/02/13 Javascript
JavaScript中switch语句的用法详解
2015/06/03 Javascript
关于javascript的一些知识以及循环详解
2016/09/12 Javascript
对vue里函数的调用顺序介绍
2018/03/17 Javascript
详解解决使用axios发送json后台接收不到的问题
2018/06/27 Javascript
AngularJs分页插件使用详解
2018/06/30 Javascript
layui监听工具栏的实例(操作列表按钮)
2019/09/10 Javascript
Python解惑之整数比较详解
2017/04/24 Python
python 将数据保存为excel的xls格式(实例讲解)
2018/05/03 Python
OpenCV python sklearn随机超参数搜索的实现
2020/01/17 Python
SHEIN香港:价格实惠的女性时尚服装
2018/08/14 全球购物
Java TransactionAPI (JTA) 主要包含几部分
2012/12/07 面试题
编写类String的构造函数、析构函数和赋值函数
2012/05/29 面试题
如何在存储过程中使用Loop
2016/01/05 面试题
Oracle快照(snapshot)
2015/03/13 面试题
abstract是什么意思
2012/02/12 面试题
应届大学生求职的自我评价
2013/11/17 职场文书
写演讲稿要注意的六件事
2014/01/14 职场文书
大学军训感言1000字
2014/02/25 职场文书
班委竞选演讲稿
2014/04/28 职场文书
文化产业实施方案
2014/06/07 职场文书
党员违纪检讨书怎么写
2014/11/01 职场文书
实训报告范文大全
2014/11/04 职场文书
2016简历自荐信优秀范文
2016/01/29 职场文书
抖音短视频(douyin)去水印工具的实现代码
2021/03/30 Javascript
python之基数排序的实现
2021/07/26 Python