css - 滚动不工作 : react-grid-layout: 0-showcase example

标签 css reactjs scroll gatsby react-grid-layout

我正在尝试复制 0-showcase react-grid-layout 的示例,一切正常,除了包含网格及其组件的主要 HTML 元素不可滚动。我试过将 overflow-y scroll 提供给正文和后续元素,但无济于事。我正在使用 Gastsby 静态站点生成器来加速开发。因此,在我的 pages 中,直接在 Gastby 文件夹结构中,我创建了一个 grid.js 文件,其中包含以下 react 代码。几乎所有内容都在 example code here 中定义.这可能是我没有得到的一个小的 CSS 修复,但我真的不明白我在这里缺少什么。提前致谢!

import React, {PropTypes} from 'react'
import {Responsive, WidthProvider} from 'react-grid-layout';
import lomap from 'lodash.map'
import loresult from 'lodash.result'
import lorange from 'lodash.range'
import lorandom from 'lodash.random'

import "../node_modules/react-grid-layout/css/styles.css"
import "../node_modules/react-resizable/css/styles.css"

const ResponsiveReactGridLayout = WidthProvider(Responsive)

// const originalLayouts = getFromLS('layouts') || {}

function generateLayout() {
  return lomap( lorange(0, 25), function (item, i) {
    var y = Math.ceil(Math.random() * 4) + 1;
    return {
      x: lorandom(0, 5) * 2 % 12,
      y: Math.floor(i / 6) * y,
      w: 2,
      h: y,
      i: i.toString(),
      static: Math.random() < 0.05
    };
  });
}

export default class MyLayout extends React.Component {

  // static propTypes = {
  //   onLayoutChange: PropTypes.func.isRequired
  // }

  static defaultProps = {
    className: "layout",
    rowHeight: 30,
    cols: {lg: 12, md: 10, sm: 6, xs: 4, xxs: 2},
    initialLayout: generateLayout()
  }

  state = {
    currentBreakPoint: 'lg',
    mounted: false,
    layouts: {lg: this.props.initialLayout}
  }

  constructor(props, context){
    super(props, context)
  }

  componentDidMount() {
    this.setState({
      mounted: true
    })
  }

  generateDOM() {

    const styles = {
      background: "#eee"
    }

    return lomap(this.state.layouts.lg, (l, i) => {
      return (
        <div style={styles} key={i} className={l.static ? 'static': ''}>
          {
            l.static ? 
              <span className="text" title="This item is static and can't be removed or resized">
                static - {i}
              </span> :
              <span className="text">{i}</span> 
          }
        </div>
      )
    })
  }

  onBreakPointChange(breakpoint) {
    this.setState({
      currentBreakPoint: breakpoint
    })
  }

  onLayoutChange(layout, layouts) {
    // this.props.onLayoutChange(layout, layouts)
    console.log(layout, layouts);
  }

  onNewLayout() {
    this.setState({
      layouts: {
        lg: generateLayout()
      }
    })
  }

  render() {
    return (
      <div>
        <div>Current Breakpoint: {this.state.currentBreakpoint} ({this.props.cols[this.state.currentBreakpoint]} columns)
        </div>
        <button onClick={this.onNewLayout}>Generate New Layout</button>
        <ResponsiveReactGridLayout
          {...this.props}
          layouts={this.state.layouts}
          onBreakpointChange={this.onBreakpointChange}
          onLayoutChange={this.onLayoutChange}
          // WidthProvider option
          measureBeforeMount={false}
          // I like to have it animate on mount. If you don't, delete `useCSSTransforms` (it's default `true`)
          // and set `measureBeforeMount={true}`.
          useCSSTransforms={this.state.mounted}>
          {this.generateDOM()}
        </ResponsiveReactGridLayout>
      </div>
    )
  }
} 

最佳答案

这是一个非常愚蠢的问题。我只是不得不做

body {
  overflow: auto
}

并为我的 react-grid-layout 组件提供高度

render() {
    const styles = {
      height: "100vh" // just this!
    }
    return (
      <div>
        <div>Current Breakpoint: {this.state.currentBreakpoint} ({this.props.cols[this.state.currentBreakpoint]} columns)
        </div>
        <button onClick={this.onNewLayout}>Generate New Layout</button>
        <ResponsiveReactGridLayout
          style={styles} // AND THIS!
          {...this.props}
          layouts={this.state.layouts}
          onBreakpointChange={this.onBreakpointChange}
          onLayoutChange={this.onLayoutChange}
          // WidthProvider option
          measureBeforeMount={false}
          // I like to have it animate on mount. If you don't, delete `useCSSTransforms` (it's default `true`)
          // and set `measureBeforeMount={true}`.
          useCSSTransforms={this.state.mounted}>
          {this.generateDOM()}
        </ResponsiveReactGridLayout>
      </div>
    )
  }

关于css - 滚动不工作 : react-grid-layout: 0-showcase example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42423662/

相关文章:

html - 如何在 Bootstrap NavBar 中将图像准确地放置在我想要的位置(并且响应)

css - bootstrap 4 中心标题和右对齐按钮全部在一排

javascript - 在没有任何插件或 UI 的情况下模拟滚动条 - 可能吗?

html - CSS3 列表转换 : position of bullets

html - 在图像上叠加文本

javascript - 无需循环即可动态更改类样式

javascript - 如何更改 onRowAdd、onRowUpdate、onRowDelete 的 Material 表图标的颜色?

Javascript/React window.onerror 触发了两次

html - 如何向从小屏幕设备访问网站的平板电脑用户显示滚动条?

android - 在android中滚动子 ScrollView 时如何停止父 ScrollView 的滚动?