[js插件开发教程]定制一个手风琴插件(accordion)

本文带来一个垂直方向的手风琴插件开发,可以定制的功能如下:

contentClass : 'panel',    //面板样式
navClass : 'nav', //导航样式
activeClass : 'active', //导航激活样式
triggerElements : '*', //触发元素
activeIndex : 0, //默认选中的元素
evType : 'click', //默认触发的事件
animate : true, //是否支持动画渐变
multiple : false //是否支持多个面板同时展开

调用方法:

$(".accordion").accordion( { animate : true, multiple : true } );   //支持多个面板同时展开, 支持动画效果

效果预览:

[js插件开发教程]定制一个手风琴插件(accordion)

$(".accordion").accordion( { animate : false, multiple : true } );
这种效果就是跟选项卡一样

[js插件开发教程]定制一个手风琴插件(accordion)

$(".accordion").accordion( { 'animate' : true } );
不支持多个面板,永远只有一个面板被打开

效果预览:

[js插件开发教程]定制一个手风琴插件(accordion)

完整的手风琴插件代码:

html部分:

 <!DOCTYPE html>
<html>
<head lang="en">
<!--作者:ghostwu(吴华)-->
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="./css/accordion.css"/>
<script src="./js/accordion.js"></script>
<script>
window.onload = function(){
$(".accordion").accordion( { 'animate' : true } );
}
</script>
</head>
<body>
<div class="main">
<div class="accordion">
<div class="panel">面板1</div>
<div class="nav">导航1</div>
<div class="panel">面板2</div>
<div class="nav">导航2</div>
<div class="panel">面板3</div>
<div class="nav">导航3</div>
<div class="panel">面板4</div>
<div class="nav">导航4</div>
<div class="panel">面板5</div>
<div class="nav">导航5</div>
</div>
</div>
</body>
</html>

css样式部分:

 * {
margin:;
padding:;
}
body {
font-size: 14px;
font-family: Tahoma, Verdana,"Microsoft Yahei";
}
a{
text-decoration: none;
color:#000;
}
ul,li{
list-style-type: none;
}
img {
border:none;
}
.main {
width:960px;
margin:20px auto;
} .accordion{
width:400px;
margin: 0 auto 10px;
} .accordion .nav {
border-bottom: 1px dashed #0064cd;
}
.accordion .panel{
height:100px;
background:#ccc;
}
.accordion .nav{
height:30px;
background:#999;
}
.accordion .active{
background:#ff0;
}

js插件部分:

 /**
* Created by ghostwu(吴华).
*/
(function(){
var G = function( selectors, context ){
return new G.fn.init( selectors, context );
}
G.fn = G.prototype = {
length : 0,
constructor : G,
size : function(){
return this.length;
},
init : function( selector, context ){
this.length = 0;
context = context || document;
if ( selector.indexOf( '#' ) == 0 ){
this[0] = document.getElementById( selector.substring( 1 ) );
this.length = 1;
}else {
var aNode = context.querySelectorAll( selector );
for( var i = 0, len = aNode.length; i < len; i++ ) {
this[i] = aNode[i];
}
this.length = len;
}
this.selector = selector;
this.context = context;
return this;
}
} G.fn.init.prototype = G.fn;
G.extend = G.fn.extend = function () {
var i = 1,
len = arguments.length,
dst = arguments[0],
j;
if (dst.length === undefined) {
dst.length = 0;
}
if (i == len) {
dst = this;
i--;
}
for (; i < len; i++) {
for (j in arguments[i]) {
dst[j] = arguments[i][j];
dst.length++;
}
}
return dst;
}; function css(obj, attr, value) {
if (arguments.length == 3) {
obj.style[attr] = value;
} else {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
} function animate(obj, attr, fn) {
clearInterval(obj.timer);
var cur = 0;
var target = 0;
var speed = 0;
//var start = new Date().getTime();//起始时间
obj.timer = setInterval(function () {
var bFlag = true;
for (var key in attr) {
if (key == 'opacity') {
cur = css(obj, 'opacity') * 100;
} else {
cur = parseInt(css(obj, key));
}
target = attr[key];
speed = ( target - cur ) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
console.log( cur, target );
if (cur != target) {
bFlag = false;
if (key == 'opacity') {
obj.style.opacity = ( cur + speed ) / 100;
obj.style.filter = "alpha(opacity:" + ( cur + speed ) + ")";
} else {
obj.style[key] = cur + speed + "px";
}
}
}
if (bFlag) {
//var end = new Date().getTime();//结束时间
//console.log( '总计:', ( end - start ) );
clearInterval(obj.timer);
fn && fn.call(obj);
}
}, 30 );
} G.fn.accordion = function( options ){
options = options || {};
var defaults = {
contentClass : 'panel',
navClass : 'nav',
activeClass : 'active',
triggerElements : '*',
activeIndex : 0,
evType : 'click',
animate : true,
multiple : false
}; var opt = G.extend( {}, defaults, options ); var aNavEle = this[0].querySelectorAll( "." + opt.navClass ),
aPanel = this[0].querySelectorAll( "." + opt.contentClass ); var _api = {}; var panelHeight = parseInt( css( aPanel[opt.activeIndex], 'height' ) );
_api.setIndex = function( curIndex, effect ){
if ( opt.multiple ) {
if ( effect ) {
if ( parseInt( css( aPanel[curIndex], 'height' ) ) == 0 ) {
aPanel[curIndex].style.display = 'block';
animate( aPanel[curIndex], { 'height' : panelHeight } );
}else {
animate( aPanel[curIndex], { 'height' : 0 }, function(){
this.style.display = 'none';
} );
}
}else {
if ( css( aPanel[curIndex], 'display' ) == 'block' ){
aPanel[curIndex].style.display = 'none';
}else {
aPanel[curIndex].style.display = 'block';
}
if ( aNavEle[curIndex].classList.contains( 'active' ) ) {
aNavEle[curIndex].classList.remove( 'active' );
}else {
aNavEle[curIndex].classList.add( 'active' );
}
}
}else {
if ( effect ) {
for ( var i = 0; i < aPanel.length; i++ ) {
if( i != curIndex ) {
if ( aPanel[i].style.display == 'block' ){
animate( aPanel[i], { 'height' : 0 }, function(){
this.style.display = 'none';
} );
}else{
aPanel[i].style.display = 'none';
}
}else {
aPanel[curIndex].style.display = 'block';
animate( aPanel[curIndex], { 'height' : panelHeight } );
}
}
}else {
for ( var i = 0 ; i < aPanel.length; i++ ){
aPanel[i].style.display = 'none';
aNavEle[i].classList.remove( 'active' );
}
aPanel[curIndex].style.display = 'block';
aNavEle[curIndex].classList.add( 'active' );
}
}
}; _api.setIndex( opt.activeIndex, opt.animate ); for( var i = 0 ; i < aNavEle.length; i++ ) {
aNavEle[i].index = i;
aNavEle[i].addEventListener( opt.evType, function(){
_api.setIndex( this.index, opt.animate );
}, false );
}
return this;
} var $ = function( selectors, context ){
return G( selectors, context );
}
window.$ = $;
})();
上一篇:Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask


下一篇:关于PowerShell中的命令的别名