简体   繁体   English

动态定位React JSX按钮

[英]Dynamic positioning of a React JSX button

I am working with React and I need put this button like that: 我正在使用React,我需要像这样按下这个按钮:

在此输入图像描述

but I have a big problem because I am forbidden to use margin or other form of positional where I have values with px, or rem, basicaly static positional. 但是我有一个很大的问题,因为我被禁止使用保证金或其他形式的位置,我有px值,或者rem,basicaly静态位置。 Now, I have this code 现在,我有这个代码

<div style={{ float: 'right' }}>
      <ActionButton style={{
        display : 'flex', 
        flex : '1',
        flexDirection: 'row',
        alignItems : 'center', 
        justifyContent : 'center',
        height: '40px',
        width: '151px',

      }} name={<div style = {{
        display : 'flex',
        flex : '2',
      backgroundColor : 'red', 
      float : 'right'
    }}>FILTRAR</div>} 
      icon={<div style = {{
        display : 'flex',
        flex : '2',
      backgroundColor : 'blue',
      width: '24px',
      height: '24px',
      float : 'left'}}><FilterIcon/></div>}>
      </ActionButton>
    </div>

And now my button is like that 而现在我的按钮就是这样

在此输入图像描述 在此输入图像描述

您可以使用百分比(即:50%),然后使用百分比变换(即:transform:translateX(-50%))来调整位置。如果您可以提供静态可设置样式的html,我可以为您生成一个示例。

Parent's styles should already align children (name and icon) by the center, not by the top. 父级的样式应该已经将孩子(姓名和图标)与中心对齐,而不是顶部。 And yes, no positioning styles are required, unless there are hardcoded heigh/margin values in children itself. 是的,除非儿童本身有硬编码的高度/边距值,否则不需要定位样式。

Desired behavior can be broken because of the implementation of ActionButton . 由于ActionButton的实现,可能会破坏所需的行为。

Anyway, my suggestions to tackle this issue: 无论如何,我的建议是解决这个问题:

  1. use flex-direction: 'row-reverse' instead of float s; 使用flex-direction: 'row-reverse'而不是float s;
  2. remove display: 'flex' from child elements. 从子元素中删除display: 'flex'

Here is a simplified and working example for web https://codesandbox.io/s/62nynm1wzk , hope it can help you to get started. 以下是web https://codesandbox.io/s/62nynm1wzk的简化实用示例,希望它可以帮助您入门。

Update: here is an example using button instead of div as container, since it is closer to original markup: https://codesandbox.io/s/y7q1vlwkwj 更新:这是一个使用button而不是div作为容器的示例,因为它更接近原始标记: https//codesandbox.io/s/y7q1vlwkwj

Try to remove the flex and display style property in name and icon props like below and check it. 尝试删除名称和图标道具中的flex显示样式属性,如下所示并检查它。 Because flex:2 will change the width of child element with equal spacing. 因为flex:2将以相等的间距更改子元素的宽度。

<div style={{ float: 'right' }}>
      <ActionButton style={{
        display : 'flex', 
        flex : '1',
        flexDirection: 'row',
        alignItems : 'center', 
        justifyContent : 'center',
        height: '40px',
        width: '151px',

      }} name={<div style = {{
      backgroundColor : 'red', 
      float : 'right'
    }}>FILTRAR</div>} 
      icon={<div style = {{
      backgroundColor : 'blue',
      width: '24px',
      height: '24px',
      float : 'left'}}><FilterIcon/></div>}>
      </ActionButton>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM