【问题标题】:Overlap Button on Buttons using Styled Components使用样式化组件的按钮上的重叠按钮
【发布时间】:2022-01-20 16:42:17
【问题描述】:

我在将按钮重叠在另一个按钮上时遇到问题。 我想关注我附在下面的照片

代码沙盒CLICK HERE

const MainButton = styled.button`
  border-radius: 50%;
  border: 2px solid red;
  position: relative;
  background-color: #fff;
  display: block;
  width: 40px;
  height: 40px;
  position: relative;
  margin-bottom: 0.5rem;
  overflow: hidden;
  & > img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
`;

const IconButton = styled.button`
  border-radius: 50%;
  border: 2px solid red;
  position: absolute;
  background-color: #fff;
  display: block;
  width: 22px;
  height: 22px;
  position: relative;
  margin-bottom: 0.5rem;
  overflow: hidden;
  & > img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
`;

【问题讨论】:

    标签: css reactjs styled-components


    【解决方案1】:

    为了相对于MainButton 绝对定位IconButton,它需要呈现为MainButton 的子级。

    <MainButton type="button">
      <img
        src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"
        alt="test"
      />
      <IconButton>+</IconButton>
    </MainButton>
    

    接下来调整IconButton的定位。

    const IconButton = styled.span`     // <-- use span since nesting button is invalid
      background-color: #fff;
      border-radius: 50%;
      border: 2px solid red;
      cursor: pointer;
      position: absolute;
      display: block;
      width: 22px;
      height: 22px;
      line-height: 22px;                // <-- center text vertically
      position: relative;
      margin-bottom: 0.5rem;
      overflow: hidden;
      left: 50%;                        // <-- position halfway across parent
      bottom: 0;                        // <-- position at bottom of parent
      transform: translate(-50%, -50%); // <-- translate to center button
    
      & > img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    `;
    

    删除父 MainButton 上的 overflow: hidden; 规则。

    更新

    将图像移动为背景图像并调整位置。

    const MainButton = styled.button`
      border-radius: 50%;
      border: 2px solid red;
      position: relative;
      background-color: #fff;
      background-image: ${({ src }) => `url(${src});`}
      background-position: center center;
      background-size: contain;
      display: block;
      width: 40px;
      height: 40px;
      position: relative;
      margin-bottom: 0.5rem;
      cursor: pointer;
      & > img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    `;
    
    const IconButton = styled.div`
      background-color: #fff;
      border-radius: 50%;
      border: 2px solid red;
      cursor: pointer;
      position: absolute;
      display: block;
      width: 22px;
      height: 22px;
      line-height: 22px;
      position: relative;
      margin-bottom: 0.5rem;
      overflow: hidden;
      text-align: center;
      user-select: none;
      left: 50%;
      top: 100%;
      transform: translate(-50%, -50%);
    
      & > img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    `;
    

    【讨论】:

    • 谢谢德鲁。我分叉了您的代码框,并更改了图像标签中的“src”。问题是,它超出了&lt;MainButton&gt;。它应该只是在 &lt;MainButton&gt; codesandbox.io/s/…
    • @Joseph 好的,我将图像移动为按钮的背景图像并更新了 (+) 装饰器的位置。
    猜你喜欢
    • 2021-09-06
    • 2020-06-12
    • 1970-01-01
    • 2020-10-20
    • 2020-10-17
    • 2020-04-17
    • 2015-11-04
    • 2018-11-06
    • 2021-02-22
    相关资源
    最近更新 更多