【问题标题】:div to take remaining space whose neighbor has transform translatediv 占用邻居已转换的剩余空间
【发布时间】:2018-05-26 06:05:18
【问题描述】:

我有两个 div,如下图所示:

在上图中:

  • 灰色矩形是容器 div。
  • 白色矩形是一个带有变换的 div: translateX(somevalue), 这个矩形是可拖动的。所以,在拖拽的时候它的 translateX 和 translateY 会在代码中改变。
  • 蓝色矩形是一个应该占据所有可用宽度的 div。它的高度是固定的。

我没有任何代码,因为我正在尝试使用 react-draggable 进行反应,它使用 translateX 和 translateY 来拖动元素。

我想要一个纯 css 解决方案,因为我不想仅仅为了增加 div 的宽度而重新渲染组件。

注意:

出于演示目的,蓝色矩形被视为 div,只是为了使示例更易于理解。在实际项目中,蓝色框将是一条线,在那条线上我将放置容器左边缘和移动对象左边缘之间的距离。像尺子一样的东西。

更新:

这是我的代码:

<div className="cover-print-area-settings-container">
  <div style={{ background: 'black', height: 10, width: 'auto' }} />
  <Draggable
    bounds={{
      left: 0,
      top: 0,
      right: 300,
      bottom: 150
    }}
  >
    <div id="cover-print-area-draggable-content" className="cover-print-area-settings-content">
      <div className="contact-name">Contact Name</div>
      <div className="side-by-side-start-center">
        <PhoneIcon className="contact-detail-icon" />
        <span>Mobile No</span>
      </div>
      <div className="side-by-side-start-center email-wrapper">
        <EmailIcon className="contact-detail-icon" />
        <span>Email</span>
      </div>
      <div className="side-by-side-start-center">
        <LocationIcon className="contact-detail-icon contact-location-icon" />
        <div className="contact-address-wrapper">
          <p className="contact-address"> Address Line 1 </p>
          <span className="contact-address-details"> Address Line 2 </span>
        </div>
      </div>
    </div>
  </Draggable>
</div>

代码中的可拖动元素是从react-draggable 导入的。这里是 react-draggable 的文档:https://github.com/mzabriskie/react-draggable

上述html相关的css如下:

  .cover-print-area-settings-container
    height: 300px;
    width: 600px;
    overflow: auto
    // height: 100%
    background-color: #fff8dc
    .cover-print-area-settings-content
      border: 1px solid black
      display: inline-block
      vertical-align: top
      .contact-name
        font-size: 36px
        text-align: left    
      .contact-detail-icon
        margin-right: 7px
      .contact-location-icon
        padding-top: 12px
        align-self: flex-start
      .email-wrapper
        margin-top: 5px;
        margin-bottom: -8px;
      .contact-address-wrapper
        display: flex
        flex-direction: column
        justify-content: flex-start 
        .contact-address
          text-align: left
          align-self: flex-start
        .contact-address-details
          margin-top: -12px
          text-align: left
          align-self: flex-start

【问题讨论】:

  • 我不是 CSS 翻译方面的专家,但鉴于 CSS 是元素的一个属性,我看不出你如何在没有 Javascript mousedown、mouseup 和 mousemove 处理的情况下操作 CSS:translateX .
  • 这个蓝色元素是否包含内容?无论如何,您都需要共享代码以证明您的问题和失败。另见stackoverflow.com/help/mcve
  • @G-Cyr 不,蓝色元素不包含任何内容。它是一个仅用于演示目的的 div。这将是实际项目中的一条线,我将在其上显示拖动的框与容器左边缘之间的距离,以便用户知道他移动了多少框。
  • 它会是一把尺子吗? ,css可能会有所帮助,但您需要澄清问题:(
  • @G-Cyr 我已经开始工作了。我今晚可能会用我的答案更新这个问题。感谢您的帮助。

标签: css


【解决方案1】:

在这种情况下,我会考虑 CSS 变量,您只需调整将用于翻译的变量和宽度:

.container {
  padding:20px 0;
  height:100px;
  background:grey;
}

.blue {
  height:100%;
  background:blue;
  display:inline-block;
  width:var(--c);
  margin-right:calc(var(--c) * -1);
}
.white {
height:100%;
 width:100px;
  background:white;
  display:inline-block;
  transform:translateX(var(--c));
}
<div class="container" style="--c:50px">
<div class="blue"></div><div class="white"></div>
</div>
<div class="container" style="--c:100px">
<div class="blue"></div><div class="white"></div>
</div>
<div class="container" style="--c:200px">
<div class="blue"></div><div class="white"></div>
</div>

另一种想法是将蓝色部分视为白框的伪元素,并且更容易考虑 X/Y 平移:

.container {
  padding:20px 0;
  height:100px;
  background:grey;
  overflow:hidden;
}

.white {
height:100%;
 width:100px;
  background:white;
  display:inline-block;
  transform:translateX(var(--c));
  position:relative;
}
.white:before {
  content:"";
  position:absolute;
  top:0;
  left:-100vw;
  right:100%;
  bottom:0;
  background:blue;
}
<div class="container" style="--c:50px">
<div class="blue"></div><div class="white"></div>
</div>
<div class="container" style="--c:100px">
<div class="blue"></div><div class="white"></div>
</div>
<div class="container" style="--c:200px">
<div class="blue"></div><div class="white"></div>
</div>

【讨论】:

  • 我已经尝试了您的第二个解决方案,它就像一个魅力。谢谢您的帮助。你能解释一下left: -100vw在伪元素之前的需要吗?
  • @Vishal 有我的演示,codepen.io/gc-nomade/pen/dexbad 我使用绝对伪的正确 100%,所以它粘在左侧 ... ;)
  • @Vishal vw 是视口宽度,所以 100vw 是屏幕宽度的 100%,所以我确信它会覆盖所需的空间;)
  • @G-Cyr 你从我这里抄来的! :p
猜你喜欢
  • 2013-02-22
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 2020-06-14
  • 2013-03-09
  • 1970-01-01
  • 2017-07-03
  • 2019-10-08
相关资源
最近更新 更多