手風琴效果是專案中使用頻率較高的一種效果,原來專案一直都在用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 += '
';
if(element.submenu!=null)
{
li ='';
}
li ='';
});
obj.append(li);
}
}
}
$.fn.accordion = 函數(選項){
var pia = 新鋼琴();
pia.init($(this));
返回 this.each(function () {
var accs = $(this).children('li');
accs.each(重置);
accs.click(onClick);
var menu_li = $(".submenu").children("li");
menu_li.each(函數(索引,元素){
$(this).mousemove(function (e) {
$(this).siblings().removeClass("hover");
$(this).find("a").css("color", "#fff");
$(this).siblings().find("a").css("color", "#000");
$(this).addClass("懸停");
});
});
});
}
函數 onClick() {
$(this).siblings('li').find("ul").each(hide);
$(this).find("ul").slideDown('normal');
回傳錯誤;
}
函數隱藏() {
$(this).slideUp('正常');
}
函數重置() {
$(this).find("ul").hide();
}
})(jQuery);
html呼叫方式
顯示效果
以上就是本文的全部內容了,希望大家能夠喜歡