JavaScript 入门教程:jquery手风琴特效插件
手风琴效果是项目中使用频率较高的一种效果,原来项目一直都在用easyui的,临近年末,试着自己写了一个css样式。
代码如下:
/* css document */
body {
margin: 0 auto;
padding: 0 auto;
font-size: 9pt;
font-family: 微软雅黑, 宋体, arial, helvetica, verdana, sans-serif;
}
.accordion {
padding-left: 0px;
}
.accordion li {
border-top: 1px solid #000;
list-style-type: none;
}
.titlemenu {
width: 100%;
height: 30px;
background-color: #f2f2f2;
padding: 5px 0px;
text-align: left;
cursor: pointer;
}
.titlemenu img {
position: relative;
left: 20px;
top: 5px;
}
.titlemenu span {
display: inline-block;
position: relative;
left: 40px;
}
.submenu {
text-align: left;
width: 100%;
padding-left: 0px;
}
.submenu li {
list-style-type: none;
width: 100%;
}
.submenu li img {
position: relative;
left: 20px;
top: 5px;
}
.submenu li a {
position: relative;
left: 40px;
top: 5px;
text-decoration: none;
}
.submenu li span {
display: inline-block;
height: 30px;
padding: 5px 0px;
}
.hover {
background-color: #4a5b79;
}
自定义js
代码如下:
(function ($) {
piano = function () {
_menu ='[{"title":"一级目录","img":"images/cog.png","submenu":[{"title":"二级目录","img":"images/monitor_window_3d.png"},{"title":"二级目录","img":"images/monitor_window_3d.png"},{"title":"二级目录","img":"images/monitor_window_3d.png"}]},{"title":"一级目录","img":"images/cog.png","submenu":[{"title":"二级目录","img":"images/monitor_window_3d.png"},{"title":"二级目录","img":"images/monitor_window_3d.png"},{"id":"4","title":"二级目录","img":"images/monitor_window_3d.png"}]}]';
return ep = {
init: function (obj) {
_menu = eval('(' + _menu + ')');
var li ="";
$.each(_menu, function (index, element) {
li += '<li><p class="titlemenu"><img src=' + element.img + ' width="16" height="16" alt=""/><span>' + element.title + '</span></p>';
if(element.submenu!=null)
{
li+='<ul class="submenu">';
$.each(element.submenu, function (ind, ele) {
li += '<li><img src=' + ele.img + ' width="16" height="16" alt=""/><span><a href="#">' + ele.title + '</a></span></li>';
});
li+='</ul>';
}
li+='</li>';
});
obj.append(li);
}
}
}
$.fn.accordion = function (options) {
var pia = new piano();
pia.init($(this));
return this.each(function () {
var accs = $(this).children('li');
accs.each(reset);
accs.click(onclick);
var menu_li = $(".submenu").children("li");
menu_li.each(function (index, element) {
$(this).mousemove(function (e) {
$(this).siblings().removeclass("hover");
$(this).find("a").css("color", "#fff");
$(this).siblings().find("a").css("color", "#000");
$(this).addclass("hover");
});
});
});
}
function onclick() {
$(this).siblings('li').find("ul").each(hide);
$(this).find("ul").slidedown('normal');
return false;
}
function hide() {
$(this).slideup('normal');
}
function reset() {
$(this).find("ul").hide();
}
})(jquery);
html调用方式
代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="jquery-1.8.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="accordion.js"></script>
<script type="text/javascript">
$(function(){
$("#accordion").accordion();
});
</script>
</head>
<body>
<ul id="accordion" class="accordion" style="width:200px;height:500px;">
</ul>
</body>
</html>
显示效果
推荐阅读
-
JavaScript编程开发中jQuery插件开发详细教程
-
JavaScript开发中JQuery插件的基本知识
-
基于JQuery和原生JavaScript实现网页定位导航特效
-
JavaScript编程开发中基于jQuery的视频播放插件开发教程
-
JavaScript开发中jQuery动画与特效详解
-
JavaScript 入门教程:jquery手风琴特效插件
-
使用jQuery快速高效制作网页交互特效——04 第四章JavaScript对象及初识面向对象
-
web前端设计师们常用的jQuery特效插件汇总
-
JavaScript开发中如何使用jQuery jqPlot插件绘制柱状图
-
jQuery/CSS3图片特效插件整理推荐