jquery - 使用 i 标签而不是下拉按钮的背景属性来切换效果
问题描述
我对不需要 JS 的下拉菜单感兴趣。我在网上找到了一些资源并成功了。但是,我想通过 i 标签(或任何更好的建议)更改我用作下拉“按钮”的图像,以便我可以改用 font-awesome/line-awesome 类(获取 .svg 需要付费许可我所知)。
我不知道如何处理输入:选中 ~ 标签以更改为关闭图标,如果选择
<label for="dropOptsPost"><i class="fa fa-ellipsis-v"></i></label>
.dropdown label {
display: block;
cursor: pointer;
background: url(img/closed.png) no-repeat left center;
padding: 3rem 0 3rem 8rem;
}
/* Toggle effect */
input:checked ~ label {
background: url(img/open.png) no-repeat left center;
}
input:checked ~ .dropdown-content {
max-height: 100%;
}
<div class="dropdown drop-container">
<input type="checkbox" id="dropdownMenu">
<label for="dropdownMenu"></label>
<div class="dropdown-content">
<a href="#" title="">Option 1</a>
<a href="#" title="">Option 2</a>
<a href="#" title="">Option 3</a>
</div>
</div>
更新 请参阅下面的类似示例中的我的解决方案
解决方案
$(document).ready(function() {
$('.toggle').click(function() {
$(this).find('.toggle-icon').toggleClass('fa-plus-circle fa-times active');
$('.toggle').toggleClass('active');
$('.toggle-content').toggleClass('active');
});
});
.hidden {
overflow: hidden;
}
.container {
width: 100%;
height: 1000px;
margin: 0 auto;
background-color: #eee;
}
.wrapper {
background-color: pink;
position: relative;
display: flex;
align-items: center;
}
.toggle {
display: block;
width: 20px;
height: 20px;
float: left;
cursor: pointer;
color: white;
text-align: center;
background-color: green;
margin: 10px;
}
.toggle-icon {
color: white;
font-size: 10px;
text-align: center;
}
.toggle.active {
background-color: red;
}
.toggle-content {
display: none;
}
.toggle-content.active {
display: block;
background-color: white;
border: 1px solid black;
position: absolute;
top: 40px;
left: 0;
}
<div class="container">
<div class="wrapper">
<div class="toggle"><a href="javascript:void"><i class="toggle-icon fas fa-cogs"></i></a></div>
<div class="toggle-content">
<p>Some content</p>
</div>
</div>
</div>
推荐阅读
- python - 在熊猫数据框中使用查询
- java - 将 SecureRandom 字节转换为字符串,而不会在 java 中出现编码问题
- sql-server - 升级 IBM Datastage 和 SQL Server 后出现死锁问题
- sql - 保存数据的外键表与父表中的“内联”数据?表现?
- android - 我在初始化适配器内部的 exoplayer2 和在 viewpager2 上实现适配器时遇到错误
- go - 调整切片大小 - 是否检查 `len(slice) > 1` 或 `newCap > 2*len(slice)`
- javascript - 如何使用反应打字稿在 Electron 中获取节点类型?
- javascript - Mocha & Chai - 测试异步函数在传递无效参数时是否抛出 TypeError?
- r - 在 Shiny 中,如何在给定复选框输入的情况下为绘图记录转换变量(x 和或 y)?
- linux-kernel - os 杀死了 apache ignite 进程并且没有留下任何理由