我想为溢出的元素添加“显示更多”按钮,然后仅在单击更多鞋子后,才想显示新行

阿舒托什·库马尔

我想为溢出的元素添加“显示更多”按钮,然后仅在单击更多鞋子后,才想显示新行​​。这是我的代码:https : //codesandbox.io/s/distracted-microservice-ld267?file=/ src / App.js这是我的情况,我们需要对溢出的属性(例如颜色)进行处理,数据将动态显示对于大小,如果还有更多元素,我们需要这样做。这是我要制作的用户界面,只需选中它即可为您提供帮助

扎卡里·哈伯(Zachary Haber)

这是一个解决方案:主要工作方式是知道包含div的大小,因此我们可以将height设置为一行的高度。然后设置overflow: hidden为隐藏第一行之后的所有内容。

这使用CSS变量来允许对色块的大小进行更动态的设置(基于您在代码中显示的按钮)。每个大小按钮都会更改--sizeCSS变量以更改正方形大小。

因此height: calc(var(--size) + var(--margin) * 2);包含div的高度可根据变量动态计算高度,因此当色块因按钮按下而改变大小时,它会改变。

“显示更多” /“显示更少”按钮可切换showMore状态。

const { useState } = React;

const sizeToNumber = {
  standard: "30px",
  medium: "50px",
  small: "15px",
  large: "75px",
  "Extra Large": "100px",
  "Extra Small": "5px"
};

function App() {
  // The current size in pixels - To be used as a CSS variable
  const [size, setSize] = useState(sizeToNumber.standard);
  const onSizeChange = (val) => {
    // Convert from the string text to a pixel value
    setSize(sizeToNumber[val]);
  };

  // State for hiding and showing the extra rows
  const [showMore, setShowMore] = useState(true);

  let data = [
    {
      id: 1,
      name: "Color",
      value: [
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue"
      ]
    },
    {
      id: 2,
      name: "Size",
      value: [
        "standard",
        "medium",
        "small",
        "large",
        "Extra Large",
        "Extra Small"
      ]
    }
  ];

  return (
    // Setup --size and --margin CSS variables
    <div style={{ margin: "5px", "--size": size, "--margin": "5px" }}>
      {data.map((d) => {
        const isColor = d.name.toLowerCase().includes("color");
        const hiddenClass = isColor && !showMore ? "hide-rows" : "";
        return (
          <div key={d.id}>
            <div>{d.name}</div>
            <div
              style={{ background: "aqua" }}
              // Set a class if the extra rows should be hidden
              className={"flex " + hiddenClass}
            >
              {d.value.map((val, index) => {
                if (isColor) {
                  return (
                    <div
                      key={index}
                      // This class applies width/height/margin
                      // based on the variables
                      className="color-block"
                      style={{
                        background: val,
                        display: "inline-block"
                      }}
                    ></div>
                  );
                }
                return (
                  <button
                    key={val}
                    // Change the --size variable based on the button pressed!
                    onClick={() => onSizeChange(val)}
                    className="btn btn-primary m-2"
                  >
                    {val}
                  </button>
                );
              })}
            </div>
          </div>
        );
      })}
      <button
        className="btn btn-primary m-2"
        // Toggle showing/hiding everything
        onClick={() => setShowMore(!showMore)}
      >
        {showMore ? "Show Less" : "Show More"}
      </button>
    </div>
  );
}
ReactDOM.render(<App/>,document.getElementById('root'))
.App {
  font-family: sans-serif;
  text-align: center;
}


/* Setup a flex grid to the color-block's parent to make 
   things render consistently */

.flex {
  display: flex;
  flex-wrap: wrap;
}


/* apply margin/width/height variables to the color blocks */

.color-block {
  margin: var(--margin);
  width: var(--size);
  height: var(--size);
}


/* When the extra rows should be hidden, set the height and no overflow
   Height is calculated based on the size + (margin * 2)
   This way things can be dynamically changed, but this will remain possible to use
   since we need to know the height in order to hide things in a reasonable way*/

