Home > Web Front-end > JS Tutorial > body text

How to achieve the switching effect of tab bar

一个新手
Release: 2017-10-11 10:25:18
Original
2855 people have browsed it

Navigation bar switching effects:

The html code is as follows:

<ul id="tab">
    <li class="fli">tab1</li>
    <li>tab2</li>
    <li>tab3</li>
</ul>

<p id="tab_con">

    <p class="fp">aaaa</p>

    <p>bbbb</p>

    <p>cccc</p>

</p>
Copy after login

css style code As follows:

       ul, li, p {
            padding: 0;
            margin: 0;
        }

        ul li {
            float: left;
            width: 100px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: #fff;
            border: 1px #bbb solid;
            border-bottom: none;
            cursor: pointer;
        }

        ul li.fli {
            background-color: #ccc;
            color: red;
        }

        ul {
            overflow: hidden;
            zoom: 1;
            list-style-type: none;
        }

        #tab_con {
            width: 304px;
            height: 200px;
        }

        #tab_con p {
            width: 304px;
            height: 200px;
            display: none;
            border: 1px #bbb solid;
            border-top: none;
        }

        #tab_con p.fp {
            display: block;
            background-color: #ccc;
        }

        .tclass{
            width: 100px;
            height: 30px;
            background: #000;
        }
Copy after login

js code is as follows:

<script type="text/javascript">

    var tabs = document.getElementById("tab").getElementsByTagName("li");
    var ps = document.getElementById("tab_con").getElementsByTagName("p");
    for (var i = 0; i < tabs.length; i++) {
        tabs[i].onclick = function () {
            change(this);
        }
    }

    function change(obj) {

        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i] == obj) {
                tabs[i].className = "fli";
                ps[i].className = "fp";
            }
            else {
                tabs[i].className = "";
                ps[i].className = "";
            }
        }
    }

</script>
Copy after login

The above is the detailed content of How to achieve the switching effect of tab bar. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!