热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> AJAX编程>>正文内容

用jquery实现自定义风格的滑动条

发布时间:2011年04月26日点击数: 佚名

前些天我们学生在线首页改版,要做一个工具栏,由于版面的限制,原先策划的很多工具只好安排在一个小区域里面,具体效果如下:

当然,这样的效果,用html自带的控件也可以实现。不过自定义的话就可以自己设置滑动条的样式啦,比如说设为红色、蓝色等,按钮形状也可以自己做啦。

需要实现的效果是,这些工具一次最多在可见区域显示9个(这里假设工具项总数多于9个,不满9个的话,将来也很有可能扩展到9个),点击上下的按钮即可将可见区域内的工具区域上下移动。

但是这样做好后,运营人员给我提意见了:要是移动滑动条就可以实现工具栏上下滑动,那用户体验会更好,不过说的简单,做起来就有点麻烦了。

首先从头开始讲解吧。我的构思如下:

  1. 整个区域分为两个,一个是工具区域(class=” toolBox”),一个是滑动条区域(class="slideBar")。
  2. 工具区域(class=” toolBox”)设为相对定位,内部有一个大长条(class="innerToolBox"),存放所有的工具,一行三个工具,超出部分不可见(这很 关键哦),并且相对外部工具区域(class=” toolBox”)是绝对定位的,刚开始时,top=0,这样每次滑动只需改变其top值即可。
  3. 右边的滑动条区域(class="slideBar")有三个东西:向上按钮(class="upBtn")、向下按钮 (class="downBtn")、滑动条框(class="barBox")。滑动条框(class="barBox")仅仅是存放滑动条的“盒 子”,里面有一个滑动条(class=” innerBar”)。和工具栏类似,滑动条(class=” innerBar”)相对滑动条框(class="barBox")是绝对定位的,只需改变滑动条(class=” innerBar”)的top值即可实现滑动。并且是和左边的工具条是同步滑动的。那么滑动条的高度是固定的吗,当然不是,它的高度得由左边工具的行数决 定。这就需要由js来实现了(这个后面会提到)。

html代码如下:

  1. <div id="smallTools" class="clearfix"> 
  2. <div class="toolBox"> 
  3. <div class="innerToolBox"> 
  4. <ul> 
  5. <li class="tool1"> 
  6. <a href="#" target="_blank">校车表</a> 
  7. </li> 
  8. <li class="tool2"> 
  9. <a href="http://online.cumt.edu.cn/dzhbl/" target="_blank">电子海报</a> 
  10.  </li> 
  11.  <li class="tool3"> 
  12.  <a href="http://lib.cumt.edu.cn/" target="_blank">图书馆</a> 
  13.  </li> 
  14.  </ul> 
  15.  <ul> 
  16.  <li class="tool4"> 
  17.  <a href="http://stu.cumt.edu.cn/xgxt" target="_blank">学生工作系统</a> 
  18.  </li> 
  19.  <li class="tool5"> 
  20.  <a href="http://jwchu.cumt.edu.cn/" target="_blank">教务系统</a> 
  21.  </li> 
  22.  <li class="tool6"> 
  23.  <a href="http://service.js.vnet.cn/" target="_blank">网卡查询</a> 
  24.  </li> 
  25.  </ul> 
  26.  <ul> 
  27.  <li class="tool7"> 
  28.  <a href="http://219.219.35.66/index.php" target="_blank">自主学习</a> 
  29.  </li> 
  30.  <li class="tool8"> 
  31.  <a href="#" target="_blank">新生入口</a> 
  32.  </li> 
  33.  <li class="tool9"> 
  34.  <a href="#" target="_blank">焦点视频</a> 
  35.  </li> 
  36.  </ul> 
  37.  <ul> 
  38.  <li class="tool7"> 
  39.  <a href="http://219.219.35.66/index.php" target="_blank">自主学习</a> 
  40.  </li> 
  41.  <li class="tool8"> 
  42.  <a href="#" target="_blank">新生入口</a> 
  43.  </li> 
  44.  <li class="tool9"> 
  45.  <a href="#" target="_blank">焦点视频</a> 
  46.  </li> 
  47.  </ul> 
  48.  <ul> 
  49.  <li class="tool7"> 
  50.  <a href="http://219.219.35.66/index.php" target="_blank">自主学习</a> 
  51.  </li> 
  52.  <li class="tool8"> 
  53.  <a href="#" target="_blank">新生入口</a> 
  54.  </li> 
  55.  <li class="tool9"> 
  56.  <a href="#" target="_blank">焦点视频</a> 
  57.  </li> 
  58.  </ul> 
  59.  </div> 
  60.  </div> 
  61.  <div class="slideBar"> 
  62.  <a href="#" class="upBtn">&nbsp;</a> 
  63.  <div class="barBox"> 
  64.  <div class="innerBar"></div> 
  65.  </div> 
  66.  <a href="#" class="downBtn">&nbsp;</a> 
  67.  </div> 
  68.  <div class="clear"></div> 
  69.   </div> 

