Thursday, 1 March 2018

MVC - jQuery - Vertical Tabs manually

Watch this example on YouTube



1. Layout

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/Scripts/jquery-1.12.4.min.js")
    @Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

2. Index

<div class="container">
    <ul class="tabs">
        <li class="tab-link current" data-tab="tab-1">Tab 1</li>
        <li class="tab-link" data-tab="tab-2">Tab 2</li>
        <li class="tab-link" data-tab="tab-3">Tab 3</li>
        <li class="tab-link" data-tab="tab-4">Tab 4</li>
    </ul>
    <div id="tab-1" class="tab-content current">some text 1</div>
    <div id="tab-2" class="tab-content">some text 2</div>
    <div id="tab-3" class="tab-content">some text 3</div>
    <div id="tab-4" class="tab-content">some text 4</div>
</div>

<script>
    $(document).ready(function () {
        $('ul.tabs li').click(function () {
            var tab_id = $(this).attr('data-tab');
            $('ul.tabs li').removeClass('current');
            $('.tab-content').removeClass('current');
            $(this).addClass('current');
            $("#" + tab_id).addClass('current');
        });
        $('ul.tabs li').mouseover(function () {
            var tab_id = $(this).attr('data-tab');
            $('ul.tabs li').removeClass('current');
            $('.tab-content').removeClass('current');
            $(this).addClass('current');
            $("#" + tab_id).addClass('current');
        });
    });
</script>

3. Add to CSS

ul.tabs{
    width:15%;
    float:left;
    margin: 0px;
    padding:0px;
    list-style:none;
}
ul.tabs li{
    width: 100%;
    background: none;
    color: red;
    display: inline-block;
    padding: 10px 0px;
    background-color: green;
}
ul.tabs li.current{
    color: blue;
    background-color: yellow;
}
.tab-content{
    width: 60%;
    height:30em;
    float:left;
    display: none;
    background:white;
    padding: 15px;
}
.tab-content.current{
    display:inherit;
}

No comments:

Post a Comment