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

 更新时间:2015年02月27日 10:07:52   作者:代码家园  
这篇文章主要介绍了js用拖动滑块来控制图片大小的方法,实例分析了javascript使用鼠标事件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Three.js快速入门教程

    Three.js快速入门教程

    这篇文章主要为大家详细介绍了Three.js快速入门教程,帮助大家快速入门,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • JS读取cookies信息(记录用户名)

    JS读取cookies信息(记录用户名)

    很多网站、博客把网页生成html静态页面了,以利于搜索引擎的索引排名,减轻服务器负担。静态页面由于其稳定快速更快,给用户及站长带来了方便。但评论后如何记住用户的信息呢
    2012-01-01
  • JS中如何判断传过来的JSON数据中是否存在某字段

    JS中如何判断传过来的JSON数据中是否存在某字段

    这篇文章主要介绍了JS中如何判断传过来的JSON数据中是否存在某字段,需要的朋友可以参考下
    2014-08-08
  • 详解JavaScript设计模式中的享元模式

    详解JavaScript设计模式中的享元模式

    享元模式是一种用于性能优化的模式。享元模式的核心是运用共享技术来有效支持大量细粒度的对象.如果系统中创建了大量类似的对象而导致内存占用过高,本文通过介绍书中文件上传的优化案例来说明享元模式的使用方式和作用,需要的朋友可以参考下
    2023-06-06
  • JavaScript框架设计模式详解

    JavaScript框架设计模式详解

    这篇文章主要介绍了javascript框架设计模式,结合实例形式分析了javascript模板方法模式相关概念、原理、用法及操作注意事项,需要的朋友可以参考下
    2021-09-09
  • 基于JS模仿windows文件按名称排序效果

    基于JS模仿windows文件按名称排序效果

    这篇文章主要介绍了基于JS实现模仿windows文件按名称排序的相关资料,主要是对数字的处理操作,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • 原生Javascript和jQuery做轮播图简单例子

    原生Javascript和jQuery做轮播图简单例子

    这篇文章主要为大家详细介绍了原生Javascript和jQuery做轮播图简单例子,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • JavaScript toDataURL图片转换问题解读

    JavaScript toDataURL图片转换问题解读

    这篇文章主要介绍了JavaScript toDataURL图片转换问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Flexigrid在IE下不显示数据的处理的解决方法

    Flexigrid在IE下不显示数据的处理的解决方法

    Flexigrid在IE下不显示数据的情况,想必大家都有遇到过吧,下面有个不错的解决方法,感兴趣的朋友可以参考下
    2013-10-10
  • JavaScript基于activexobject连接远程数据库SQL Server 2014的方法

    JavaScript基于activexobject连接远程数据库SQL Server 2014的方法

    这篇文章主要介绍了JavaScript基于activexobject连接远程数据库SQL Server 2014的方法,结合实例形式分析了javascript使用activexobject远程连接数据库的相关操作技巧,需要的朋友可以参考下
    2017-07-07

最新评论