DIV CSS 佈局教程網

用jQuery打造自訂風格滑動條:可拖曳的工具欄完整實作
編輯:JQuery特效代碼     

學生線上首頁改版時,我們想加一條工具欄。受限於版面,原本規劃的眾多工具只能塞進一小塊區域,效果如下:

坦白說,用 HTML 原生的控制項也能做出類似效果。但自製的好處是滑桿的樣式完全自己說了算——紅的、藍的、按鈕形狀,通通能改。

目標是這樣的:可見區域一次最多顯示 9 個工具(這裡假設工具總數超過 9 個;就算目前不滿 9 個,未來擴充到 9 個的機會也不小),點擊上下按鈕就能讓工具區塊上下移動。

做完之後,營運同事提了個建議:如果可以直接拖動滑桿來滑動工具欄,使用體驗會更好。聽起來簡單,做起來卻有點棘手。

設計思路

  1. 整個區域切成兩塊:工具區(class="toolBox")與滑桿區(class="slideBar")。
  2. 工具區設為相對定位,內含一個長條容器(class="innerToolBox")放全部工具,一行三個,超出的部分隱藏(這點很關鍵),並相對外層絕對定位。初始 top=0,之後每次滑動只需改這個 top 值。
  3. 右側滑桿區有三個元素:向上按鈕(upBtn)、向下按鈕(downBtn)、滑桿外框(barBox)。外框裡再放一個滑桿(innerBar),同樣絕對定位,改 top 值即可滑動,而且必須和左側工具欄同步。滑桿高度不是固定的,得由工具的行數決定,這部分就交給 JavaScript 運算。

HTML

<div id="smallTools" class="clearfix">
<div class="toolBox">
<div class="innerToolBox">
<ul>
<li class="tool1">
<a href="#" target="_blank">校車表</a>
</li>
<li class="tool2">
<a href="https://online.cumt.edu.cn/dzhbl/" target="_blank">電子海報</a>
</li>
<li class="tool3">
<a href="https://lib.cumt.edu.cn/" target="_blank">圖書館</a>
</li>
</ul>
<ul>
<li class="tool4">
<a href="https://stu.cumt.edu.cn/xgxt" target="_blank">學生工作系統</a>
</li>
<li class="tool5">
<a href="https://jwchu.cumt.edu.cn/" target="_blank">教務系統</a>
</li>
<li class="tool6">
<a href="https://service.js.vnet.cn/" target="_blank">網卡查詢</a>
</li>
</ul>
<ul>
<li class="tool7">
<a href="https://219.219.35.66/index.php" target="_blank">自主學習</a>
</li>
<li class="tool8">
<a href="#" target="_blank">新生入口</a>
</li>
<li class="tool9">
<a href="#" target="_blank">焦點視頻</a>
</li>
</ul>
<ul>
<li class="tool7">
<a href="https://219.219.35.66/index.php" target="_blank">自主學習</a>
</li>
<li class="tool8">
<a href="#" target="_blank">新生入口</a>
</li>
<li class="tool9">
<a href="#" target="_blank">焦點視頻</a>
</li>
</ul>
<ul>
<li class="tool7">
<a href="https://219.219.35.66/index.php" target="_blank">自主學習</a>
</li>
<li class="tool8">
<a href="#" target="_blank">新生入口</a>
</li>
<li class="tool9">
<a href="#" target="_blank">焦點視頻</a>
</li>
</ul>
</div>
</div>
<div class="slideBar">
<a href="#" class="upBtn"> </a>
<div class="barBox">
<div class="innerBar"></div>
</div>
<a href="#" class="downBtn"> </a>
</div>
<div class="clear"></div>
</div>

CSS