css代码如下:

  1. /***大框***/ 
  2. #smallTools 
  3.  { 
  4. background:url(../images10/smallBarBg.gif) repeat-x left bottom
  5. position:relative
  6. height:227px
  7. overflow:hidden
  8.  
  9.  /***左右两边的布局***/ 
  10. #smallTools .toolBox /***左边的工具框区域***/ 
  11.  { 
  12. height:207px
  13. margin-top:10px
  14. float:left
  15. width:237px
  16. margin-left:10px
  17. overflow:hidden
  18. position:relative
  19.  
  20.  } 
  21. #smallTools .slideBar /***右边的滑动条区域***/ 
  22.  { 
  23. height:227px
  24. float:right
  25. width:11px
  26.  } 
  27.  
  28. /***左框内元素的具体样式***/ 
  29. .innerToolBox 
  30. position:absolute
  31. left:0px
  32. top:0px
  33. #smallTools ul 
  34. height:69px
  35. #smallTools ul li 
  36. float:left
  37. width:79px
  38. height:69px
  39. text-align:center
  40. #smallTools ul li a 
  41. display:block
  42. width:79px
  43. height:22px
  44. padding-top:47px
  45. color:#000
  46. /***以下是给各工具项设置背景***/ 
  47. #smallTools ul li.tool1 
  48. background:url(../images/tool1.gif) no-repeat center 7px 
  49. #smallTools ul li.tool2 
  50. background:url(../images/tool2.gif) no-repeat center 7px 
  51. #smallTools ul li.tool3 
  52. background:url(../images/tool3.gif) no-repeat center 7px 
  53. #smallTools ul li.tool4 
  54. background:url(../images/tool4.gif) no-repeat center 7px 
  55. #smallTools ul li.tool5 
  56. background:url(../images/tool5.gif) no-repeat center 7px 
  57. #smallTools ul li.tool6 
  58. background:url(../images/tool6.gif) no-repeat center 7px 
  59. #smallTools ul li.tool7 
  60. background:url(../images/tool7.gif) no-repeat center 7px 
  61. #smallTools ul li.tool8 
  62. background:url(../images/tool8.gif) no-repeat center 7px 
  63. #smallTools ul li.tool9 
  64. background:url(../images/tool9.gif) no-repeat center 7px 
  65.  
  66. /***右边滑动条框的具体样式***/ 
  67. .slideBar .upBtn /***向上滑动按钮***/ 
  68. display:block
  69. line-height:0px
  70. width:9px
  71. height:7px
  72.  background:url(../images/up_btn.png) no-repeat left top
  73.  margin-top:2px
  74.  padding:0px
  75.  } 
  76.  .slideBar .upBtn:hover 
  77.  { 
  78.  border:1px solid #000000
  79.  } 
  80.  .slideBar .downBtn /***向下滑动按钮***/ 
  81.  { 
  82.  display:block
  83.  line-height:0px
  84.  width:9px
  85.  height:7px
  86.  background:url(../images/down_btn.png) no-repeat left top
  87.  margin:0px
  88.  padding:0px
  89.  } 
  90.  .slideBar .downBtn:hover 
  91.  { 
  92.  border:1px solid #000000
  93.  } 
  94.  #smallTools .barBox 
  95.  { 
  96.  height:196px
  97.  margin:4px 0px
  98.  width:11px
  99.  position:relative
  100.  } 
  101.  
  102.  .innerBar 
  103.  { 
  104.  position:absolute
  105.  width:10px
  106.  background:#a4a4a4
  107.  cursor:s-resize
  108.  top:0px
  109.  } 

接下来就是给它添加脚本代码了。为了方便,在这里用的是jQuery库。

我决定为它建立一个对象,大致成员变量如下:

  1. $( function() 
  2. /***对象方法,进行一些成员变量的赋值 
  3. 变量:elem:要被上下移动的工具条区域名(元素名、id和class的组合) 
  4. perHight:每一格一次被移动的长度 
  5. slideN:工具栏中工具的行数 
  6. showN:可见的工具的行数(这里是3) 
  7. speed:一次移动所花的毫秒数 
  8. index:可见区域的第一行的索引 
  9.  barElem:滑动条名(元素名、id和class的组合) 
  10.  ***/ 
  11.  function toolBar(elem,perHeight,slideN,showN,speed,index,barElem) 
  12.  {……} 
  13.  toolBar.prototype= 
  14.  { 
  15.  /***滑动条的高度的设置 
  16.  高度计算公式:滑动条可设置的最大高度*可见的工具的行数/工具栏中工具的总行数 
  17.  ***/ 
  18.  initBar:function() 
  19.  {……}, 
  20.  /***工具条滑动的函数,to是要被滑动到的索引,这函数在点上下按钮或移动滑动条的时候会被触发***/ 
  21.  slide:function(to) 
  22.  {……}, 
  23.  /***滑动条滑动的函数,to是要被滑动到的索引,这函数在点上下按钮的时候会被触发,和slide函数同步实现***/ 
  24.  barSlide:function(to) 
  25.  {……}, 
  26.  /***本函数为上下按钮添加点击事件 
  27.  upelem:向上按钮的名字(元素名、id和class的组合) 
  28.  downelem:向下按钮的名字(元素名、id和class的组合) 
  29.  ***/ 
  30.  clickTab:function(upelem,downelem) 
  31.  {……}, 
  32.  /***拖动滑动条的函数,拖动后,工具框也进行相应移动。 
  33.  elem:需要被移动的元素名(元素名、id和class的组合) 
  34.  handle:使相应元素被移动所需要拖动的把柄元素名(元素名、id和class的组合) 
  35.  uplev:被拖动元素最高点(这里是0) 
  36.  downlev:被拖动元素的最低点(这里是196) 
  37.  ***/ 
  38.  drag:function(elem,handle,uplev,downlev) 
  39.  {……} 
  40.  } 
  41.  
  42.  /***这里进行对象的实例化,与相关函数的调用***/ 
  43.  }) 

