javascript - 在 jquery 中创建类似书籍的布局

标签 javascript jquery angularjs layout

我正在开发一个网络应用程序,它要求其数据以类似书籍的格式显示。数据是这样的

Menu{
  String menuName;
  List<String> subMenu;
}

我使用jquery以书籍格式显示数据。div是动态生成的。
接下来的步骤:

1. created a single array with menuName and its subMenus.like completeData = [menuObj1,"submenu1","subMenu2",menuObj2,"subMenu1","subMenu2","subMenu3",...];  
2. First fill left Container  
      For Each value in completeData
      if it is obj then create a new Div menuDiv and display its menuName and append to container.
check container height if it exceeds then break.

      if it is String then create a new subMenuSpan and append it to subMenuDiv and finally append it to container.
check container height if it exceeds then break and store the index.

3. Now fill the right container
      start from the storedIndex from Left Container and continue the same process.

代码:左侧容器

for(var i=index;i<completeData.length;i++){

    if(typeof completeData[i] == 'string'){
        // subMenu
        menuSpan = $('<div id="menuSpan">').text(completeData[i]);
        if(typeof completeData[i-1] == 'string'){
            menuSpan.appendTo(menuDiv);

            // Break if container height exceeds and store the index.
            if($('#menu-inner-left').height() >= height){
                menuSpan.remove();
                rightIndex=i;
                break;
            }
        }else{
            menuDiv = $('<div id="menuDiv">').appendTo('#menu-inner-left');
            menuSpan.appendTo(menuDiv);

            // Break if container height exceeds and store the index.
            if($('#menu-inner-left').height() >= height){
                 menuDiv.remove();
                 rightIndex=i;
                 break;
            }
        }
    }else{
        // Menu Name
        menuNameDiv = $('<div id="menuNameDiv">').appendTo('#menu-inner-left');
        $('<div>').text(completeData[i].menuName).appendTo(menuNameDiv).css('float','left');

        // Break if container height exceeds and store the index.
        if($('#menu-inner-left').height() >= height){
              menuDiv.remove();
              rightIndex=i;
              break;
        }
     }
  }


代码:适用于正确的容器 从左侧容器中存储的索引开始,然后进行相同的过程

for(var i=rightIndex;i<completeData.length;i++){
    // Same Code of Left Container
}

结果的最终图像 enter image description here

现在图书 View 已正确显示,但问题是: 1. 由于创建了大量 div,因此需要花费大量时间来显示。

我尝试用纯 JavaScript 创建 div,但仍然没有效果。 现在我们正在转向 Angular JS,以避免使用 jquery 创建 div。这会影响加载时间吗?

还有什么其他方法可以避免创建大量 div 并减少加载时间。

最佳答案

这不是解决方案,只是根据屏幕捕获实现的一种方法。 不要立即加载所有数据,而是尝试在从源获取数据后将数据存储在缓存中,而是在每个下一页/上一页上单击编写一个回调,该回调将仅获取当前页面数据,以便可以减少渲染时间。或者您可以使用一些插件来实现这种目的,例如 @aeno 指定的
Turn.js。

Menu{
  data:[ 
       String menuName;
       List<String> subMenu;
      ]
  pagingObject:{
   pageNumber<int>,
   currentPage<int>,
   pageSize<int>

  }
}

关于javascript - 在 jquery 中创建类似书籍的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115469/

相关文章:

jquery - 每 3 秒更改一次文本或使用左/右箭头转到下一个文本 - bootstrap 3

javascript - 试图了解 meteor

javascript - 下拉菜单打乱其他标签

javascript - 在 AngularJs Controller 中使用 Asp.Net ViewBag 值

javascript - 当对象非常多并且我试图在范围内访问它时,为什么我会得到 "Can not read property 0 of undefined"?

javascript - 单击按钮,计算总价(Javascript)

javascript - Discord.js v13 选项中的选择 - 斜线命令

javascript - 如何在 Angular js View 中返回整数值而不是科学值?

javascript - ionic 选择框与默认选择下拉列表相同

angularjs - 如何检查元素是否具有 AngularJS 类?