jquery写的缩放边栏

写在前面:
这个缩放栏长得很丑,支持你自己用CSS进行美化
[img]http://dl2.iteye.com/upload/attachment/0089/1849/ce3f1324-b2c7-35d2-9f25-5c76e4ca80d9.jpg[/img]
调用方法:

<link rel="stylesheet" type="text/css" href="zoombar.css" rel="stylesheet">
<body>
<div style="margin:200px;width:200px;height:200px;" id="text"><h1>正在开发,尽请期待</h1></div>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="zoombar.js"></script>
<script type="text/javascript">
$(function(){
$('body').Zoombar({container:$("#text"),
zoomInFunction:function(){
//点击放大的自定义事件
},
zoomOutFunction:function(){
//点击缩小的自定义事件
},
zoomFunction:function(evt,scale){
//在杆上拖拽的自定义事件 evt事件,scale代表放大或者缩小倍数的参数
}});
})
</script>

重点说:
zoomFunction中scale参数的处理方法如下所示:

if(scale/5-4>0){
if(scale/5-4>1){
zoomFunc(Math.pow(0.8,parseInt(scale/5-4)));//0.8一个单位的缩小量,parseInt(scale/5-4)缩小倍数
}else{
zoomFunc(1);
}
}else{
if(4-scale/5>1){
zoomFunc(Math.pow(1.2,parseInt(4-scale/5)));//1.2一个单位的放大量,parseInt(4-scale/5)放大倍数
}else{
zoomFunc(1);
}
}

如何做到元素在杆上的拖拽?
在父元素(中间的杆)的mousemove事件中判断鼠标左键是否是按下状态,然后写按下状态的拖拽事件

$("#gun").mousemove(
function(evt){
if(isPress){
$("#mblock").offset({top:evt.clientY}).addClass("zoombar-mousedown");
$("#gun").addClass("zoombar-mousedown");
zoomThePlace(evt)
}
});

捕获鼠标左键按下的代码,由于div堆叠的情况,和浏览器各种兼容的情况,最终以以下方式获取鼠标左键按下的判断:

var isPress = false;//处理IE9和火狐对鼠标按下状态监听的矫情的兼容
$("body,#mblock,#gun,.zoombar-infor,.zoombar").mouseup(function(evt){
isPress = false;
}).mousedown(function(evt){
isPress = true;
});

注意事项:
缩放边栏要适应浏览器窗口大小改变的情况

var me = this;
$container.resize(function(){
me.location($container);
});

缺点总结:
chrome下拖拽时鼠标不显示手型
通过鼠标按键的抬起事件对isPress的捕获判断不是很准确
鼠标在拖拽滑块时容易造成选中一大片变蓝色的情况,如下图中所示:
[img]http://dl2.iteye.com/upload/attachment/0089/1861/c8530905-d41e-32f8-a61a-0086bb0420ea.jpg[/img]
最后:
希望大伙儿 给我美化下,并且解决以上提到的bug们
源码:
zoombar.js


$.Zoombar = $.Zoombar||{};
$.extend($.Zoombar,{
init:function($container){
var me = this;
$container.resize(function(){
me.location($container);
});
return $("<div class='zoombar' id='zoombar'/>").appendTo("body");

},
initFunction:function(options){
this.zoomInFunction = options.zoomInFunction;
this.zoomOutFunction = options.zoomOutFunction;
this.zoomFunction = options.zoomFunction;
},
location:function($container){
var xc = $("#mblock").position().top-$("#zoombar").position().top==0?43:$("#mblock").position().top-$("#zoombar").position().top;
$("#zoombar").offset({top: $container.position().top+50,left: $container.position().left+10});
$("#mblock").offset({top: $("#zoombar").position().top+xc,left: $("#zoombar").position().left});
$(".zoombar-infor").offset({top: $container.position().top+45,left: $container.position().left+25});

},
addContent:function($container){
var me = this;
$("<div class='zoombar-inButton'>+</div>").appendTo("#zoombar").click(function(){
if($("#mblock").offset().top>$("#gun").offset().top){
me.zoomInFunction();
$("#mblock").offset({top:$("#mblock").offset().top-5});
}
});
$("<div class='zoombar-gun' id='gun'></div>").appendTo("#zoombar");
$("<div class='zoombar-outButton'>-</div>").appendTo("#zoombar").click(function(){
if($("#mblock").offset().top<($("#gun").offset().top+$("#gun").height())){
me.zoomOutFunction();
$("#mblock").offset({top:$("#mblock").offset().top+5});
}
});
$("<div id='mblock' class='zoombar-mblock'></div>").appendTo("body");
var isPress = false;//处理IE9和火狐对鼠标按下状态监听的矫情的兼容
$("body,#mblock,#gun,.zoombar-infor,.zoombar").mouseup(function(evt){
isPress = false;
}).mousedown(function(evt){
isPress = true;
});
$("#gun").mousedown(function(evt){
$("#mblock").offset({top:evt.clientY});
$("#gun").addClass("zoombar-mousedown");
zoomThePlace(evt);
}).mousemove(function(evt){
if(isPress){
$("#mblock").offset({top:evt.clientY}).addClass("zoombar-mousedown");
$("#gun").addClass("zoombar-mousedown");
zoomThePlace(evt)
}
}).mouseup(function(evt){
isPress = false;
});
$("<div class='zoombar-infor'>-200%<br><br>-100%<br><br>-1%</div>").appendTo("body");
function zoomThePlace(evt){
$("#cellEditToolbar").remove();
if($("#mblock").offset().top<($("#gun").offset().top+$("#gun").height())&&$("#mblock").offset().top>$("#gun").offset().top){
var scale = $("#mblock").offset().top-$("#gun").offset().top;
me.zoomFunction(evt,scale);
}
}
}

});
$.extend($.fn,{
Zoombar : function(options){
$.Zoombar.initFunction(options);
this.$elt = $.Zoombar.init(options.container);
$.Zoombar.addContent(options.container);
$.Zoombar.location(options.container)
return this;
}
});

zoombar.css

.zoombar {
width:9px;
position:absolute!important;
}
.zoombar-inButton {
background-color:rgb(223, 223, 223);
cursor:pointer;
text-align:center;
}
.zoombar-outButton {
background-color:rgb(223, 223, 223);
cursor:pointer;
text-align:center;
}
.zoombar-gun {
background-color:rgb(223, 223, 223);
height:50px;
width:5px;
margin-left:2px;
margin-right:2px;
cursor:pointer;
}
.zoombar-mousedown {
cursor:pointer;
}
.zoombar-mblock {
position:absolute!important;
width:10px;
height:5px;
background-color:gray;
cursor:pointer;
}
.zoombar-infor {
position:absolute!important;
width:50px;
padding-left:3px;
font-size:10px;
line-height: 20px;
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@好久不见的分割线@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
得不到的永远在躁动,被偏爱的却有恃无恐
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值