完整的js代码如下:

  1. $(function() 
  2. function toolBar(elem,perHeight,slideN,showN,speed,index,barElem) 
  3. this.elem=$(elem); 
  4. this.perHeight=perHeight; 
  5. this.slideN=slideN; 
  6. this.showN=showN; 
  7. this.speed=speed; 
  8. this.index=index; 
  9. this.barElem=barElem; 
  10. toolBar.prototype= 
  11. initBar:function() 
  12. var tl=$(this.barElem).parent().height(); 
  13. $(this.barElem).css({'height':tl*this.showN/this.slideN}); 
  14. }, 
  15. slide:function(to) 
  16. this.elem.animate({'top':-(to*this.perHeight)},this.speed) 
  17. }, 
  18. barSlide:function(to) 
  19. var tl=$(this.barElem).parent().height(); 
  20. $(this.barElem).animate({'top':tl*to/this.slideN},this.speed) 
  21. }, 
  22. clickTab:function(upelem,downelem) 
  23. var _this=this
  24. $(upelem).bind('click',function() 
  25. if(_this.index>0) 
  26. _this.index--; 
  27. _this.slide(_this.index); 
  28. _this.barSlide(_this.index); 
  29. return false
  30. }); 
  31. $(downelem).bind('click',function() 
  32. if(_this.index<_this.slideN-_this.showN) 
  33. _this.index++; 
  34. _this.slide(_this.index); 
  35. _this.barSlide(_this.index); 
  36. return false
  37. }); 
  38. }, 
  39. drag:function(elem,handle,uplev,downlev) 
  40. var _this=this
  41. var tl=$(this.barElem).parent().height(); 
  42. var C=$(elem); 
  43. var dy, moveout; 
  44. var T = $(handle); 
  45. T.bind("selectstart"function() 
  46. return false
  47. }); 
  48. T.mousedown(function(e) 
  49. //dx = e.clientX - parseInt(C.css("left")); 
  50. e=e?e:window.event; 
  51. dy = e.clientY - parseInt(C.css("top")); 
  52. C.mousemove(move).mouseout(out).css('opacity', 0.8); 
  53. T.mouseup(up); 
  54. }); 
  55. function move(e) 
  56. e=e?e:window.event; 
  57. moveout = false
  58. if((e.clientY - dy)>=uplev&&(e.clientY - dy)<=(downlev-C.height())) 
  59. C.css({"top": e.clientY - dy}); 
  60. function out(e) 
  61. e=e?e:window.event; 
  62. moveout = true
  63. setTimeout(function(){checkout(e);}, 100); 
  64. function up(e) 
  65. e=e?e:window.event; 
  66. var adaptTop; 
  67. var presTop=parseInt(C.css("top")); 
  68. C.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1); 
  69. T.unbind("mouseup", up); 
  70. //alert(parseInt(_this.slideN)); 
  71. if(((presTop/(tl/_this.slideN))-parseInt(presTop/(tl/_this.slideN)))>=0.5) 
  72. _this.index=parseInt(presTop/(tl/_this.slideN))+1; 
  73. else 
  74. _this.index=parseInt(presTop/(tl/_this.slideN)); 
  75.  } 
  76.  adaptTop=_this.index*(tl/_this.slideN); 
  77.  _this.slide(_this.index); 
  78.  C.css({"top":adaptTop}); 
  79.  } 
  80.  function checkout(e) 
  81.  { 
  82.  e=e?e:window.event; 
  83.  moveout && up(e); 
  84.  } 
  85.  } 
  86.  } 
  87.  
  88.  
  89.  
  90.  var slength=$("#smallTools .innerToolBox ul").length; 
  91.  var stBox=new toolBar("#smallTools .innerToolBox",69,slength,3,700,0,"#smallTools .innerBar"); 
  92.  stBox.initBar(); 
  93.  stBox.clickTab("#smallTools .upBtn","#smallTools .downBtn"); 
  94.  stBox.drag("#smallTools .innerBar","#smallTools .innerBar",0,196); 
  95.  }) 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作