【问题标题】:Dynamically setting CSS property left using width calculation Angular 6使用宽度计算Angular 6动态设置CSS属性左侧
【发布时间】:2018-11-19 12:23:44
【问题描述】:

我目前正在构建一个 CSS 音频播放器,我正在设置一个 div 的宽度以使用如下 [style] 来表示音频的当前进度,效果非常好:

<div class="player-progress-current" [style.width.%]="(currentTime * 100)/duration"></div>

我还想通过设置另一个类的 'left' CSS 属性在上面的进度 div 的末尾画一个小圆圈。这将是英文:

(Parent Width px) - (Progress Width px)

我尝试过使用 calc() 函数,但它不喜欢它,并且计算出的百分比不知道使用我认为的宽度......

<div class="player-progress-handle" [style.left.px]="calc(100% - (currentTime * 100)/duration"></div>

CSS 类是:

.player-progress-current {
    width: 100%;
    height: 1px;
    background-color: red;
}

.player-progress-handle {
    position: relative;
    bottom: 1px;
    border: 1px solid #f50;
    border-radius: 100%;
    height: 8px;
    width: 8px;
    background-color: #f50;
    box-sizing: border-box;
    margin-top: -4px;
    margin-left: -4px;
 }

任何想法如何做到这一点的最佳方式是?我确定我可以找到一种 hacky 方式,但希望找到正确的方式

【问题讨论】:

    标签: css angular angular6 ng-style


    【解决方案1】:

    您可以使用:after 作为句柄并摆脱计算:

    .player-progress-current {
      position: relative;
      width: 50%;
      height: 1px;
      margin: 20px 0;
      background: red;
    }
    
    .player-progress-current:after {
      content: '';
      position: absolute;
      right:-3px; bottom: 1px;
      border: 1px solid #f50;
      border-radius: 55%;
      height: 8px;
      width: 8px;
      background-color: #f50;
      box-sizing: border-box;
    }
    &lt;div class="player-progress-current" [style.width.%]="(currentTime * 100)/duration"&gt;&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2012-04-09
      • 2013-07-22
      • 2021-09-13
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多