使用单击标签在页面底部显示隐藏的内容

时间:2013-07-23 17:35:10

标签: javascript html css3 tabs

所以我在页面底部有一组4个div。我打算让这些div的顶部看起来像标签,当点击div(tab)时,div的高度会增加,因此看起来隐藏的页面从窗口底部升起。

到目前为止,这是我的代码:

      ---Css---

 tab1 {
   float: left;
   width: 400px;
   height: 100px;
   background: red;
   position: absolute;
   left: 300px;
   bottom: 0px;
   clear:both;
      }

 tab2 {
   width: 400px;
   height: 100px;
   border-radius: 10px 10px 0px 0px;
   background: green;
   position: absolute;
   left: 400px;
   bottom: 0px;
      } 

 tab3 {
   width: 400px;
   height: 100px;
   border-radius: 10px 10px 0px 0px;
   background: red;
   position: absolute;
   left: 500px;
   bottom: 0px;
      }
 tab4 {
   width: 400px;
   height: 100px;
   border-radius: 10px 10px 0px 0px;
   background: green;
   position: absolute;
   left: 600px;
   bottom: 0px;
      }



      ---HTML---

 <tab1></tab1>
 <tab2></tab2>
 <tab3></tab3>
 <tab4></tab4>


     ---JavaScript---
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"type="text/javascript"></script>
<script>

 $('tab1').toggle(function(){
     $(this).animate({'height': '500px'},{speed:10,});
 }, function(){
     $(this).animate({'height': '100px'},{speed:10, });
 });
  $('tab2').toggle(function(){
     $(this).animate({'height': '500px'},{speed:10});
 }, function(){
     $(this).animate({'height': '100px'}, {speed:10});
 });
  $('tab3').toggle(function(){
     $(this).animate({'height': '500px'},{speed:10,});
 }, function(){
     $(this).animate({'height': '100px'},{speed:10, });
 });
 $('tab4').toggle(function(){
     $(this).animate({'height': '500px'},{speed:10});
}, function(){
     $(this).animate({'height': '100px'}, {speed:10});
 });

这是一个jsfiddle来演示我有http://jsfiddle.net/tkTJr/

我想允许每个div为窗口宽度的100%,但仍然可以单击其他div。目前,如果我这样做,我只能点击具有最低z-index的那个,因为它们是重叠的。我正在考虑让每个div的顶部像一个标签一样突出,以区分彼此。这样做有什么帮助吗?

非常感谢,我希望有人知道解决这个问题的方法。

1 个答案:

答案 0 :(得分:2)

我尝试使用两种方法实现这一目标:

方法#1:Javascript / jQuery

我继续添加了关闭“有效”标签的功能,如果用户点击标签的内容或标签,我会调用它。基本上,这只是切换选项卡的底部位置,并在每次用户单击选项卡时显示/隐藏(通过向上/向下滑动)。 Here is a live demo。如果您不希望获得添加的功能,this fiddle will do just fine为您

以下是具有增强功能的版本的相关代码:

<script> // The most important section for you
$('.tab').click(function (e) {
    e.stopPropagation(); // allows the :not to function in the next .click function
    var toggleBot = $(this).css('bottom') == "400px" ? "0px" : "400px"; 
    // ^^ Clever way of toggling between two values
    $(this).animate({
        'bottom': toggleBot // Toggle the value
    });
    var number = $(this).attr('class').split(' ')[1]; // Get the number to relate to the content

    if ($('.active')[0] && number != $('.active').attr('class').split(' ')[1]) {
    // This part makes only one content stay open at a time
        var number2 = $('.active').attr('class').split(' ')[1];
        var toggleBot2 = $('.tab.' + number2).css('bottom') == "0px" ? "400px" : "0px";
        $('.tab.' + number2).animate({
            'bottom': toggleBot2
        });
        $('.content.' + number2).slideToggle().toggleClass('active');
    }
    $('.content.' + number).slideToggle().toggleClass('active');
});
$('.content').click(function(e) { // Again, allows the :not to function correctly below
    e.stopPropagation();
});
$('body:not(.tab, .content)').click(function() { 
// Allows the 'active' tab to be closed when the anything but a tab/content is clicked
    var number2 = $('.active').attr('class').split(' ')[1];
    $('.tab.' + number2).animate({
        'bottom': '0'
    });
    $('.content.' + number2).slideToggle().toggleClass('active');
});
</script>

