用CSS和Javascript创建一个选项卡系统
Create a tab system in CSS and Javascript
我正在开发一个选项卡系统:
<script>
$(function () {
var links = $('.sidebar-links > div');
links.on('click', function () {
links.removeClass('selected');
$(this).addClass('selected');
});
});
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
/* The main content */
.main-content {
font-family: Arial, Helvetica, sans-serif;
max-width: 600px;
padding-top: 40px;
margin: 0 0 40px 260px;
}
.tabcontent {
display: none;
}
/* The left-collapsing sidebar */
.sidebar-left-collapse {
font-family: Arial, Helvetica, sans-serif;
position: fixed;
top: 0;
left: 0;
background-color: #292c2f;
width: 180px;
height: 100%;
padding: 20px 0;
}
.sidebar-left-collapse > a {
display: block;
text-decoration: none;
font-family: Cookie, cursive;
width: 122px;
height: 122px;
margin: 0 auto;
text-align: center;
color: #ffffff;
font-size: 44px;
font-weight: normal;
line-height: 2.6;
border-radius: 50%;
background-color: #181a1b;
}
.sidebar-left-collapse .sidebar-links {
margin: 30px auto;
}
.sidebar-links div > a {
display: block;
text-decoration: none;
margin: 0 auto 5px auto;
padding: 10px 0 10px 5px;
background-color: #35393e;
text-align: left;
color: #b3bcc5;
font-size: 12px;
font-weight: bold;
line-height: 2;
border-left-width: 2px;
border-left-style: solid;
}
.sidebar-links div.selected > a{
background-color: #ffffff;
color: #565d63;
line-height: 2.3;
margin: 0;
}
.sidebar-links div > a i.fa {
position: relative;
font-size: 20px;
top: 3px;
width: 40px;
text-align: center;
}
.sidebar-links div ul.sub-links {
max-height: 0;
overflow: hidden;
list-style: none;
padding: 0 0 0 30px;
color: #b3bcc5;
font-size: 12px;
font-weight: bold;
line-height: 24px;
margin: 0;
transition: 0.4s;
}
.sidebar-links div.selected ul.sub-links {
max-height: 150px;
padding: 12px 0 12px 30px;
}
.sidebar-links div .sub-links a {
text-decoration: none;
color: #b3bcc5;
display: block;
text-align: left;
}
/* Link Colors */
.sidebar-links div.link-blue > a {
border-color: #487db2;
}
.sidebar-links div.link-blue > a i.fa {
color: #487db2;
}
.sidebar-links div.link-red > a {
border-color: #da4545;
}
.sidebar-links div.link-red > a i.fa {
color: #da4545;
}
.sidebar-links div.link-yellow > a {
border-color: #d0d237;
}
.sidebar-links div.link-yellow > a i.fa {
color: #d0d237;
}
.sidebar-links div.link-green > a {
border-color: #86be2e;
}
.sidebar-links div.link-green > a i.fa {
color: #86be2e;
}
/* Making the sidebar responsive */
@media (max-width: 900px) {
.main-content{
max-width: none;
padding: 70px 20px;
margin: 0 0 40px;
}
.sidebar-left-collapse {
width: auto;
height: auto;
position: static;
padding: 20px 0 0;
}
.sidebar-left-collapse .sidebar-links {
text-align: center;
margin: 20px auto 0;
}
.sidebar-links div {
display: inline-block;
width: 100px;
}
.sidebar-links div > a {
text-align: center;
margin: 0;
padding: 10px 0;
border-left: none;
border-top-width: 2px;
border-top-style: solid;
}
.sidebar-links div > a i.fa {
display: block;
margin: 0 auto 5px;
}
.sidebar-links div ul.sub-links {
display: none;
}
.sidebar-links div.selected .sub-links {
display: block;
position: absolute;
text-align: center;
width: auto;
left: 0;
right: 0;
}
.sidebar-links div.selected .sub-links li {
display: inline-block;
}
.sidebar-links div.selected .sub-links a {
display: inline-block;
margin-right: 20px;
font-size: 13px;
color: #748290;
}
}
/* Smartphone version */
@media (max-width: 450px) {
.main-content{
padding: 90px 20px;
}
.sidebar-left-collapse {
padding: 20px 0;
}
.sidebar-left-collapse .sidebar-links {
text-align: center;
margin: 20px auto 0;
position: relative;
}
.sidebar-links div {
display: block;
width: 240px;
margin: 0 auto 5px;
}
.sidebar-links div > a {
text-align: left;
padding: 10px 25px;
vertical-align: middle;
border-top: none;
border-left-width: 2px;
border-left-style: solid;
}
.sidebar-links div > a i.fa {
display: inline-block;
font-size: 20px;
width: 20px;
margin: 0 20px 0 0;
}
.sidebar-links div.selected .sub-links {
bottom: -90px;
}
}
/* Removing margins and paddings from the body, so that
the sidebar takes the full height of the page */
body {
margin: 0;
padding: 0;
}
<aside class="sidebar-left-collapse">
<a href="#" class="company-logo">Logo</a>
<div class="sidebar-links">
<div class="link-blue selected">
<a href="#">
<i class="fa fa-picture-o"></i>Photography
</a>
<ul class="sub-links">
<li><a href="#" id="portraits" class="tablinks" onclick="openCity(event, 'portraits')">Portraits</a></li>
<li><a href="#" id="landscape" class="tablinks" onclick="openCity(event, 'landscape')">Landscape</a></li>
<li><a href="#" id="studioshots" class="tablinks" onclick="openCity(event, 'studioshots')">Studio shots</a></li>
<li><a href="#" id="macros" class="tablinks" onclick="openCity(event, 'macros')">Macros</a></li>
</ul>
</div>
</div>
</aside>
<div class="main-content">
<div id="portraits" class="tabcontent"> Portraits ...</div>
<div id="landscape" class="tabcontent"> Landscape ...</div>
<div id="studioshots" class="tabcontent"> Studioshots ...</div>
<div id="macros" class="tabcontent"> Macros ...</div>
</div>
也可在JsFiddle 上获得
我想做的是在侧边栏中显示每个可用子链接的内容。对于我所做的,当我点击一个子链接时,什么都不会显示。我认为应用于类.tabcontent
的CSS属性display: none;
不会更改为display: block;
请帮我弄清楚,我的代码
如果您没有使用ie8或更早版本,这是一个简单的解决方案。正如Diego所说,你不能在一个页面中重复id值。
<script>
$(function () {
var links = $('.sidebar-links > div');
links.on('click', function () {
links.removeClass('selected');
$(this).addClass('selected');
});
});
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
/* The main content */
.main-content {
font-family: Arial, Helvetica, sans-serif;
max-width: 600px;
padding-top: 40px;
margin: 0 0 40px 260px;
}
.tabcontent {
display: none;
}
.tabcontent:target
{
display:block;
}
/* The left-collapsing sidebar */
.sidebar-left-collapse {
font-family: Arial, Helvetica, sans-serif;
position: fixed;
top: 0;
left: 0;
background-color: #292c2f;
width: 180px;
height: 100%;
padding: 20px 0;
}
.sidebar-left-collapse > a {
display: block;
text-decoration: none;
font-family: Cookie, cursive;
width: 122px;
height: 122px;
margin: 0 auto;
text-align: center;
color: #ffffff;
font-size: 44px;
font-weight: normal;
line-height: 2.6;
border-radius: 50%;
background-color: #181a1b;
}
.sidebar-left-collapse .sidebar-links {
margin: 30px auto;
}
.sidebar-links div > a {
display: block;
text-decoration: none;
margin: 0 auto 5px auto;
padding: 10px 0 10px 5px;
background-color: #35393e;
text-align: left;
color: #b3bcc5;
font-size: 12px;
font-weight: bold;
line-height: 2;
border-left-width: 2px;
border-left-style: solid;
}
.sidebar-links div.selected > a{
background-color: #ffffff;
color: #565d63;
line-height: 2.3;
margin: 0;
}
.sidebar-links div > a i.fa {
position: relative;
font-size: 20px;
top: 3px;
width: 40px;
text-align: center;
}
.sidebar-links div ul.sub-links {
max-height: 0;
overflow: hidden;
list-style: none;
padding: 0 0 0 30px;
color: #b3bcc5;
font-size: 12px;
font-weight: bold;
line-height: 24px;
margin: 0;
transition: 0.4s;
}
.sidebar-links div.selected ul.sub-links {
max-height: 150px;
padding: 12px 0 12px 30px;
}
.sidebar-links div .sub-links a {
text-decoration: none;
color: #b3bcc5;
display: block;
text-align: left;
}
/* Link Colors */
.sidebar-links div.link-blue > a {
border-color: #487db2;
}
.sidebar-links div.link-blue > a i.fa {
color: #487db2;
}
.sidebar-links div.link-red > a {
border-color: #da4545;
}
.sidebar-links div.link-red > a i.fa {
color: #da4545;
}
.sidebar-links div.link-yellow > a {
border-color: #d0d237;
}
.sidebar-links div.link-yellow > a i.fa {
color: #d0d237;
}
.sidebar-links div.link-green > a {
border-color: #86be2e;
}
.sidebar-links div.link-green > a i.fa {
color: #86be2e;
}
/* Making the sidebar responsive */
@media (max-width: 900px) {
.main-content{
max-width: none;
padding: 70px 20px;
margin: 0 0 40px;
}
.sidebar-left-collapse {
width: auto;
height: auto;
position: static;
padding: 20px 0 0;
}
.sidebar-left-collapse .sidebar-links {
text-align: center;
margin: 20px auto 0;
}
.sidebar-links div {
display: inline-block;
width: 100px;
}
.sidebar-links div > a {
text-align: center;
margin: 0;
padding: 10px 0;
border-left: none;
border-top-width: 2px;
border-top-style: solid;
}
.sidebar-links div > a i.fa {
display: block;
margin: 0 auto 5px;
}
.sidebar-links div ul.sub-links {
display: none;
}
.sidebar-links div.selected .sub-links {
display: block;
position: absolute;
text-align: center;
width: auto;
left: 0;
right: 0;
}
.sidebar-links div.selected .sub-links li {
display: inline-block;
}
.sidebar-links div.selected .sub-links a {
display: inline-block;
margin-right: 20px;
font-size: 13px;
color: #748290;
}
}
/* Smartphone version */
@media (max-width: 450px) {
.main-content{
padding: 90px 20px;
}
.sidebar-left-collapse {
padding: 20px 0;
}
.sidebar-left-collapse .sidebar-links {
text-align: center;
margin: 20px auto 0;
position: relative;
}
.sidebar-links div {
display: block;
width: 240px;
margin: 0 auto 5px;
}
.sidebar-links div > a {
text-align: left;
padding: 10px 25px;
vertical-align: middle;
border-top: none;
border-left-width: 2px;
border-left-style: solid;
}
.sidebar-links div > a i.fa {
display: inline-block;
font-size: 20px;
width: 20px;
margin: 0 20px 0 0;
}
.sidebar-links div.selected .sub-links {
bottom: -90px;
}
}
/* Removing margins and paddings from the body, so that
the sidebar takes the full height of the page */
body {
margin: 0;
padding: 0;
}
<aside class="sidebar-left-collapse">
<a href="#" class="company-logo">Logo</a>
<div class="sidebar-links">
<div class="link-blue selected">
<a href="#">
<i class="fa fa-picture-o"></i>Photography
</a>
<ul class="sub-links">
<li><a href="#portraits" class="tablinks">Portraits</a></li>
<li><a href="#landscape" class="tablinks">Landscape</a></li>
<li><a href="#studioshots" class="tablinks">Studio shots</a></li>
<li><a href="#macros" class="tablinks">Macros</a></li>
</ul>
</div>
</div>
</aside>
<div class="main-content">
<div id="portraits" class="tabcontent"> Portraits ...</div>
<div id="landscape" class="tabcontent"> Landscape ...</div>
<div id="studioshots" class="tabcontent"> Studioshots ...</div>
<div id="macros" class="tabcontent"> Macros ...</div>
</div>
<script> window.location = "#portraits"; </script>
所以,我稍微改变了你的逻辑和行为:
现在"根"菜单通过点击切换子链接,子链接显示其内容并隐藏子链接:
$(function() {
$(".root-link").click(function() {
$(this).parent().toggleClass("selected");
});
$(".tablinks").click(function() {
$(".tabcontent").hide();
var idtab = $(this).data("tab");
$("#" + idtab + "-tab").show();
$(".link-blue").removeClass("selected");
});
});
PS:不同的元素不能有相同的id,所以我在内容元素id上添加了sufix-tab。
Fiddle:https://jsfiddle.net/diegopolido/sar6spzb/
相关文章:
- 无法在Ionic select中预先选择最后一个选项
- 如果类不是一个选项,如何在使用 jQuery 时控制(避免)嵌套 html 元素的样式
- AngularJS ng选项在选择一个选项后删除了默认的空白值
- 在下拉列表中选择一个选项的值
- 创建了一个选项卡,但点击时不会起作用-jQuery issue/newbie
- HTML-从mysql生成的选择框中的一个选项更改文本区域的颜色
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 链接到手风琴中的另一个选项卡,嵌入的链接将不起作用
- 如何将数组附加到下拉导航栏并禁用其中的一个选项
- 尝试添加一个选项以使用append进行选择,但没有任何结果
- 在dojo中将数据从一个选项卡绑定到另一个选项卡(打开选项卡?)
- 单击按钮进入下一个选项卡,而不是单击选项卡
- PHP Jquery选项卡:内容加载到一个选项卡's面板
- 对添加另一个选项卡的onsubmit函数停止重定向
- 用CSS和Javascript创建一个选项卡系统
- 在下拉列表中的页面加载中已经选择了一个选项,该选项也可以更改
- jQuery UI 选项卡取消选择事件或在选择时保存上一个选项卡
- 单击链接时添加另一个选项
- 如何在 PHP 中使用 OnChange 事件的下拉列表中选择一个选项后,对大量数据行设置分页
- jQuery - 根据标识符获取下一个选项的总值