0

So I have a set of 4 divs at the bottom of my page. I intend for the top of these divs to look like tabs, and when the div (tab) is clicked on, that div's height will increase and thus appear like a hidden page is rising from the bottom of the window.

Here is my code so far:

      ---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});
 });

Here is a jsfiddle to demonstrate what I have http://jsfiddle.net/tkTJr/

I want to allow each div to be 100% of the window width but still enable the others to be clicked. At the moment if I did that I can only click on the one with the lowest z-index because they are overlapping. I was thinking of making the top of each div stick out like a tab to differentiate one from another. Any help in doing so?

Many thanks, I hope some one will know the solution to this problem.

4

1 回答 1

2

我尝试使用两种方法来实现这一点:

方法 #1:Javascript/jQuery

我继续添加功能,如果用户单击选项卡的内容或选项卡,则关闭“活动”选项卡。本质上,这只是切换选项卡的底部位置并在用户每次单击选项卡时显示/隐藏(通过向上/向下滑动)。这是一个现场演示。如果您不希望拥有附加功能,这个小提琴对您来说很好

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

<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>s 和<label>s 的 CSS 切换宽度/高度之前。这次我决定尝试仅使用 CSS 制作相同的选项卡,所以这是我的尝试。本质上,它会在选项卡周围放置一个链接,并在单击时对其进行动画处理,并在单击时对内容的高度进行动画处理。完成的工作少了很多,我总是喜欢完整的 CSS 项目 <3 但是这种方法并不能完全实现与 jQuery 方法相同的功能,这是我害怕并让我难过的:(问题描述如下在“功能说明”中

这是仅 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 方法不允许用户单击选项卡以“关闭”内容,他们必须单击其他任何位置。它还允许在单击内容时关闭选项卡,因此除非有人找到解决方法,否则它只能用于显示图像、文本等静态内容。

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

如果您想进一步解释它的任何部分,请告诉我。我希望我有所帮助!

于 2013-07-24T03:04:07.387 回答