火狐瀏覽器隱藏滾動(dòng)條:
#navMenuWrapper {
max-width: 189px;
overflow: hidden;
}
#navManu {
max-height: calc(100vh - 135px);
overflow-x: hidden;
overflow-y: scroll;
width: 205px;
}
問題:點(diǎn)擊每一項(xiàng)時(shí)保證展開的子菜單可見(操作滾動(dòng)條的位置)
解決方法:移除類dropdown-toggle,手動(dòng)操作顯示和隱藏贪惹,因?yàn)榧由线@個(gè)類css的變化總在js后面,導(dǎo)致無法在js中控制滾動(dòng)條的位置儒拂;獲取點(diǎn)擊元素距頁(yè)面底端的距離琉历,如果高度不夠卦尊,就讓滾動(dòng)條向下滾動(dòng)使其可見揉燃,如果點(diǎn)擊元素距頁(yè)面頂端不在可見區(qū)域內(nèi)扫尺,讓滾動(dòng)條向上滾動(dòng)使其可見。
html:
html += "<li>";
html += "<a href=\"" + res.data[manuIndex].Path + "\">";
html += "<i class=\"" + res.data[manuIndex].Icon + "\"></i>";
html += "<span class=\"menu-text\">" + res.data[manuIndex].Name + "</span>";
html += "<b class=\"arrow icon-angle-down\"></b>";
html += "</a>";
html += "<ul class=\"submenu\">";
for (var subManuIndex = 0; subManuIndex < res.data[manuIndex].SubMenus.length; subManuIndex++) {
html += "<li>";
html += "<a path=\"" + res.data[manuIndex].SubMenus[subManuIndex].Path + "\" style=\"cursor:pointer;\" onclick=\"loadHtml(this);\">";
html += "<i class=\"" + res.data[manuIndex].SubMenus[subManuIndex].Icon + "\"></i>" + res.data[manuIndex].SubMenus[subManuIndex].Name;
html += "</a>";
html += "</li>";
}
html += "</ul>";
html += "</li>";
js:
$("#navManu li a:has(b)").on("click.bs.dropdown", function () {
var obj = $(this).parent();
//console.log("Opening dropdown..");
//手動(dòng)處理菜單項(xiàng)顯示和隱藏
if (oldobj == null) {//首次點(diǎn)擊炊汤,顯示
$(obj).attr("class", "active open");
$(obj).find(".submenu").css("display", "block");
$(obj).find(".arrow").css("transform", "rotate(90deg)");
oldobj = obj;
} else if ($(oldobj).html() == $(obj).html()) {//和上次點(diǎn)擊菜單相同正驻,隱藏
$(obj).removeClass("active open");
$(obj).find(".submenu").css("display", "none");
$(obj).find(".arrow").css("transform", "rotate(0deg)");
oldobj = null;
} else {//點(diǎn)擊了其他菜單,隱藏上個(gè)抢腐,顯示當(dāng)前
$(oldobj).removeClass("active open");
$(oldobj).find(".submenu").css("display", "none");
$(oldobj).find(".arrow").css("transform", "rotate(0deg)");
$(obj).attr("class", "active open");
$(obj).find(".submenu").css("display", "block");
$(obj).find(".arrow").css("transform", "rotate(90deg)");
oldobj = obj;
}
//if ($(obj).attr("class") == 'active open') {
// $(obj).removeClass("active open");
// $(obj).find(".submenu").css("display", "none");
//} else {
// $(obj).attr("class", "active open");
// $(obj).find(".submenu").css("display", "block");
//}
var h1 = $(obj).height();
var h2 = $(obj).find(".submenu").height();
var wh = $(window).height();//瀏覽器窗口高度
var xh = wh - (h1 + $(obj).offset().top);//元素到瀏覽器底部的高度
if (xh < 50) {
$("#navManu").scrollTop(-xh + 50 + $("#navManu").scrollTop());
}
if ($(obj).offset().top < 85) {
$("#navManu").scrollTop(-h2);
}
});