整页选项卡

创建于 2024-12-03 / 23
字体: [默认] [大] [更大]

了解如何使用 CSS 和 JavaScript 创建覆盖整个浏览器窗口的整页选项卡。


整页选项卡

点击链接显示"当前"页面:

家是心的所在..

今天好消息!

保持联系,或闲逛喝杯咖啡。

我们是谁,我们做什么。


亲自试一试 »


创建单页项卡

步骤 1) 添加 HTML:

实例

<button class="tablink" onclick="openPage('Home', this, 'red')">Home</button>
<button class="tablink" onclick="openPage('News', this, 'green')" id="defaultOpen">News</button>
<button class="tablink" onclick="openPage('Contact', this, 'blue')">Contact</button>
<button class="tablink" onclick="openPage('About', this, 'orange')">About</button>

<div id="Home" class="tabcontent">
  <h3>Home</h3>
  <p>Home is where the heart is..</p>
</div>

<div id="News" class="tabcontent">
  <h3>News</h3>
  <p>Some news this fine day!</p>
</div>

<div id="Contact" class="tabcontent">
  <h3>Contact</h3>
  <p>Get in touch, or swing by for a cup of coffee.</p>
</div>

<div id="About" class="tabcontent">
  <h3>About</h3>
  <p>Who we are and what we do.</p>
</div>

创建按钮以打开特定的选项卡内容。 默认情况下,所有带有 class="tabcontent" 的 <div> 元素都是隐藏的(使用 CSS 和 JS)。 当用户单击一个按钮时 - 它会打开与该按钮"匹配"的选项卡内容。


步骤 2) 添加 CSS:

设置链接和选项卡内容的样式(整页):

实例

/* 将正文和文档的高度设置为 100% 以启用"整页选项卡" */
body, html {
  height: 100%;
  margin: 0;
  font-family: Arial;
}

/* 选项卡链接样式 */
.tablink {
  background-color: #555;
  color: white;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  font-size: 17px;
  width: 25%;
}

.tablink:hover {
  background-color: #777;
}

/* 设置标签内容的样式(并为整页内容添加 height:100% ) */
.tabcontent {
  color: white;
  display: none;
  padding: 100px 20px;
  height: 100%;
}

#Home {background-color: red;}
#News {background-color: green;}
#Contact {background-color: blue;}
#About {background-color: orange;}

Advertisement

-->
步骤 3) 添加 JavaScript:

实例

function openPage(pageName, elmnt, color) {
   // 默认隐藏所有带有 class="tabcontent" 的元素 */
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // 删除所有标签链接/按钮的背景颜色
  tablinks = document.getElementsByClassName("tablink");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].style.backgroundColor = "";
  }

  // 显示具体的标签内容
  document.getElementById(pageName).style.display = "block";

  // 为用于打开选项卡内容的按钮添加特定颜色
  elmnt.style.backgroundColor = color;
}

// 用 id="defaultOpen" 获取元素并点击它
document.getElementById("defaultOpen").click(); 亲自试一试 »

提示: 另请查看 如何 - 选项卡



0 人点赞过