typescript 如何改变CSS值在Angular 15+当复选框被选中?

xxe27gdn  于 2023-03-04  发布在  TypeScript
关注(0)|答案(1)|浏览(124)

我希望在选中特定复选框时更改垂直时间轴的高度。例如,在CSS组件中,选中复选框时应更改高度%

.timeline:before {
  top: 0;
  bottom: 0;
  position: absolute;
  content: " ";
  width: 10px;
  height:100%;
  background-color: #ccc;
  transition: all 0.3s;
  left: 49.7%;
  margin-left: -1.5px; 
}

这是我的HTML组件代码

<div class="container">
    <div class="page-header">
        <h1 id="timeline">Timeline</h1>
        <ul>
          <li><input type="checkbox">height 8%</li>
          <li><input type="checkbox">height 25%</li>
          <li><input type="checkbox">height 38%</li>
          <li><input type="checkbox">height 53%</li>
          <li><input type="checkbox">height 72%</li>
          <li><input type="checkbox">height 100%</li>
        </ul>
    </div>
    <ul class="timeline">
        <li>
          <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
          <div class="timeline-panel">
            <p>Sponsor Approval</p>
          </div>
        </li>
        <li class="timeline-inverted">
          <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
          <div class="timeline-panel">
            <p>Initial Payment</p>
          </div>
        </li>
        <li>
          <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
            <div class="timeline-panel">
            <p>HR Approved Date</p> 
            </div>
        </li>
        <li class="timeline-inverted">
            <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
            <div class="timeline-panel">
            <p>Manager Approved Date</p>
            </div>
        </li>
        <li>
          <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
            <div class="timeline-panel">
            <p>Letter of intent</p> 
            </div>
        </li>
        <li class="timeline-inverted">
          <div class="timeline-badge"><button><i class="fa fa-times"></i></button></div>
          <div class="timeline-panel">
          <p>Final Offer Leter</p>
          </div>
        </li>
    </ul>

这是我的CSS组件代码

h1{
    text-align: center;
}
.timeline {
    list-style: none;
    padding: 20px 0 20px;
    position: relative;
}
button{
    border: none;
    background-color: transparent;
}
p{
    text-align: center;
}
.timeline:before {
  top: 0;
  bottom: 0;
  position: absolute;
  content: " ";
  width: 10px;
  height:100%;
  background-color: #ccc;
  transition: all 0.3s;
  left: 49.7%;
  margin-left: -1.5px; 
}
.h8{
  height: 8%;
}

.timeline > li {
  margin-bottom: 2cm;
  position: relative;
}

.timeline > li:before,
.timeline > li:after {
  content: " ";
  display: table;
}

.timeline > li:after {
  clear: both;
}

.timeline > li:before,
.timeline > li:after {
  content: " ";
  display: table;
}

.timeline > li:after {
  clear: both;
}

.timeline > li > .timeline-panel {
  width: 46%;
  height: 2cm;
  float: left;
  border: 1px solid #d4d4d4;
  border-radius: 25px;
  padding: 20px;
  position: relative;
  -webkit-box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
}

.timeline > li > .timeline-panel:before {
  position: absolute;
  top: 26px;
  right: -15px;
  display: inline-block;
  border-top: 15px solid transparent;
  border-left: 15px solid #ccc;
  border-right: 0 solid #ccc;
  border-bottom: 15px solid transparent;
  content: " ";
}

.timeline > li > .timeline-panel:after {
  position: absolute;
  top: 27px;
  right: -14px;
  display: inline-block;
  border-top: 14px solid transparent;
  border-left: 14px solid #fff;
  border-right: 0 solid #fff;
  border-bottom: 14px solid transparent;
  content: " ";
}

.timeline > li > .timeline-badge {
  color: #fff;
  width: 50px;
  height: 50px;
  line-height: 50px;
  font-size: 1.4em;
  text-align: center;
  position: absolute;
  top: 16px;
  left: 50%;
  margin-left: -25px;
  background-color: #999999;
  z-index: 100;
  border-top-right-radius: 50%;
  border-top-left-radius: 50%;
  border-bottom-right-radius: 50%;
  border-bottom-left-radius: 50%;
}

.timeline > li.timeline-inverted > .timeline-panel {
  float: right;
}

.timeline > li.timeline-inverted > .timeline-panel:before {
  border-left-width: 0;
  border-right-width: 15px;
  left: -15px;
  right: auto;
}

.timeline > li.timeline-inverted > .timeline-panel:after {
  border-left-width: 0;
  border-right-width: 14px;
  left: -14px;
  right: auto;
}


.timeline-title {
    margin-top: 0;
    color: inherit;
}

.timeline-body > p,
.timeline-body > ul {
    margin-bottom: 0;
}

.timeline-body > p + p {
  margin-top: 5px;
}

@media (max-width: 767px) {
  ul.timeline:before {
    left: 40px;
  }

    ul.timeline > li > .timeline-panel {
        width: calc(100% - 90px);
        width: -moz-calc(100% - 90px);
        width: -webkit-calc(100% - 90px);
    }

    ul.timeline > li > .timeline-badge {
        left: 15px;
        margin-left: 0;
        top: 16px;
    }

    ul.timeline > li > .timeline-panel {
        float: right;
    }

        ul.timeline > li > .timeline-panel:before {
          
            border-left-width: 0;
            border-right-width: 15px;
            left: -15px;
            right: auto;
        }

        ul.timeline > li > .timeline-panel:after {
            border-left-width: 0;
            border-right-width: 14px;
            left: -14px;
            right: auto;
        }
}
ee7vknir

ee7vknir1#

据我所知,checkbox不是适合这项工作的工具。checkbox通常用于可以选择多个元素的情况。这里似乎不是这种情况。您可能希望时间轴的高度8%100%,但不能同时为两者。应该使用一个select或一组radio-buttons,然后需要一个可选高度的数组,以及组件中当前选定的高度:

public selectedHeight;
public timelineHeights = ['8%', '25%', '38%', '53%', '72%', '100%'];

在模板中,只需迭代该数组并为每个数组创建一个select选项,还需要将selectedHeight绑定到该select元素,以便引用其当前值:

<select [(ngModel)]='selectedHeight'>
    <option *ngFor="let height of timelineHeights" [value]="height">height {{height}}</option>
</select>

最后,您现在可以使用selectedHeight控制时间轴高度元素:

<div [style]="'height:' + selectedHeight"> TIMELINE </div>

这种方法有一个主要优点:你只需要改变数组的值就可以控制可选的高度。这些值现在甚至可以来自服务,从数据库或配置文件中获取。

相关问题