/***大框***/
#smallTools
{
background:url(../images10/smallBarBg.gif) repeat-x left bottom;
position:relative;
height:227px;
overflow:hidden;
}
/***左右兩邊的布局***/
#smallTools .toolBox /***左邊的工具框區域***/
{
height:207px;
margin-top:10px;
float:left;
width:237px;
margin-left:10px;
overflow:hidden;
position:relative;
}
#smallTools .slideBar /***右邊的滑動條區域***/
{
height:227px;
float:right;
width:11px;
}
/***左框內元素的具體樣式***/
.innerToolBox
{
position:absolute;
left:0px;
top:0px;
}
#smallTools ul
{
height:69px;
}
#smallTools ul li
{
float:left;
width:79px;
height:69px;
text-align:center;
}
#smallTools ul li a
{
display:block;
width:79px;
height:22px;
padding-top:47px;
color:#000;
}
/***以下是給各工具項設置背景***/
#smallTools ul li.tool1
{
background:url(../images/tool1.gif) no-repeat center 7px
}
#smallTools ul li.tool2
{
background:url(../images/tool2.gif) no-repeat center 7px
}
#smallTools ul li.tool3
{
background:url(../images/tool3.gif) no-repeat center 7px
}
#smallTools ul li.tool4
{
background:url(../images/tool4.gif) no-repeat center 7px
}
#smallTools ul li.tool5
{
background:url(../images/tool5.gif) no-repeat center 7px
}
#smallTools ul li.tool6
{
background:url(../images/tool6.gif) no-repeat center 7px
}
#smallTools ul li.tool7
{
background:url(../images/tool7.gif) no-repeat center 7px
}
#smallTools ul li.tool8
{
background:url(../images/tool8.gif) no-repeat center 7px
}
#smallTools ul li.tool9
{
background:url(../images/tool9.gif) no-repeat center 7px
}
/***右邊滑動條框的具體樣式***/
.slideBar .upBtn /***向上滑動按鈕***/
{
display:block;
line-height:0px;
width:9px;
height:7px;
background:url(../images/up_btn.png) no-repeat left top;
margin-top:2px;
padding:0px;
}
.slideBar .upBtn:hover
{
border:1px solid #000000;
}
.slideBar .downBtn /***向下滑動按鈕***/
{
display:block;
line-height:0px;
width:9px;
height:7px;
background:url(../images/down_btn.png) no-repeat left top;
margin:0px;
padding:0px;
}
.slideBar .downBtn:hover
{
border:1px solid #000000;
}
#smallTools .barBox
{
height:196px;
margin:4px 0px;
width:11px;
position:relative;
}
.innerBar
{
position:absolute;
width:10px;
background:#a4a4a4;
cursor:s-resize;
top:0px;
}

JavaScript:把滑桿包成一個物件

為了方便維護,這裡用 jQuery 把行為封裝成一個物件,成員大致如下:

$( function()
{
/***對象方法,進行一些成員變量的賦值
變量:elem:要被上下移動的工具條區域名(元素名、id和class的組合)
perHight:每一格一次被移動的長度
slideN:工具欄中工具的行數
showN:可見的工具的行數(這裡是3)
speed:一次移動所花的毫秒數
index:可見區域的第一行的索引
barElem:滑動條名(元素名、id和class的組合)
***/
function toolBar(elem,perHeight,slideN,showN,speed,index,barElem)
{……}
toolBar.prototype=
{
/***滑動條的高度的設置
高度計算公式:滑動條可設置的最大高度*可見的工具的行數/工具欄中工具的總行數
***/
initBar:function()
{……},
/***工具條滑動的函數,to是要被滑動到的索引,這函數在點上下按鈕或移動滑動條的時候會被觸發***/
slide:function(to)
{……},
/***滑動條滑動的函數,to是要被滑動到的索引,這函數在點上下按鈕的時候會被觸發,和slide函數同步實現***/
barSlide:function(to)
{……},
/***本函數為上下按鈕添加點擊事件
upelem:向上按鈕的名字(元素名、id和class的組合)
downelem:向下按鈕的名字(元素名、id和class的組合)
***/
clickTab:function(upelem,downelem)
{……},
/***拖動滑動條的函數,拖動後,工具框也進行相應移動。
elem:需要被移動的元素名(元素名、id和class的組合)
handle:使相應元素被移動所需要拖動的把柄元素名(元素名、id和class的組合)
uplev:被拖動元素最高點(這裡是0)
downlev:被拖動元素的最低點(這裡是196)
***/
drag:function(elem,handle,uplev,downlev)
{……}
}
/***這裡進行對象的實例化,與相關函數的調用***/
})

