html 實現tab切換


tab切換在項目中也算是常用技術,一般實現tab切換都用js或者jq實現,今天介紹兩種只用css實現tab切換方法:

方法一:

原理:通過label標簽的關聯屬性和input的單選類型實現相應div的顯示

1.創建一個類名為wrap的div當作容器

2.創建四個label標簽,這將作為tab切換項

3.在每一個label中創建一個span標簽(導航內容),input標簽(實現選中於取消選中)type類型為radio,還要創建一個div作為這個導航項被點中是顯示內容框,

這里要注意的是input標簽的name必須是相同的,我這邊取名叫tab

最終HTML為下面這樣:

<div class="wrap">
    <label>
        <span>home</span>
        <input type="radio" name="tab" checked>
        <div>home-page</div>
    </label>
    <label>
        <span>list</span>
        <input type="radio" name="tab">
        <div>list-page</div>
    </label>
    <label>
        <span>news</span>
        <input type="radio" name="tab">
        <div>news-page</div>
    </label>
    <label>
        <span>mine</span>
        <input type="radio" name="tab">
        <div>mine-page</div>
    </label>
</div>

重要的css,通過將input的width設為0使得input的那個小圓點不現實,又通過label的關聯用導航項的點擊實現input的checked,然后通過input:checked+div{display:block}實現相應div的顯示

<style type="text/css">
        *{margin: 0;padding: 0;}
        .wrap{
            margin: 20px auto;
            width: 403px;
            height: 600px;
            border:1px solid brown;
            position: relative;
        }
        label{
            width: 100px;
            height: 30px;
            float: left;
            text-align: center;
            line-height:30px;
            border-right: 1px solid brown;
            border-bottom: 1px solid brown;
        }
        label:nth-of-type(4){
            border-right: none;
        }
        label span{
            cursor: pointer;
        }
        label div{
            width: 403px;
            height: 568px;
            position: absolute;
            left: 0;
            top: 31px;
            background: #eeeeee;
            display: none;
        }
        label input{
            width: 0;
        }
        input:checked+div{
            display: block;
        }
    </style>

方法二:

原理:通過a標簽的錨點實現切換,也就a的href的路徑是要切換div的id

1.創建一個類名為wrap的div作為容器

2.創建一個類名為nav的div,在里邊創建四個a標簽,a標簽的href分別是要切換到的div的id

3.創建一個和nav兄弟關系的類名為sh的容器用來放置切換的div

4.創建顯示內容div,id分別和上面a標簽對應

最終代碼如下:

<div class="wrap">
    <div class="nav">
        <a href="#home">home</a>
        <a href="#list">list</a>
        <a href="#news">news</a>
        <a href="#mine">mine</a>
    </div>
    <div class="sh">
        <div id="home">home-page</div>
        <div id="list">list-page</div>
        <div id="news">news-page</div>
        <div id="mine">mine-page</div>
    </div>
</div>

css樣式設置,即將類名為sh下的div設置為display:none;然后通過div:target{display:block}實現顯示選中項

<style type="text/css">
        *{margin: 0;padding: 0}
        .wrap{
            width: 400px;
            height: 600px;
            border: 1px solid brown;
            margin: 20px auto;
            position: relative;
        }
        .nav{
            width: 100%;
            height: 30px;
        }
        .nav a{
            width: 99px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            border-right: 1px solid brown;
            border-bottom: 1px solid brown;
            float: left;
            text-decoration: none;
            color:black;
        }
        .sh{
            width: 400px;
            height: 569px;
            position: absolute;
            left: 0;
            top:31px;
            background: #eeeeee;
        }
        .sh div{
            display: none;
            text-align: center;
        }
        .sh div:target{
            display: block;
        }
    </style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM