代码之家  ›  专栏  ›  技术社区  ›  flash

在css中启用页面加载时悬停

  •  0
  • flash  · 技术社区  · 6 年前

    我正在一个网站上工作,我想在其中启用页面加载悬停。

    我用于选项卡的HTML代码是:

    <div class="tab">
      <button class="tablinks" onclick="openCity(event, 'London')">London</button>
      <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
      <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
    </div>
    

    我用来启用伦敦选项卡内容的CSS代码是:

    .tab-contents>.active
    {
    display:block;
    }
    

    问题陈述:

    我想知道我应该在小提琴上做些什么改变以便在页面加载时 “伦敦”选项卡始终启用“悬停” . 内容部分已经启用,我只想现在用鼠标悬停来启用选项卡。

    代码:

    function openCity(evt, cityName) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
      }
      document.getElementById(cityName).style.display = "block";
      evt.currentTarget.className += " active";
    }
    body {
      font-family: Arial;
    }
    
    
    /* Style the tab */
    
    .tab {
      overflow: hidden;
      border: 1px solid #ccc;
      background-color: #f1f1f1;
    }
    
    
    /* Style the buttons inside the tab */
    
    .tab button {
      background-color: inherit;
      float: left;
      border: none;
      outline: none;
      cursor: pointer;
      padding: 14px 16px;
      transition: 0.3s;
      font-size: 17px;
    }
    
    
    /* Change background color of buttons on hover */
    
    .tab button:hover {
      background-color: #ddd;
    }
    
    
    /* Create an active/current tablink class */
    
    .tab button.active {
      background-color: #ccc;
    }
    
    
    /* Style the tab content */
    
    .tabcontent {
      display: none;
      padding: 6px 12px;
      border: 1px solid #ccc;
      border-top: none;
    }
    
    
    /* Style the tab content */
    
    .tab-contents>.active {
      display: block;
    }
    <p>Click on the buttons inside the tabbed menu:</p>
    
    <div class="tab">
      <button class="tablinks" onclick="openCity(event, 'London')">London</button>
      <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
      <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
    </div>
    
    <div class="tab-contents">
      <div id="London" class="tabcontent active">
        <h3>London</h3>
        <p>London is the capital city of England.</p>
      </div>
    
      <div id="Paris" class="tabcontent">
        <h3>Paris</h3>
        <p>Paris is the capital of France.</p>
      </div>
    
      <div id="Tokyo" class="tabcontent">
        <h3>Tokyo</h3>
        <p>Tokyo is the capital of Japan.</p>
      </div>
    </div>
    1 回复  |  直到 6 年前
        1
  •  1
  •   apple apple    6 年前

    如果由 悬停 ,你是说 挑选出来的 .

    然后简单地附加对应的类( active ).

    <button class="tablinks active" onclick="openCity(event, 'London')">London</button>
    

    然后你想在鼠标打开时移除活动的,只需改变这个。(仅适用于 button.active 当鼠标未打开时 .tab )

    .tab:not(:hover) button.active {
       /*^^^^^^^^^^*/
        background-color: #ccc;
    }
    

    完整的代码。

    function openCity(evt, cityName) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
      }
      document.getElementById(cityName).style.display = "block";
      evt.currentTarget.className += " active";
    }
    body {
      font-family: Arial;
    }
    
    
    /* Style the tab */
    
    .tab-bar {
      overflow: hidden;
      border: 1px solid #ccc;
      background-color: #f1f1f1;
    }
    
    
    /* Style the buttons inside the tab */
    
    .tab button {
      background-color: inherit;
      float: left;
      border: none;
      outline: none;
      cursor: pointer;
      padding: 14px 16px;
      transition: 0.3s;
      font-size: 17px;
    }
    
    
    /* Change background color of buttons on hover */
    
    .tab button:hover {
      background-color: #ddd;
    }
    
    /* Create an active/current tablink class */
    .tab:not(:hover) button.active {
      background-color: #ccc;
    }
    
    
    /* Style the tab content */
    
    .tabcontent {
      display: none;
      padding: 6px 12px;
      border: 1px solid #ccc;
      border-top: none;
    }
    
    
    /* Style the tab content */
    
    .tab-contents>.active {
      display: block;
    }
    <p>Click on the buttons inside the tabbed menu:</p>
    
    <div class="tab-bar">
      <div class="tab">
        <button class="tablinks active" onclick="openCity(event, 'London')">London</button>
        <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
        <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
      </div>
    </div>
    
    <div class="tab-contents">
      <div id="London" class="tabcontent active">
        <h3>London</h3>
        <p>London is the capital city of England.</p>
      </div>
    
      <div id="Paris" class="tabcontent">
        <h3>Paris</h3>
        <p>Paris is the capital of France.</p>
      </div>
    
      <div id="Tokyo" class="tabcontent">
        <h3>Tokyo</h3>
        <p>Tokyo is the capital of Japan.</p>
      </div>
    </div>