发布于 2021-08-12 11:50:21
我猜,当你说容器时,你指的是布局项目。如果是这样的话,使用自定义的onResize方法。使用您从问题中获得的沙箱代码:
export default class ShowcaseLayout extends React.Component {
constructor(props) {
...
// add the line below
this.onResize = this.onResize.bind(this);
}
...
// add the method
onResize(layout, oldLayoutItem, layoutItem, placeholder) {
// `oldLayoutItem` contains the state of the item before the resize.
// You can modify `layoutItem` to enforce constraints.
const allowableW = this.props.cols[this.state.currentBreakpoint] - oldLayoutItem.x
if (layoutItem.w <= 4) {
layoutItem.w = 4;
placeholder.w = 4;
} else if (layoutItem.w <= 6) {
layoutItem.w = allowableW < 6 ? 4 : 6;
placeholder.w = allowableW < 6 ? 4 : 6;
} else {
layoutItem.w = allowableW < 12 ? 6 : 12;
placeholder.w = allowableW < 12 ? 6 : 12;
}
}
render() {
return (
<div>
...
<ResponsiveReactGridLayout
...
{/* bind method to component */}
onResize={this.onResize}
>
{this.generateDOM()}
</ResponsiveReactGridLayout>
</div>
);
}
}
ShowcaseLayout.propTypes = {
onLayoutChange: PropTypes.func.isRequired
};
ShowcaseLayout.defaultProps = {
...
// ensure your breakpoints have a minimum of 4 columns
cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 4 },
};
function generateLayout() {
return _.map(_.range(0, 25), function (item, i) {
var y = Math.ceil(Math.random() * 4) + 1;
return {
x: (_.random(0, 5) * 2) % 12,
y: Math.floor(i / 6) * y,
// set item's default width to 4
w: 4,
h: y,
i: i.toString(),
static: Math.random() < 0.05
};
});
}发布于 2021-08-12 09:47:37
我对react-grid-layout.不太了解
但是,您可以通过简单地更改cols属性ShowcaseLayout.defaultProps来完成这一任务,如下所示。
ShowcaseLayout.defaultProps = {
className: "layout",
rowHeight: 30,
onLayoutChange: function() {},
cols: { lg: 12, md: 12, sm: 6, xs: 4, xxs: 4 },
initialLayout: generateLayout()
};发布于 2021-08-12 14:41:53
是的,使用反应网格布局当然可以做到这一点。在标记为“无褐化用法/Webpack”的文档部分中,它们有以下代码片段:
import { Responsive as ResponsiveGridLayout } from 'react-grid-layout';
class MyResponsiveGrid extends React.Component {
render() {
// {lg: layout1, md: layout2, ...}
const layouts = getLayoutsFromSomewhere();
return (
<ResponsiveGridLayout className="layout" layouts={layouts}
breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
cols={{lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}}>
<div key="1">1</div>
<div key="2">2</div>
<div key="3">3</div>
</ResponsiveGridLayout>
)
}
}您可以任意修改断点,这表明屏幕的屏幕/视口宽度将考虑新的大小(例如,lg与md和sm,等等)。
定义breakpoints之后,使用cols属性定义希望每个容器位于不同断点的列数。如果您只想要您提到的三个大小-1全宽(12 Cols)、5网格(6 Cols)和⅓网格(4 cols),那么cols属性如下所示:
cols={{lg: 12, md: 6, sm: 6, xs: 4, xxs: 4}}使用这些重新定义的值,该网格中的元素将在lg视图上为全宽度,在md和sm视图上为5-网格,在xs和xxs视图上为⅓网格。
https://stackoverflow.com/questions/68668120
复制相似问题