乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當前位置:首頁 >  站長 >  編程技術(shù) >  正文

      html 實現(xiàn)tab切換的示例代碼

       2020-10-19 11:11  來源: 腳本之家   我來投稿 撤稿糾錯

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了html 實現(xiàn)tab切換的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

      方法一:

      原理:通過label標簽的關(guān)聯(lián)屬性和input的單選類型實現(xiàn)相應(yīng)div的顯示

      1.創(chuàng)建一個類名為wrap的div當作容器

      2.創(chuàng)建四個label標簽,這將作為tab切換項

      3.在每一個label中創(chuàng)建一個span標簽(導(dǎo)航內(nèi)容),input標簽(實現(xiàn)選中于取消選中)type類型為radio,還要創(chuàng)建一個div作為這個導(dǎo)航項被點中是顯示內(nèi)容框,

      這里要注意的是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設(shè)為0使得input的那個小圓點不現(xiàn)實,又通過label的關(guān)聯(lián)用導(dǎo)航項的點擊實現(xiàn)input的checked,然后通過input:checked+div{display:block}實現(xiàn)相應(yīng)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標簽的錨點實現(xiàn)切換,也就a的href的路徑是要切換div的id

      1.創(chuàng)建一個類名為wrap的div作為容器

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

      3.創(chuàng)建一個和nav兄弟關(guān)系的類名為sh的容器用來放置切換的div

      4.創(chuàng)建顯示內(nèi)容div,id分別和上面a標簽對應(yīng)

      最終代碼如下:

      <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è)置,即將類名為sh下的div設(shè)置為display:none;然后通過div:target{display:block}實現(xiàn)顯示選中項

      <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>

      到此這篇關(guān)于html 實現(xiàn)tab切換的示例代碼的文章就介紹到這了,更多相關(guān)html tab切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

      來源:腳本之家

      鏈接:https://www.jb51.net/web/732940.html

      申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

      相關(guān)標簽
      html

      相關(guān)文章

      熱門排行

      信息推薦