【问题标题】:How to prevent image icon from being cut off?如何防止图像图标被切断?
【发布时间】:2022-01-06 21:01:24
【问题描述】:

我在 React 中创建了一个时间线组件,并尝试在时间线上包含一个图标。然而,由于某种原因,它被切断了,并且只在两边显示了一半的图标。我知道这是一个溢出问题,但我无法确定它。这是它目前的样子:

当我添加溢出时:可见。它确实显示了完整的图标,但时间线部分由于其长度而延伸到下一部分。

这就是我添加溢出时发生的情况:可见。

如您所见,它正在侵犯联系人。

如何完全显示图标,又不让时间线部分侵占其他部分?

这是我目前所拥有的。

TimelineItem.jsx

import {Person, Mail} from "@material-ui/icons"

const TimelineItem = ({ data }) => (
    <div className="timeline-item">
        <div className="timeline-item-content">
            <span className="tag" style={{ background: data.category.color }}>
                {data.category.tag}
            </span>
            <time>{data.date}</time>
            <p>{data.text}</p>
            {data.link && (
                <a
                    href={data.link.url}
                    target="_blank"
                    rel="noopener noreferrer"
                >
                    {data.link.text}
                </a>
            )}
            <div className="itemContainer">
            <Mail classname="icon" />
            </div>
            {/* <span className="circle"/> */}
        </div>
    </div>
);

export default TimelineItem;

时间线.jsx

import TimelineData from './TimelineData';
import TimelineItem from './TimelineItem'
import './timeline.scss'


export default function Timeline() {
    return (
        <div className="reactTimeline">
            <h1>Satyen Singh Timeline</h1>
            <div className="timeline-container" id="timeline">
                {TimelineData.map((data, idx) => (
                    <TimelineItem data={data} key={idx} />
                ))}
            </div>
        </div>
    )
}

时间线.scss

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
//   background-color: white;
  overflow-x: visible;
}



h1 {
    text-align: center;
}

body {
    // overflow: visible;

//   line-height: 1.5;
}
.timeline-container {
    display: flex;
    flex-direction: column;
    position: relative;
    margin: 40px 0;
    // overflow-x: visible;
  }
  .timeline-container::after {
    background-color: #e17b77;
    content: '';
    position: absolute;
    left: calc(50% - 2px);
    width: 4px;
    height: 100%;
    // overflow-x: visible;
  }
  
  .timeline-item {
    display: flex;
    justify-content: flex-end;
    padding-right: 30px;
    position: relative;
    margin: 10px 0;
    width: 50%;
    // overflow-x: visible;
  }
  .timeline-item:nth-child(odd) {
    align-self: flex-end;
    justify-content: flex-start;
    padding-left: 30px;
    padding-right: 0;
    // overflow-x: visible;
  }
  
  .timeline-item-content {
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    padding: 15px;
    position: relative;
    width: 400px;
    max-width: 70%;
    text-align: right;
    overflow-x: visible
  }
  .timeline-item-content::after {
    content: ' ';
    background-color: #fff;
    box-shadow: 1px -1px 1px rgba(0, 0, 0, 0.2);
    position: absolute;
    right: -7.5px;
    top: calc(50% - 7.5px);
    transform: rotate(45deg);
    width: 15px;
    height: 15px;
    // overflow-x: visible
  }
  .timeline-item:nth-child(odd) .timeline-item-content {
    text-align: left;
    align-items: flex-start;
    // overflow-x: visible
  }
  .timeline-item:nth-child(odd) .timeline-item-content::after {
    right: auto;
    left: -7.5px;
    box-shadow: -1px 1px 1px rgba(0, 0, 0, 0.2);
    overflow-x: visible
  }
  
  .timeline-item-content .tag {
    color: #fff;
    font-size: 12px;
    font-weight: bold;
    top: 5px;
    left: 5px;
    letter-spacing: 1px;
    padding: 5px;
    position: absolute;
    text-transform: uppercase;
    overflow-x: visible;
  }
  .timeline-item:nth-child(odd) .timeline-item-content .tag {
    left: auto;
    right: 5px;
    overflow: visible;
  }
  .timeline-item-content time {
    color: #777;
    font-size: 12px;
    font-weight: bold;
  }
  .timeline-item-content p {
    font-size: 16px;
    line-height: 24px;
    margin: 15px 0;
    max-width: 250px;
  }
  .timeline-item-content a {
    font-size: 14px;
    font-weight: bold;
  }
  .timeline-item-content a::after {
    content: ' ►';
    font-size: 12px;
    overflow: visible;
  }
  .timeline-item-content .itemContainer {
    position: absolute;
    top: calc(50% - 10px);
    right: -40px;
    width: 20px;
    height: 20px;
    z-index: 100;
    overflow-x: visible;
  }
  .timeline-item:nth-child(odd) .timeline-item-content .itemContainer {
    right: auto;
    left: -40px;
    overflow-x: visible;
  }

