---
title: "jquery写的缩放边栏"
author: "lynnlysh"
date: "2013-09-11T14:03:21Z"
tags: ["UI", "XHTML", "jQuery"]
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2013-09-11T14:03:21Z"
id: 1940753
url: "https://lynnlysh.iteye.com/blog/1940753"
---

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

# 正在开发,尽请期待

\[/code] 重点说： zoomFunction中scale参数的处理方法如下所示： \[code="js"] if(scale/5-4&gt;0){ if(scale/5-4&gt;1){ zoomFunc(Math.pow(0.8,parseInt(scale/5-4)));//0.8一个单位的缩小量，parseInt(scale/5-4)缩小倍数 }else{ zoomFunc(1); } }else{ if(4-scale/5&gt;1){ zoomFunc(Math.pow(1.2,parseInt(4-scale/5)));//1.2一个单位的放大量，parseInt(4-scale/5)放大倍数 }else{ zoomFunc(1); } } \[/code] 如何做到元素在杆上的拖拽？ 在父元素(中间的杆)的mousemove事件中判断鼠标左键是否是按下状态，然后写按下状态的拖拽事件 \[code="js"] $("#gun").mousemove( function(evt){ if(isPress){ $("#mblock").offset({top:evt.clientY}).addClass("zoombar-mousedown"); $("#gun").addClass("zoombar-mousedown"); zoomThePlace(evt) } }); \[/code] 捕获鼠标左键按下的代码，由于div堆叠的情况，和浏览器各种兼容的情况，最终以以下方式获取鼠标左键按下的判断： \[code="js"] var isPress = false;//处理IE9和火狐对鼠标按下状态监听的矫情的兼容 $("body,#mblock,#gun,.zoombar-infor,.zoombar").mouseup(function(evt){ isPress = false; }).mousedown(function(evt){ isPress = true; }); \[/code] 注意事项： 缩放边栏要适应浏览器窗口大小改变的情况 \[code="js"] var me = this; $container.resize(function(){ me.location($container); }); \[/code] 缺点总结： chrome下拖拽时鼠标不显示手型 通过鼠标按键的抬起事件对isPress的捕获判断不是很准确 鼠标在拖拽滑块时容易造成选中一大片变蓝色的情况，如下图中所示： \[img]http://dl2.iteye.com/upload/attachment/0089/1861/c8530905-d41e-32f8-a61a-0086bb0420ea.jpg\[/img] 最后： 希望大伙儿 给我美化下，并且解决以上提到的bug们 源码： zoombar.js \[code="js"] $.Zoombar = $.Zoombar||{}; $.extend($.Zoombar,{ init:function($container){ var me = this; $container.resize(function(){ me.location($container); }); return $("

").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; $("

\+

").appendTo("#zoombar").click(function(){ if($("#mblock").offset().top&gt;$("#gun").offset().top){ me.zoomInFunction(); $("#mblock").offset({top:$("#mblock").offset().top-5}); } }); $("

").appendTo("#zoombar"); $("

\-

").appendTo("#zoombar").click(function(){ if($("#mblock").offset().top&lt;($("#gun").offset().top+$("#gun").height())){ me.zoomOutFunction(); $("#mblock").offset({top:$("#mblock").offset().top+5}); } }); $("

").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; }); $("

-200%

-100%

-1%

").appendTo("body"); function zoomThePlace(evt){ $("#cellEditToolbar").remove(); if($("#mblock").offset().top&lt;($("#gun").offset().top+$("#gun").height())&&$("#mblock").offset().top&gt;$("#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; } }); \[/code] zoombar.css \[code="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; } \[/code] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@好久不见的分割线@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 得不到的永远在躁动，被偏爱的却有恃无恐