【问题标题】:enabling hover on page load in css在css中启用页面加载悬停
【发布时间】:2018-08-09 01:07:50
【问题描述】:

我正在开发一个网站,我希望在其中启用悬停页面加载功能。

我用于标签的 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>

【问题讨论】:

    标签: javascript jquery html css hover


    【解决方案1】:

    如果悬停,你的意思是选中

    然后你只需附加对应的类(active)。

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

    然后您想在鼠标打开时删除活动,只需更改它即可。 (仅当鼠标不在.tab 上时才应用button.active

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

    【讨论】:

    • active 标签属于标签,而不是按钮。
    • @Barmar 的 css 是 .tab button:hover{...},我看不出有什么问题。
    • @apple apple 你可以在fiddle 中进行更改吗?这样我很容易想象。
    • 使用 Stack Snippet 而不是外部链接。
    • 我明白了,他在默认选项卡上已经有active,他对相应的按钮使用相同的类。
    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 2016-12-06
    • 2023-04-08
    • 1970-01-01
    • 2021-01-06
    • 2023-04-02
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多