【问题标题】:Toggle button / decrease width grid and div Semantic React切换按钮/减小宽度网格和 div 语义反应
【发布时间】:2020-01-08 18:09:47
【问题描述】:

我有一个有大小的菜单,当您单击按钮时,它会将宽度减小到 50 像素

我会有一个带有按钮和图标的菜单,当点击按钮时只会出现图标

但是我很难减少我的 div 的宽度以及它如何在语义网格上工作

代码:

function Menu() {
  const [open, setOpen] = useState(true); // declare new state variable "open" with setter
  const handleClick = e => {
    e.preventDefault();
    setOpen(!open);
  };
  return (
    <Grid style={{background: '#eee'}}>

    <Grid.Column computer={2} tablet={4} mobile={5} style={{background: '#000', padding:'0', height:'100vh'}}>

    <div style={{background:'#000', width:'100%', height:'100%'}}>

    </div>
    </Grid.Column>
    <Grid.Column width={14} style={{background: '#eee', padding:'0'}}>
          <Button icon onClick={handleClick}>
            <Icon name="align justify" />
          </Button>
    </Grid.Column>
   </Grid>
  );
}

css:

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#root,
.App,
.ui.grid{
  height: 100vh !important;
  margin: 0 !important;
  padding:0 !important;
}

代码:https://codesandbox.io/s/cool-kepler-cxj4x

【问题讨论】:

  • 这是你想要的吗? codesandbox.io/s/goofy-silence-63gi8
  • 请让我知道您是否想要一些特定的东西来做到这一点。
  • 是的,就是这样,但我正在为网格绞尽脑汁,因为内容不仅要适合菜单
  • 太棒了!!!...让我在这里发布答案...同时发布关于另一个问题的答案stackoverflow.com/a/59639573/5783700
  • 是啊,非常感谢

标签: css reactjs semantic-ui


【解决方案1】:

当点击按钮打开状态改变时,您可以减小div的宽度,请查看demo link

App.js

import React, { useState } from "react";
import { Grid, Button, Icon } from "semantic-ui-react";
import "./style.css";

function Menu() {
  const [open, setOpen] = useState(true); // declare new state variable "open" with setter

  const handleClick = e => {
    e.preventDefault();
    setOpen(!open);
  };

  return (
    <Grid style={{ background: "#eee" }}>
      <Grid.Column
        computer={2}
        tablet={4}
        mobile={5}
        style={{
          background: "#000",
          padding: "0",
          height: "100vh"
        }}
      >
        <div
          style={{
            background: "red",
            width: open ? "100%" : "calc(100% - 50px)",
            height: "100vh"
          }}
        />
      </Grid.Column>
      <Grid.Column
        computer={14}
        tablet={12}
        mobile={11}
        style={{ background: "#eee", padding: "0" }}
      >
        <Button icon onClick={handleClick}>
          <Icon name="align justify" />
        </Button>
      </Grid.Column>
    </Grid>
  );
}
export default Menu;

【讨论】:

  • 据我所见,我必须更改网格的宽度,以便内容也移动到侧面而不仅仅是菜单?
  • @MykonSpt,请参考链接codesandbox.io/s/runtime-flower-qdqjw
  • 其实我们提供了网格电脑、平板、手机属性的宽度,所以在按钮点击时,我们无法改变它的原始宽度,所以我提供了一种解决方案来减少宽度。
  • 谢谢兄弟,我想我明白了。如果我有任何问题可以问你吗?
  • 我的第一个问题是有没有可能使用@media only screen and (max-width: 767px) and (min-width: 320px) no styled?基本上我在考虑它打开时我会为每种屏幕类型提供一定的宽度,而当它也使用媒体关闭时,或者最好使用响应式并为每种类型都做
猜你喜欢
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
相关资源
最近更新 更多