<style>
div {
    text-align:center;
}
.tab {
    width: 100px;
    height: 50px;
    border-radius: 10px 10px 0px 0px;
    position: absolute; /* !!Important for functionality of tab!! */
    bottom: 0px; /* !!Important for functionality of tab!! */
    z-index:2;
}
.tab.one {
    background: red;
    left:10%;
}
.tab.two {
    background: blue;
    left:30%;
}
.tab.three {
    background: yellow;
    left:50%;
}
.tab.four {
    background:green;
    left:70%;
}
.content {
    border-radius: 10px 10px 0px 0px;
    position:absolute; /* !!Important for functionality of content!! */
    display:none; /* !!Important for functionality of content!! */
    bottom:0; /* !!Important for functionality of content!! */
    left:0px; 
    background:black;
    color:white;
    height:400px;
    width:100%;
    z-index:1;
}
/* Just to add some content */
#mainContent {
    position:relative;
    width:25%;
    height:75%;
    clear:both;
}
</style>

<html> <!-- Note: they are all on the same level -->
<body>
    <div id='#mainContent' style='position:relative; width:75%; height:75%; left:12.5%;'>Zombie ipsum content</div>
    <div class='tab one'>Tab 1</div>
    <div class='content one'>Content 1!</div>
    <div class='tab two'>Tab 2</div>
    <div class='content two'>Content 2!</div>
    <div class='tab three'>Tab 3</div>
    <div class='content three'>Content 3!</div>
    <div class='tab four'>Tab 4</div>
    <div class='content four'>Content 4!</div>
</body>
</html>

方法#2:CSS

在我使用基于<input><label> s的CSS切换宽度/高度之前。这次我决定尝试使用CSS制作相同的标签,所以here is my attempt。从本质上讲,它会在选项卡周围添加一个链接,并在单击时将其设置为动画,并在单击时激活内容的高度。完成后花了很多东西,我总是喜欢完整的CSS项目&lt; 3然而,这种方法并没有完全实现与jQuery方法相同的功能,这是我害怕的并让我伤心 :( < / strong>下面的“功能说明”

中描述了该问题

以下是仅限CSS方法的相关代码:

//No javascript, Yay!
<style>
div {
    text-align:center;
}
.tab {
    width: 100px;
    height: 50px;
    text-align:center;
    border-radius: 10px 10px 0px 0px;
    color:black;
    position: absolute;
    bottom: 0px;
    z-index:2;
}
.tab.one {
    background: red;
    left:10%;
}
.tab.two {
    background: blue;
    left:30%;
}
.tab.three {
    background: yellow;
    left:50%;
}
.tab.four {
    background:green;
    left:70%;
}

 #mainContent {
    position:relative;
    width:25%;
    height:75%;
    clear:both;
}
#wrapper { /* Allows the tabs to be at the bottom */
    position:absolute;
    bottom:0;
    width:100%;
    text-decoration: none;
}
.content {
    border-radius: 10px 10px 0px 0px;
    position:absolute;
    bottom:0;
    left:0px;
    background:black;
    color:white;
    height:0px;
    width:100%;
    z-index:1;
    -webkit-transition:all 1s ease;
    -moz-transition:all 1s ease;
    -ms-transition:all 1s ease;
}
.hideUp {
    display:block;
    position:relative;
    -webkit-transition:all 1s ease;
    -moz-transition:all 1s ease;
    -ms-transition:all 1s ease;
}
.hideUp:focus {
    bottom: 400px;
}
.hideUp:focus + .content {
    height:400px;
}
</style>

<html>
<body>
<div id='#mainContent' style='position:relative; width:75%; height:75%; left:12.5%;'>Zombie ipsum content.</div>
<div id='wrapper'>
    <a href="#" tabindex="-1" class="hideUp"> <!-- Allows the CSS to know whether the tab has focus or not -->
        <div class="tab one">Tab 1</div>
    </a> 
    <div class="content">Content 1</div>

    <a href="#" tabindex="-1" class="hideUp">
        <div class="tab two">Tab 2</div>
    </a>
    <div class="content">Content 2</div>

    <a href="#" tabindex="-1" class="hideUp">
        <div class="tab three">Tab 3</div>
    </a>
    <div class="content">Content 3</div>

    <a href="#" tabindex="-1" class="hideUp">
        <div class="tab four">Tab 4</div>
    </a>
    <div class="content">Content 4</div>
</div>
</body>
</head>

使用说明:jQuery方法要求设备能够运行jQuery(当然),CSS方法要求用户使用允许CSS3过渡的“现代”浏览器。我没有在我的CSS中包含所有浏览器标签,只包括webkit,mozilla和IE的标签。

功能说明:我使用的CSS方法不允许用户点击标签来“关闭”内容,他们必须点击其他地方。它还允许在单击内容时关闭选项卡,因此除非有人找到解决方法,否则它只能用于显示静态内容,如图像,文本等。

可以将CSS演示更改为仅在使用checkbox-hack单击选项卡本身时打开/关闭,允许选择内容等

如果你想进一步解释它的任何部分,请告诉我。我希望我能帮忙!