From 40532b4e7b4d6f20dae7d4f93aa874b72d4bdfcf Mon Sep 17 00:00:00 2001 From: IrisVega Date: Wed, 10 Jul 2024 15:35:23 +0800 Subject: [PATCH] modified: script.js --- script.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/script.js b/script.js index 0c6373c..df8d2a1 100644 --- a/script.js +++ b/script.js @@ -3,7 +3,7 @@ $(document).ready(function() { $(".tabcontent").hide(); $("#Tab0").show(); $(".tablinks").first().addClass("active"); - + var openedTabs = new Set(); // 点击选项卡按钮时切换内容 $(".tablinks").click(function() { var tabName = $(this).data("tab"); @@ -41,6 +41,7 @@ $(document).ready(function() { $(`.tablinks[data-tab="${newTabId}"]`).addClass("active"); bindTabEvents(); updateContentOpacity($("#mySlider").val()); + openedTabs.add(newIndex); }); // 删除选项卡 $(document).on("click", ".closeButton", function() { @@ -122,12 +123,23 @@ $(document).ready(function() { return parseInt($(this).data("tab").replace("Tab", "")); }).get(); indices.sort((a, b) => a - b); // 确保索引是按顺序排列的 - for (var i = 1; i <= indices.length + 1; i++) { - if (!indices.includes(i)) { - return i; - } + // for (var i = 1; i <= indices.length + 1; i++) { + // if (!indices.includes(i)) { + // return i; + // } + // } + // return indices.length + 1; + //最小索引值 + // var maxIndex = indices.length > 0 ? indices[indices.length - 1] : 0; // 找到最大索引值,如果没有索引则默认为0 + // return maxIndex + 1; // 返回最大索引值加1 + //最大索引值 + // 找到未打开过的最小索引 + var nextIndex = 1; + while (indices.includes(nextIndex) || openedTabs.has(nextIndex)) { + nextIndex++; } - return indices.length + 1; + + return nextIndex; } // 初始绑定事件 bindTabEvents();