簡體   English   中英

使用 jquery 或 javascript 將菜單顯示到頂部

[英]show menu to the top using jquery or javascript

我在我的項目中左側導航包含一個帶有菜單的滾動問題是,當我打開一個菜單時,子菜單項會顯示,並且某些項目 go 在我們必須滾動后隱藏。 所以我想在打開菜單時在頂部顯示菜單顯示在頂部不需要滾動用戶所以如何實現;

例如:

在此處輸入圖像描述

您好,您可以使用javascriptjquery ,因為您需要閱讀 javascript 中的scrollIntoView scrollIntoView()方法及其工作原理。

你可以在這里查看和閱讀

因此,首先您需要在要顯示的特定div上添加idclass

假設我有這樣的 HTML 代碼

<li class="treeview" id="view"> // this treeview class is for showing submenu
  <a>Menu 4</a>
  <ul>
    <li>item 1</li>
    <li>item 1</li>
    <li>item 1</li>
  </ul>
<li>

之后只需在<script></script>標簽中添加以下代碼

$(".treeview").click(function(e){
      setTimeout(() => {
         var element = document.getElementById(e.currentTarget.id); // e.currentTarget.id give you current id when ever you click li or div this will give you view 
         if(e.currentTarget.id){
            element.scrollIntoView({
               behavior: 'smooth', block: 'nearest', inline: 'start',
            });
         }
      }, 500);  
 }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM