jquery實現手風琴展開效果
本文實例為大傢分享瞭jquery實現手風琴展開效果的具體代碼,供大傢參考,具體內容如下
手風琴模式實現方式:
dom層代碼:
<div class="accordionWrap"> <div class="wrap"> <div class="title"> <span>內容一</span> <span class="arrow slideTog"></span> </div> <div class="accordionCon"> <div> <p>錦瑟無端五十弦,一弦一柱思華年。</p> <p>莊生曉夢迷蝴蝶,望帝春心托杜鵑。</p> <p>滄海月明珠有淚,藍田日暖玉生煙。</p> <p>此情可待成追憶?隻是當時已惘然。</p> </div> <div class="moreCon" style="display:none"> <p>相見時難別亦難,東風無力百花殘。</p> <p>春蠶到死絲方盡,蠟炬成灰淚始幹。</p> <p>曉鏡但愁雲鬢改,夜吟應覺月光寒。</p> <p>蓬山此去無多路,青鳥殷勤為探看。</p> </div> </div> </div> <div class="wrap"> <div class="title"> <span>內容一</span> <span class="arrow slideTog"></span> </div> <div class="accordionCon"> <div> <p>錦瑟無端五十弦,一弦一柱思華年。</p> <p>莊生曉夢迷蝴蝶,望帝春心托杜鵑。</p> <p>滄海月明珠有淚,藍田日暖玉生煙。</p> <p>此情可待成追憶?隻是當時已惘然。</p> </div> <div class="moreCon" style="display:none"> <p>相見時難別亦難,東風無力百花殘。</p> <p>春蠶到死絲方盡,蠟炬成灰淚始幹。</p> <p>曉鏡但愁雲鬢改,夜吟應覺月光寒。</p> <p>蓬山此去無多路,青鳥殷勤為探看。</p> </div> </div> </div> <div class="wrap"> <div class="title"> <span>內容一</span> <span class="arrow slideTog"></span> </div> <div class="accordionCon"> <div> <p>錦瑟無端五十弦,一弦一柱思華年。</p> <p>莊生曉夢迷蝴蝶,望帝春心托杜鵑。</p> <p>滄海月明珠有淚,藍田日暖玉生煙。</p> <p>此情可待成追憶?隻是當時已惘然。</p> </div> <div class="moreCon" style="display:none"> <p>相見時難別亦難,東風無力百花殘。</p> <p>春蠶到死絲方盡,蠟炬成灰淚始幹。</p> <p>曉鏡但愁雲鬢改,夜吟應覺月光寒。</p> <p>蓬山此去無多路,青鳥殷勤為探看。</p> </div> </div> </div> </div>
css代碼:
.accordionWrap{ width: 218px; background:#1890ff; border-radius: 4px; position:absolute; left: 10px; top: 50px; padding: 10px; box-sizing: border-box; .wrap{ line-height: 18px; .title{ span{ font-size: 12px; font-family: SourceHanSansCN-Regular, SourceHanSansCN; font-weight: 400; color: #fff; } .arrow{ width: 12px; height: 7px; background:url("../img/arrow.svg") no-repeat; background-size: 100% 100%; float:right; margin-top: 5px; transform: rotate(180deg); cursor: pointer; transition: all 0.5s; } .slideTogExchange { width: 12px; height: 7px; background: url('../img/arrow.svg') no-repeat; background-size: 100% 100%; float:right; margin-top: 10px; cursor: pointer; transform: rotate(0deg); } } .accordionCon{ border-top: 1px dashed #45fff8; border-bottom: 1px solid #dddddd; p{ font-size: 12px; font-family: SourceHanSansCN-Regular, SourceHanSansCN; font-weight: 400; color: #fff; } .moreCon{ p{ color: #45FFF8 !important; } } } } }
js操作dom的時候一定要註意層級之間的關系
$(function () { // 點擊箭頭展開隱藏的內容 $(".slideTog").click(function () { var hasClass = $(this).hasClass('slideTogExchange') if (hasClass) { $(this).parent().siblings('.accordionCon').children('.moreCon').slideUp() $(this).removeClass('slideTogExchange') } else { $(this).parent().siblings('.accordionCon').children('.moreCon').slideDown() $(this).addClass('slideTogExchange') } $(this).parents('.wrap').siblings('.wrap').children('.accordionCon').children('.moreCon').slideUp().removeClass('slideTogExchange') $(this).parents('.wrap').siblings('.wrap').find('.slideTog').removeClass('slideTogExchange') }); })
實現效果如下:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。