Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created May 7, 2015 09:34
Show Gist options
  • Select an option

  • Save arnabdas/572eb304fb99c9991744 to your computer and use it in GitHub Desktop.

Select an option

Save arnabdas/572eb304fb99c9991744 to your computer and use it in GitHub Desktop.
Responsive Tabs with Bootstrap and jQuery
<!DOCTYPE html>
<!--
Copyright 2015 Arnab Das
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>Bootstrap Responsive Tabs</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<style type="text/css">
.nav-tabs li a.tab-control{
float: left;
width: 36px;
height: 36px;
margin-top: 4px;
font-size: 56px;
font-weight: 100;
line-height: 8px;
color: #fff;
display: none;
text-align: center;
background: #444;
-webkit-border-radius: 18px;
-moz-border-radius: 18px;
border-radius: 18px;
}
.nav-tabs li a.tab-control.spacer {
background: transparent;
}
@media (max-width: 992px) {
.nav-tabs li {
min-width: 98%;
display: none;
}
.nav-tabs li.active {
display: block;
}
.nav-tabs li a.tab-control {
display: block;
}
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<ul class="nav nav-tabs">
<li role="presentation" class="active">
<a href="javascript:void(0);" class="pull-left">Home</a>
<a href="javascript:void(0);" class="pull-right tab-control nav-right">›</a>
<a href="javascript:void(0);" class="pull-right tab-control nav-left spacer"></a>
</li>
<li role="presentation">
<a href="javascript:void(0);" class="pull-left">Profile</a>
<a href="javascript:void(0);" class="pull-right tab-control nav-right">›</a>
<a href="javascript:void(0);" class="pull-right tab-control nav-left">‹</a>
</li>
<li role="presentation">
<a href="javascript:void(0);" class="pull-left">Messages</a>
<a href="javascript:void(0);" class="pull-right tab-control nav-right spacer"></a>
<a href="javascript:void(0);" class="pull-right tab-control nav-left">‹</a>
</li>
</ul>
</div>
</div>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
(function ($) {
var firstLeft = $('.nav-tabs li:first-child a.nav-left');
firstLeft.css('border', 0);
var lastRight = $('.nav-tabs li:last-child a.nav-right');
lastRight.css('border', 0);
$('.nav-tabs li a.tab-control').on('click', function (e) {
e.preventDefault();
/**
* As we are showing only the li having the class active, so we need to
* remove the class 'active' from current li, and add it to the correct li
* ***/
var _self = $(this);
var parent = _self.parent(); // Parent li
var toRight = _self.hasClass('nav-right'); // If we have to go to the right
var reqActive = toRight? parent.next(): parent.prev(); // Required li to show
// This check is for the clicking on first left and last right nav
if (reqActive.length > 0) {
parent.removeClass('active'); // Remove 'active' from current
reqActive.addClass('active'); // Add 'active' to the proper li
}
});
})(jQuery);
</script>
</body>
</html>
@arnabdas
Copy link
Copy Markdown
Author

arnabdas commented May 7, 2015

CSS is not fixed yet.

@arnabdas
Copy link
Copy Markdown
Author

arnabdas commented May 7, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment