我有一个带有以下标签的网页:
但是,每当我最小化窗口或调整窗口大小时,所有选项卡的位置都会消失,事情会像这样失真:
将来,我将添加更多的选项卡,并且像这样继续下去,将会出现可视化它们的问题。有谁知道如何解决?
是否可以在标签上进行水平滚动?请任何人知道如何解决?
这是代码:
<div class="tab" id="navbar">
<button class="tablink" onclick="openPage('tab1', this, 'gray')" id="defaultOpen">Tab 1</button>
<button class="tablink" onclick="openPage('tab2', this, 'gray')">Tab 2</button>
<button class="tablink" onclick="openPage('tab3', this, 'gray')">Tab 3</button>
<button class="tablink" onclick="openPage('tab4', this, 'gray')">Tab 4</button>
<button class="tablink" onclick="openPage('tab5', this, 'gray')">Tab 5</button>
<button class="tablink" id="myBtn">Tab 6</button>
<button class="tablink" onclick="location.href='Logout.php'">Tab 7</button>
</div>
选项卡具有的2个脚本:
<script>
function openPage(pageName,elmnt,color) {
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;
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
<script>
<!---"sticky" navbar----->
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
</script>
css:
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #555;
}
/* Style tab links */
.tablink {
/*margin: auto;*/
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 50px;
font-size: 20px;
/*width: 30%;*/
width: fit-content;
}
.tablink:hover {
background-color: #777;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
答案 0 :(得分:2)
您可以将所有选项卡与CSS放在同一行:
.tab{display: flex; flex-wrap: nowrap;}
,如果您想在移动设备上进行滚动,则可以使用相同的方式进行滚动:
.tab{display: flex; flex-wrap: nowrap; overflow: auto}