React and Gatsby:在某些页面上应用css / js砌体

问题描述 投票:0回答:1

我对React知之甚少,只是试图让Gatsby产品组合发展起来,它带有一个索引页面和一个制作带标签页面的模板。我想将此css / js砌体应用于索引和标记页面https://codepen.io/didumos/pen/xNPKRJ

但是我不确定最好的方法是什么。我想将JS分解为utils.js文件,然后将其导入index.js和aggedtemplate.js吗?它很长,并且具有一些加载/刷新功能。一位同事告诉我要使用挂钩,但不确定如何使用。

任何建议表示赞赏!谢谢!

哦stackoverflow告诉我在链接到Codepen时需要输入代码。这是从该代码笔改编的JS。

const minColWidth = 500;
let roots;

function onLoad() {
  var rootElements = document.getElementsByClassName('indexWrapper');
  roots = Array.prototype.map.call(rootElements, function(rootElement) {
    var cellElements = rootElement.getElementsByClassName('item');
    var cells = Array.prototype.map.call(cellElements, function(cellElement) {
      var style = getComputedStyle(cellElement);
      return {
        'element': cellElement,
        'outerHeight': parseInt(style.marginTop) + cellElement.offsetHeight + parseInt(style.marginBottom)
      };
    });
    return {
      'element': rootElement,
      'noOfColumns': 0,
      'cells': cells
    };
  });

  // do the first layout
  onResize();
}

function onResize() {
  for (let root of roots) {
    // only layout when the number of columns has changed
    var newNoOfColumns = Math.floor(root.element.offsetWidth / minColWidth);
    if (newNoOfColumns != root.noOfColumns) {

      // initialize
      root.noOfColumns = newNoOfColumns;
      var columns = Array.from(new Array(root.noOfColumns)).map( function(column) {
        return {
          'cells': new Array(),
          'outerHeight': 0
        };
      });

      // divide...
      for (let cell of root.cells) {
        var minOuterHeight = Math.min(...columns.map( function(column) {
          return column.outerHeight;
        }));
        var column = columns.find( function(column) {
          return column.outerHeight == minOuterHeight;
        });
        column.cells.push(cell);
        column.outerHeight += cell.outerHeight;
      }

      // calculate masonry height
      var masonryHeight = Math.max(...columns.map( function(column) {
        return column.outerHeight;
      }));

      // ...and conquer
      var order = 0;
      for (let column of columns) {
        for (let cell of column.cells) {
          cell.element.style.order = order++;
          // set the cell's flex-basis to 0
          cell.element.style.flexBasis = 0;
        }
        // set flex-basis of the last cell to fill the
        // leftover space at the bottom of the column
        // to prevent the first cell of the next column
        // to be rendered at the bottom of this column
        column.cells[column.cells.length - 1].element.style.flexBasis = column.cells[column.cells.length - 1].element.offsetHeight + masonryHeight - column.outerHeight - 1 + 'px';
      }

      // set the masonry height to trigger
      // re-rendering of all cells over columns
      // one pixel more than the tallest column
      root.element.style.maxHeight = masonryHeight + 1 + 'px';

      console.log(columns.map( function(column) {
        return column.outerHeight;
      }));
      console.log(root.element.style.maxHeight);
    }
  }
}

// subscribe to load and resize events
window.addEventListener('load', onLoad);
window.addEventListener('resize', onResize);
javascript css reactjs gatsby masonry
1个回答
0
投票

我在这里不能精确,因为我不确定确切的问题是什么。但是看一下代码,您似乎需要在响应的过程中采取较小的步骤。您可能希望尽可能避免dom查询和dom操作,其中包括getElementsByClassName,每个初学者都知道,但是在react上下文中使用它通常会使您感到痛苦。

您的协同工作对钩子是正确的,应该在使用addEventListener的任何时候都应使用它们,您希望将它们包装在useEffect中,以防止执行成百上千的addEventListener

有一个用于情感的gatsby插件,我在CSS中经常使用。但是在搜索砌体时,我怀疑它是为响应而设计的,除非它有特定的插件,否则您那里可能会有更多的运气。否则,祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.