【问题讨论】:

  • 为联系人添加 z-index
  • 嗨@JunaidShaikh,你是说.timeline-item-content 吗?我应该将 z-index 设置为什么?
  • 底部的蓝色条
  • 尝试将 z-index 设置为 1
  • 你能在沙箱中重现这段代码吗

标签: javascript html css reactjs frontend


【解决方案1】:

它正在工作.. 看看,需要一些 css 问题来溢出

.moreSec {
  padding: 150px 0;
  font-size: 50px;
  text-align: center;
  background: antiquewhite;
}

.timeline-container {
  display: flex;
  flex-direction: column;
  position: relative;
  padding: 0 0 40px;
  background: beige;
  overflow: hidden;
  margin-top:20px;
}

.timeline-container::after {
  background-color: #e17b77;
  content: '';
  position: absolute;
  left: calc(50% - 2px);
  width: 4px;
  height: 100%;
  ;
}

.timeline-item {
  display: flex;
  justify-content: flex-end;
  padding-right: 30px;
  position: relative;
  margin: 10px 0;
 width: 45%;
    max-width: 100%;
}

.timeline-item:nth-child(odd) {
  align-self: flex-end;
  justify-content: flex-start;
  padding-left: 30px;
  padding-right: 0;
  ;
}

.timeline-item-content {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
  border-radius: 5px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 15px;
  position: relative;
  width: 400px;
  max-width: 70%;
  text-align: right;
  overflow-x: visible
}

.timeline-item-content::after {
  content: ' ';
  background-color: #fff;
  box-shadow: 1px -1px 1px rgba(0, 0, 0, 0.2);
  position: absolute;
  right: -7.5px;
  top: calc(50% - 7.5px);
  transform: rotate(45deg);
  width: 15px;
  height: 15px;
}

.timeline-item:nth-child(odd) .timeline-item-content {
  text-align: left;
  align-items: flex-start;
}

.timeline-item:nth-child(odd) .timeline-item-content::after {
  right: auto;
  left: -7.5px;
  box-shadow: -1px 1px 1px rgba(0, 0, 0, 0.2);
  overflow-x: visible
}

.timeline-item-content .tag {
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  top: 5px;
  left: 5px;
  letter-spacing: 1px;
  padding: 5px;
  position: absolute;
  text-transform: uppercase;
  overflow-x: visible;
}

.timeline-item:nth-child(odd) .timeline-item-content .tag {
  left: auto;
  right: 5px;
  overflow: visible;
}

.timeline-item-content time {
  color: #777;
  font-size: 12px;
  font-weight: bold;
}

.timeline-item-content p {
  font-size: 16px;
  line-height: 24px;
  margin: 15px 0;
  max-width: 250px;
}

.timeline-item-content a {
  font-size: 14px;
  font-weight: bold;
}

.timeline-item-content a::after {
  content: ' ►';
  font-size: 12px;
  overflow: visible;
}

.timeline-item-content .itemContainer {
  position: absolute;
  top: calc(50% - 10px);
  right: -40px;
  width: 20px;
  height: 20px;
  z-index: 100;
  overflow-x: visible;
}

.timeline-item:nth-child(odd) .timeline-item-content .itemContainer {
  right: auto;
  left: -40px;
  overflow-x: visible;
}
.timeline-item-content .icon {
    background: #ffffff;
    border-radius: 100%;
    box-shadow: 0 0 5px rgb(0 0 0 / 30%);
    height: 25px;
    width: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 23px;
}
<div class="reactTimeline">
  <h1>Timeline</h1>
  <div class="timeline-container" id="timeline">
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <!-- <Mail class="icon" /> -->
          <span class="icon">✉</span>
        </div>
      </div>
    </div>
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <!-- <Mail class="icon" /> -->
          <span class="icon">✉</span>
        </div>
      </div>
    </div>
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <!-- <Mail class="icon" /> -->
          <span class="icon">✉</span>
        </div>
      </div>
    </div>
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <span class="icon">✉</span>

        </div>
      </div>
    </div>
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <span class="icon">✉</span>

        </div>
      </div>
    </div>
    <div class="timeline-item">
      <div class="timeline-item-content">
        <span class="tag" style="background-color: aqua;">
                        urgent
                    </span>
        <time>24/05/2022</time>
        <p>dummy text fnejfenjk ehfuheufh, dummy text fnejfenjk ehfuheufh</p>

        <a href=# target="_blank" rel="noopener noreferrer">
                            Read More
                        </a>
        <div class="itemContainer">
          <span class="icon">✉</span>

        </div>
      </div>
    </div>
  </div>
</div>
<div class="moreSec">
  Next Section
</div>

【讨论】:

猜你喜欢
  • 2013-06-03
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多