.hide-rows {
  height: calc(var(--size) + var(--margin) * 2);
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div id="root" />


这是相同的基本原理,将按钮移动到与颜色一致的位置,并且删除了有趣的颜色样本大小更改。

这里的主要区别是,它在颜色的div周围使用了一个flexbox跨度来使其变为“ Show More”(插入更多)按钮。

const { useState } = React;

function App() {
  // State for hiding and showing the extra rows
  const [showMore, setShowMore] = useState(false);

  let data = [
    {
      id: 1,
      name: "Color",
      value: [
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue",
        "red",
        "green",
        "yellow",
        "blue"
      ]
    },
    {
      id: 2,
      name: "Size",
      value: [
        "standard",
        "medium",
        "small",
        "large",
        "Extra Large",
        "Extra Small"
      ]
    }
  ];

  return (
    <div style={{ margin: "5px" }}>
      {data.map((d) => {
        const isColor = d.name.toLowerCase().includes("color");
        const hiddenClass = isColor && !showMore ? "hide-rows" : "";
        return (
          <div key={d.id}>
            <div>{d.name}</div>
            {/* Extra span around the flex container to make the 
                flex container and the show more button inline */}
            <span style={{ display: "flex" }}>
              <div
                style={{ background: "aqua" }}
                // Set a class if the extra rows should be hidden
                className={"flex " + hiddenClass}
              >
                {d.value.map((val, index) => {
                  if (isColor) {
                    return (
                      <div
                        key={index}
                        // This class applies width/height/margin
                        // based on the variables
                        className="color-block"
                        style={{
                          background: val,
                          display: "inline-block"
                        }}
                      ></div>
                    );
                  }
                  return (
                    <button key={val} className="btn btn-primary m-2">
                      {val}
                    </button>
                  );
                })}
              </div>
              {/* Only display the button on the color section */}
              {/* It is super annoying to try and calculate to determine
                  if there's a need for the show more button.
                  Though doable if you know the size ahead of time*/}
              {isColor && (
                <button
                  className="btn btn-primary"
                  // The button needs to align itself to the top rather than default stretch
                  style={{ alignSelf: "flex-start", marginLeft: "5px" }}
                  // Toggle showing/hiding everything
                  onClick={() => setShowMore(!showMore)}
                >
                  {showMore ? "Show Less" : "Show More"}
                </button>
              )}
            </span>
          </div>
        );
      })}
    </div>
  );
}

ReactDOM.render(<App />,document.getElementById('root'))
.App {
  font-family: sans-serif;
  text-align: center;
}


/* Setup a flex grid to the color-block's parent to make 
   things render consistently */

.flex {
  display: flex;
  flex-wrap: wrap;
  /* Setup --size and --margin CSS variables */
  --size: 30px;
  --margin: 5px;
}


/* apply margin/width/height variables to the color blocks */

.color-block {
  margin: var(--margin);
  width: var(--size);
  height: var(--size);
}


/* When the extra rows should be hidden, set the height and no overflow
   Height is calculated based on the size + (margin * 2)
   This way things can be dynamically changed, but this will remain possible to use
   since we need to know the height in order to hide things in a reasonable way*/

.hide-rows {
  height: calc(var(--size) + var(--margin) * 2);
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div id="root" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

单击后显示更多隐藏按钮

来自分类Dev

Android:单击按钮后如何显示更多视图

来自分类Dev

添加显示更多显示更少按钮

来自分类Dev

单击加载更多按钮后如何显示加载更多页面的整个源代码?

来自分类Dev

单击按钮jquery时显示更多div

来自分类Dev

单击按钮时显示更多或更少

来自分类Dev

如何使jQuery先显示1个数据,然后在单击后显示更多数据,再显示1个

来自分类Dev

单击“更多”按钮后如何显示数据库的全部内容

来自分类Dev

按钮在javascript中单击后如何显示“没有更多数据”消息

来自分类Dev

单击“阅读更多”后如何在我的代码上添加“阅读更少”按钮?

来自分类Dev

表单仅在单击提交按钮后显示

来自分类Dev

仅在单击按钮时显示div元素

来自分类Dev

如何在按钮单击上添加更多元素

来自分类Dev

我想在我的odoo中添加“显示更多评论..”按钮| openerp模块

来自分类Dev

无法单击以显示更多文本

来自分类Dev

显示/隐藏Div-并在单击时隐藏“更多”按钮

来自分类Dev

在C ++中显示总和,然后添加更多总和cpp

来自分类Dev

单击以显示更多(按钮)后如何将主题列表限制为5(项)仅显示3(项)

来自分类Dev

div 内按钮的“显示更多”链接(多个显示更多按钮)

来自分类Dev

不显示将在jQuery按钮单击中添加新行

来自分类Dev

200个字符后显示更多阅读按钮

来自分类Dev

点击显示更多元素

来自分类Dev

按按钮显示更多而更少显示

来自分类Dev

单击按钮后如何显示元素?

来自分类Dev

实施“加载更多”按钮以在单击时加载更多元素

来自分类Dev

实施“加载更多”按钮以在单击时加载更多元素

来自分类Dev

默认情况下,“阅读更多”描述不隐藏,而是仅在单击按钮后

来自分类Dev

显示更多/显示更少ng有关按钮的数据,请单击

来自分类Dev

当我单击JavaScript中的按钮后,为什么显示该值然后消失?

Related 相关文章

  1. 1

    单击后显示更多隐藏按钮

  2. 2

    Android:单击按钮后如何显示更多视图

  3. 3

    添加显示更多显示更少按钮

  4. 4

    单击加载更多按钮后如何显示加载更多页面的整个源代码?

  5. 5

    单击按钮jquery时显示更多div

  6. 6

    单击按钮时显示更多或更少

  7. 7

    如何使jQuery先显示1个数据,然后在单击后显示更多数据,再显示1个

  8. 8

    单击“更多”按钮后如何显示数据库的全部内容

  9. 9

    按钮在javascript中单击后如何显示“没有更多数据”消息

  10. 10

    单击“阅读更多”后如何在我的代码上添加“阅读更少”按钮?

  11. 11

    表单仅在单击提交按钮后显示

  12. 12

    仅在单击按钮时显示div元素

  13. 13

    如何在按钮单击上添加更多元素

  14. 14

    我想在我的odoo中添加“显示更多评论..”按钮| openerp模块

  15. 15

    无法单击以显示更多文本

  16. 16

    显示/隐藏Div-并在单击时隐藏“更多”按钮

  17. 17

    在C ++中显示总和,然后添加更多总和cpp

  18. 18

    单击以显示更多(按钮)后如何将主题列表限制为5(项)仅显示3(项)

  19. 19

    div 内按钮的“显示更多”链接(多个显示更多按钮)

  20. 20

    不显示将在jQuery按钮单击中添加新行

  21. 21

    200个字符后显示更多阅读按钮

  22. 22

    点击显示更多元素

  23. 23

    按按钮显示更多而更少显示

  24. 24

    单击按钮后如何显示元素?

  25. 25

    实施“加载更多”按钮以在单击时加载更多元素

  26. 26

    实施“加载更多”按钮以在单击时加载更多元素

  27. 27

    默认情况下,“阅读更多”描述不隐藏,而是仅在单击按钮后

  28. 28

    显示更多/显示更少ng有关按钮的数据,请单击

  29. 29

    当我单击JavaScript中的按钮后,为什么显示该值然后消失?

热门标签

归档