简体   繁体   English

如何更改自定义手风琴的图标?

[英]How to change icon for a custom accordion?

We created a custom accordion library that will toggle the .content element based on what is clicked similar to jQuery accordion.我们创建了一个自定义手风琴库,它将根据单击的内容切换.content元素,类似于 jQuery 手风琴。 Next to the .header element we are displaying a fontawesome plus icon ..header元素旁边,我们显示了一个字体加号图标 I am running into issues where the content will hide and show, but the icon will not switch.我遇到了内容将隐藏和显示的问题,但图标不会切换。

How can i get the content element and icons to switch?如何让内容元素和图标切换?

Current issue:目前的问题:

问题

Desired output:所需的 output:

想要的

 $(function() { $('.header').on('click', function() { var hdr = $(this); var grandParents = hdr.parent().parent(); grandParents.find('.item.active').removeClass('active'); grandParents.find('.content').stop().slideUp().removeClass('active'); hdr.closest('.item').find('.content').stop().slideToggle(); hdr.parent().toggleClass('active'); }); });
 .content { display: none; }.item.active >.content { display: block; }.item.active >.header { color: white; background-color: black; }.header { padding: 16px; margin: 8px; background-color: lightgrey; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.min.js"></script> <div class="accordion"> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>First link</span> </h3> <div class='content'> SOME CONTENT HERE </div> </div> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>second link</span> </h3> <div class='content'> SOME CONTENT HERE </div> </div> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>third link</span> </h3> <div class='content'> Some more content </div> </div> </div>

I have updated your js code, please check.我已经更新了你的js代码,请检查。 The general concept of my update is, in every click, the icon is removed and replaced with a new but different one (from plus to minus and vice versa).我的更新的一般概念是,在每次点击中,图标都会被删除并替换为一个新的但不同的图标(从加号到减号,反之亦然)。

The problem on this is that the use of fontawesome icons.这方面的问题是使用了令人敬畏的图标。 But anyways, take a look on my code.但无论如何,看看我的代码。

Here's the full working code.这是完整的工作代码。 The only update I have is the js part.我唯一的更新是 js 部分。

 $(function() { $('.header').on('click', function() { var hdr = $(this); hdr.siblings('.content').slideToggle(); hdr.parent().toggleClass('active'); hdr.parent().siblings().find('.content').slideUp(); hdr.parent().siblings().removeClass('active'); if(hdr.parent().hasClass('active')) { hdr.parent().find('svg').remove(); hdr.parent().siblings().find('svg').remove(); hdr.prepend('<i class="fas fa-minus"></i>'); hdr.parent().siblings().find('.header').prepend('<i class="fas fa-plus"></i>'); } else { hdr.parent().find('svg').remove(); hdr.prepend('<i class="fas fa-plus"></i>'); } }); });
 .content { display: none; }.item.active >.content { display: block; }.item.active >.header { color: white; background-color: black; }.header { padding: 16px; margin: 8px; background-color: lightgrey; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.min.js"></script> <div class="accordion"> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>First link</span> </h3> <div class='content'> SOME CONTENT HERE </div> </div> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>second link</span> </h3> <div class='content'> SOME CONTENT HERE </div> </div> <div class="item"> <h3 class="header"> <i class="fas fa-plus"></i> <span>third link</span> </h3> <div class='content'> Some more content </div> </div> </div>

We had same kind of requirement, where we had to replace the + icon of each accordion item with dynamic icon specific to the accordion content, like 1st Accordion - a.png 2nd accordion - b.png and so on.我们有同样的要求,我们必须用特定于手风琴内容的动态图标替换每个手风琴项目的 + 图标,例如 1st Accordion - a.png 2nd Accordion - b.png 等等。

I did it by adding an image to the Accordion Item page content template and assign it to the cloned accordion rendering.我通过将图像添加到 Accordion Item 页面内容模板并将其分配给克隆的手风琴渲染来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM