首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在css中启用页面加载时的悬停

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

Stack Overflow用户
提问于 2018-08-09 09:07:50
回答 1查看 985关注 0票数 0

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

我在标签中使用的HTML代码是:

代码语言:javascript
复制
<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代码是:

代码语言:javascript
复制
.tab-contents>.active
{
display:block;
}

问题陈述:

我想知道我应该做什么更改,以便在页面上加载伦敦标签总是启用悬停。内容部分已经启用,我现在只想用悬停启用选项卡。

代码:

代码语言:javascript
复制
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";
}
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-09 09:16:47

如果您所说的悬停是指选中。

然后,您只需附加相应的类(active)。

代码语言:javascript
复制
<button class="tablinks active" onclick="openCity(event, 'London')">London</button>

然后,您想要删除活动时,鼠标打开,只需改变这一点。(仅当鼠标不在.tab上时应用button.active )

代码语言:javascript
复制
.tab:not(:hover) button.active {
   /*^^^^^^^^^^*/
    background-color: #ccc;
}

完整的代码。

代码语言:javascript
复制
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";
}
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51757542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档