完整程式碼:

$(function()
{
function toolBar(elem,perHeight,slideN,showN,speed,index,barElem)
{
this.elem=$(elem);
this.perHeight=perHeight;
this.slideN=slideN;
this.showN=showN;
this.speed=speed;
this.index=index;
this.barElem=barElem;
}
toolBar.prototype=
{
initBar:function()
{
var tl=$(this.barElem).parent().height();
$(this.barElem).css({'height':tl*this.showN/this.slideN});
},
slide:function(to)
{
this.elem.animate({'top':-(to*this.perHeight)},this.speed)
},
barSlide:function(to)
{
var tl=$(this.barElem).parent().height();
$(this.barElem).animate({'top':tl*to/this.slideN},this.speed)
},
clickTab:function(upelem,downelem)
{
var _this=this;
$(upelem).bind('click',function()
{
if(_this.index>0)
{
_this.index--;
_this.slide(_this.index);
_this.barSlide(_this.index);
}
return false;
});
$(downelem).bind('click',function()
{
if(_this.index<_this.slideN-_this.showN)
{
_this.index++;
_this.slide(_this.index);
_this.barSlide(_this.index);
}
return false;
});
},
drag:function(elem,handle,uplev,downlev)
{
var _this=this;
var tl=$(this.barElem).parent().height();
var C=$(elem);
var dy, moveout;
var T = $(handle);
T.bind("selectstart", function()
{
return false;
});
T.mousedown(function(e)
{
//dx = e.clientX - parseInt(C.css("left"));
e=e?e:window.event;
dy = e.clientY - parseInt(C.css("top"));
C.mousemove(move).mouseout(out).css('opacity', 0.8);
T.mouseup(up);
});
function move(e)
{
e=e?e:window.event;
moveout = false;
if((e.clientY - dy)>=uplev&&(e.clientY - dy)<=(downlev-C.height()))
C.css({"top": e.clientY - dy});
}
function out(e)
{
e=e?e:window.event;
moveout = true;
setTimeout(function(){checkout(e);}, 100);
}
function up(e)
{
e=e?e:window.event;
var adaptTop;
var presTop=parseInt(C.css("top"));
C.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1);
T.unbind("mouseup", up);
//alert(parseInt(_this.slideN));
if(((presTop/(tl/_this.slideN))-parseInt(presTop/(tl/_this.slideN)))>=0.5)
{
_this.index=parseInt(presTop/(tl/_this.slideN))+1;
}
else
{
_this.index=parseInt(presTop/(tl/_this.slideN));
}
adaptTop=_this.index*(tl/_this.slideN);
_this.slide(_this.index);
C.css({"top":adaptTop});
}
function checkout(e)
{
e=e?e:window.event;
moveout && up(e);
}
}
}
var slength=$("#smallTools .innerToolBox ul").length;
var stBox=new toolBar("#smallTools .innerToolBox",69,slength,3,700,0,"#smallTools .innerBar");
stBox.initBar();
stBox.clickTab("#smallTools .upBtn","#smallTools .downBtn");
stBox.drag("#smallTools .innerBar","#smallTools .innerBar",0,196);
})

幾個實作上的關鍵點值得複習:滑桿高度用「最大高度 × 可見行數 ÷ 總行數」推算,滑桿位置則用「最大高度 × 目前索引 ÷ 總行數」,兩者共用同一組索引,因此按鈕點擊與拖曳可以自然同步;拖曳結束後再依目前位置四捨五入回最近的整格,畫面才不會停在半路。水平有限,若有疏漏之處,歡迎指正。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved