在自定义下拉列表中,为什么在渲染下拉列表时转换属性不起作用?

问题描述

在您在下面看到的自定义下拉菜单中,我附加了一个过渡属性,因此当单击下拉菜单时,菜单项将在 200 毫秒后淡入。我已将它附加到下拉项的父类,但它不起作用。我不确定我哪里出错了。

for (const dropdown of document.querySelectorAll(".custom__select-wrapper:not(.clearFilter)")) {
  dropdown.addEventListener("click",function () {
    this.querySelector(".custom__select").classList.toggle("open");
  });
}

for (const option of document.querySelectorAll(".custom__option")) {
  option.addEventListener("click",function () {
    if (!this.classList.contains("selected")) {
      this.parentNode
        .querySelector(".custom__option.selected")
        .classList.remove("selected");
      this.classList.add("selected");
      this.closest(".custom__select").querySelector(
        ".custom__select-trigger h6"
      ).textContent = this.textContent;
      if (this.getAttribute("data-year")) {
        current_year = this.dataset["year"];
        yearFilter(this.dataset["year"]);
      } else {
        current_story = this.dataset["type"];
        storyFilter(this.dataset["type"]);
      }
    }
  });
}

window.addEventListener("click",function (e) {
  for (const select of document.querySelectorAll(".custom__select")) {
    if (!select.contains(e.target)) {
      select.classList.remove("open");
    }
  }
});
button.clear {
  border: 0;
  background: #fff;
}

#selectedFilter {
  color: #005fec;
}

ul {
  padding-left: 0;
  margin: 0;
}

.filter {
  padding-right: 15px;
  padding-left: 15px;
  margin-bottom: 2rem;
  display: -webkit-Box;
  display: -ms-flexBox;
  display: flex;
  flex-direction: column;
}

@media (min-width: 768px) {
  .filter__settings {
    display: -webkit-Box;
    display: -ms-flexBox;
    display: flex;
    flex-direction: row;
    border-top: 1px solid #e0e5ec;
    border-bottom: 1px solid #e0e5ec;
  }
}

.custom__select {
  position: relative;
  display: -webkit-Box;
  display: -ms-flexBox;
  display: flex;
  flex-direction: column;
}

.custom__select-wrapper {
  position: relative;
  user-select: none;
  padding: 0;
  border-bottom: 1px solid #e0e5ec;
}

.custom__select-wrapper:last-child {
  border: 0;
}

.custom__select-wrapper.clearFilter {
  display: -webkit-Box;
  display: -ms-flexBox;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.custom__select-wrapper .selected-clearFilter {
  position: relative;
  user-select: none;
  padding: 1rem 0 !important;
}

@media (min-width: 768px) {
  .custom__select-wrapper {
    padding: 0 2em;
    border: 0;
    display: -webkit-Box;
    display: -ms-flexBox;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .custom__select-wrapper:first-child,.custom__select-wrapper:last-child {
    padding: 0;
  }
}

.custom__select-wrapper h6 {
  padding: 20px 3px;
  color: #62668c;
  font-weight: 300;
}

.custom__select-trigger {
  position: relative;
  display: -webkit-Box;
  display: -ms-flexBox;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
}

@media (min-width: 768px) {
  .custom__select-trigger {
    justify-content: space-evenly;
    margin-right: auto;
  }
}

.custom__select-wrapper h6,.custom__select-trigger h6 {
  font-size: 0.75rem;
  line-height: 0.75rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  padding: 20px 0;
}

.custom__select-trigger h6 {
  color: #005fec;
  font-weight: 900;
}

.custom__select-wrapper #selectedFilter {
  font-size: 12px;
  line-height: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #005fec;
  font-weight: 800;
  padding: 0;
}

.custom__options {
  display: none;
  top: 100%;
  left: 0;
  right: 0;
  border-top: 0;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  z-index: 2;
  color: #e0e5ec;
}

@media (min-width: 768px) {
  .custom__options {
    position: absolute;
    background-color: #005fec;
    max-height: 320px;
    overflow-y: scroll;
    transition: all 200ms ease-in;
  }
  .custom__options#storyFilter {
    overflow: hidden;
  }
}

@media (min-width: 768px) {
  .custom__options:before,.custom__options:after {
    content: "";
    position: absolute;
    bottom: 100%;
    left: 11px;
    top: -18px;
    border: 11px solid transparent;
    border-bottom-color: #005fec;
  }
}

.custom__options.active {
  display: block;
  visibility: visible;
  opacity: 1;
  z-index: 10;
}

.custom__select-trigger,.custom__option {
  letter-spacing: 1px;
  font-weight: 800;
  color: #005fec;
  border: 0;
  background: transparent;
}

.custom__select.open .custom__options {
  display: block;
  opacity: 1;
  visibility: visible;
  pointer-events: all;
  color: #fff;
  min-width: 15rem;
}

@media (min-width: 768px) {
  .custom__select.open .custom__options {
    Box-shadow: 0px 12px 30px rgba(0,0.12);
  }
}

.custom__option {
  position: relative;
  display: block;
  padding: 0 22px 0 12px;
  font-weight: 400;
  color: #62668c;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1rem;
  margin: 1.5rem 0;
}

@media (min-width: 768px) {
  .custom__option {
    font-size: 1.25rem;
    line-height: 1.25rem;
    color: white;
    font-weight: 300;
    padding: 0 22px 0 20px;
  }
}

.custom__option:hover {
  cursor: pointer;
}

.custom__option.selected {
  color: #005fec;
}

@media (min-width: 768px) {
  .custom__option.selected {
    color: #ffffff;
  }
}

.custom__option.selected::before {
  content: "•";
  margin-left: -12px;
  padding-right: 8px;
}

.empty-state {
  width: 100%;
  display: -webkit-Box;
  display: -ms-flexBox;
  display: flex;
  background: #005fec;
  padding: 1rem;
  border-radius: 4px;
  align-items: center;
  margin: 2rem 0.5rem;
}

.empty-state h4 {
  color: white;
  font-size: 1rem;
  line-height: 1.5rem;
  font-weight: 300;
  margin-left: 0.5rem;
}

.empty-state h4 span {
  color: white;
  text-decoration: underline;
  cursor: pointer;
}

.arrow {
  position: relative;
  height: 5px;
  width: 3px;
  margin-left: 0.5rem;
  margin-bottom: 0.1rem;
}

.arrow::before,.arrow::after {
  content: "";
  position: absolute;
  bottom: 0px;
  width: 0.1rem;
  height: 100%;
  transition: all 0.45s;
}

.arrow::before {
  left: 0px;
  transform: rotate(45deg);
  background-color: #005fec;
}

@media (min-width: 768px) {
  .arrow::before {
    left: 7px;
  }
}

.arrow::after {
  left: -2.5px;
  transform: rotate(-45deg);
  background-color: #005fec;
}

@media (min-width: 768px) {
  .arrow::after {
    left: 4.5px;
  }
}

.open .arrow::before {
  left: 0px;
  transform: rotate(-45deg);
}

@media (min-width: 768px) {
  .open .arrow::before {
    left: 7px;
  }
}

.open .arrow::after {
  left: -2.5px;
  transform: rotate(45deg);
}

@media (min-width: 768px) {
  .open .arrow::after {
    left: 4.5px;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">

<div class="container">
  <section class="filter">
    <div class="filter__settings">
      <div class="custom__select-wrapper">
        <div class="custom__select year-sel selector">
          <div class="custom__select-trigger">
            <h6>Year</h6>
            <div class="arrow"></div>
          </div>
          <div class="custom__options dropdown year-selector" id="yearFilter">
            <span class="custom__option selected" data-year="all">All</span>
            <span class="custom__option" data-year="2021">2021</span>
            <span class="custom__option" data-year="2020">2020</span>
            <span class="custom__option" data-year="2019">2019</span>
            <span class="custom__option" data-year="2018">2018</span>
            <span class="custom__option" data-year="2017">2017</span>
            <span class="custom__option" data-year="2016">2016</span>
            <span class="custom__option" data-year="2015">2015</span>
            <span class="custom__option" data-year="2014">2014</span>
            <span class="custom__option" data-year="2013">2013</span>
            <span class="custom__option" data-year="2012">2012</span>
            <span class="custom__option" data-year="2011">2011</span>
            <span class="custom__option" data-year="2010">2010</span>
            <span class="custom__option" data-year="2009">2009</span>
            <span class="custom__option" data-year="2008">2008</span>
            <span class="custom__option" data-year="2007">2007</span>
            <span class="custom__option" data-year="2006">2006</span>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

解决方法

将过渡放在类 custom__options 上是正确的做法,但 display 属性也类似于过渡状态。一旦删除,过渡就会起作用......我没有使用display: none;,而是使用visibility: hidden;

for (const dropdown of document.querySelectorAll(".custom__select-wrapper:not(.clearFilter)")) {
  dropdown.addEventListener("click",function() {
    this.querySelector(".custom__select").classList.toggle("open");
  });
}

for (const option of document.querySelectorAll(".custom__option")) {
  option.addEventListener("click",function() {
    if (!this.classList.contains("selected")) {
      this.parentNode
        .querySelector(".custom__option.selected")
        .classList.remove("selected");
      this.classList.add("selected");
      this.closest(".custom__select").querySelector(
        ".custom__select-trigger h6"
      ).textContent = this.textContent;
      if (this.getAttribute("data-year")) {
        current_year = this.dataset["year"];
        yearFilter(this.dataset["year"]);
      } else {
        current_story = this.dataset["type"];
        storyFilter(this.dataset["type"]);
      }
    }
  });
}

window.addEventListener("click",function(e) {
  for (const select of document.querySelectorAll(".custom__select")) {
    if (!select.contains(e.target)) {
      select.classList.remove("open");
    }
  }
});
@charset "UTF-8";
/*
.class {
  property: value;

  @include tablet-small {
    property: value;
  }
}
*/
button.clear {
  border: 0;
  background: #fff;
}

#selectedFilter {
  color: #005fec;
}

ul {
  padding-left: 0;
  margin: 0;
}

.filter {
  padding-right: 15px;
  padding-left: 15px;
  margin-bottom: 2rem;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
}

@media (min-width: 768px) {
  .filter__settings {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
        -ms-flex-direction: row;
            flex-direction: row;
    border-top: 1px solid #E0E5EC;
    border-bottom: 1px solid #E0E5EC;
  }
}

.custom__select {
  position: relative;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
}

@media (min-width: 768px) {
  .custom::before,.custom__options {
    -webkit-transition: all .2s ease-in;
    transition: all .2s ease-in;
  }
}

.custom__select-wrapper {
  position: relative;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  padding: 0;
  border-bottom: 1px solid #E0E5EC;
}

.custom__select-wrapper:last-child {
  border: 0;
}

.custom__select-wrapper.clearFilter {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}

.custom__select-wrapper .selected-clearFilter {
  position: relative;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  padding: 1rem 0 !important;
}

@media (min-width: 768px) {
  .custom__select-wrapper {
    padding: 0 2em;
    border: 0;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
        -ms-flex-pack: center;
            justify-content: center;
    -webkit-box-align: center;
        -ms-flex-align: center;
            align-items: center;
  }
  .custom__select-wrapper:last-child {
    margin-left: auto;
  }
  .custom__select-wrapper:first-child,.custom__select-wrapper:last-child {
    padding: 0;
  }
}

.custom__select-wrapper h6 {
  padding: 20px 3px;
  color: #62668C;
  font-weight: 300;
}

.custom__select-trigger {
  position: relative;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: justify;
      -ms-flex-pack: justify;
          justify-content: space-between;
  cursor: pointer;
}

@media (min-width: 768px) {
  .custom__select-trigger {
    -webkit-box-pack: space-evenly;
        -ms-flex-pack: space-evenly;
            justify-content: space-evenly;
    margin-right: auto;
  }
}

.custom__select-wrapper h6,.custom__select-trigger h6 {
  font-size: .75rem;
  line-height: .75rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  padding: 20px 0;
}

.custom__select-trigger h6 {
  color: #005fec;
  font-weight: 900;
}

.custom__select-wrapper #selectedFilter {
  font-size: 12px;
  line-height: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #005fec;
  font-weight: 800;
  padding: 0;
}

.custom__options {
  top: 100%;
  left: 0;
  right: 0;
  border-top: 0;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  z-index: 2;
  color: #E0E5EC;
}

@media (min-width: 768px) {
  .custom__options {
    position: absolute;
    background-color: #005fec;
    max-height: 320px;
    overflow-y: scroll;
  }
  .custom__options#storyFilter {
    overflow: hidden;
  }
}

.custom__options.active {
  display: block;
  visibility: visible;
  opacity: 1;
  z-index: 10;
}

.custom__select-trigger,.custom__option {
  letter-spacing: 1px;
  font-weight: 800;
  color: #005fec;
  border: 0;
  background: transparent;
}

.custom__select.open::before {
  content: "";
  position: absolute;
  bottom: 0;
  left: 11px;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 11px solid #005fec;
}

.custom__select .custom__options {
  min-width: 15rem;
}

.custom__select.open .custom__options {
  opacity: 1;
  visibility: visible;
  pointer-events: all;
  color: #fff;
  min-width: 15rem;
}

@media (min-width: 768px) {
  .custom__select.open .custom__options {
    -webkit-box-shadow: 0px 12px 30px rgba(0,0.12);
            box-shadow: 0px 12px 30px rgba(0,0.12);
  }
}

.custom__option {
  position: relative;
  display: block;
  padding: 0 22px 0 12px;
  font-weight: 400;
  color: #62668C;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1rem;
  margin: 1.5rem 0;
}

@media (min-width: 768px) {
  .custom__option {
    font-size: 1.25rem;
    line-height: 1.25rem;
    color: white;
    font-weight: 300;
    padding: 0 22px 0 20px;
  }
}

.custom__option:hover {
  cursor: pointer;
}

.custom__option.selected {
  color: #005fec;
}

@media (min-width: 768px) {
  .custom__option.selected {
    color: #ffffff;
  }
}

.custom__option.selected::before {
  content: "•";
  margin-left: -12px;
  padding-right: 8px;
}

.empty-state {
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  background: #005fec;
  padding: 1rem;
  border-radius: 4px;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  margin: 2rem .5rem;
}

.empty-state h4 {
  color: white;
  font-size: 1rem;
  line-height: 1.5rem;
  font-weight: 300;
  margin-left: .5rem;
}

.empty-state h4 span {
  color: white;
  text-decoration: underline;
  cursor: pointer;
}

.arrow {
  position: relative;
  height: 5px;
  width: 3px;
  margin-left: .5rem;
  margin-bottom: .1rem;
}

.arrow::before,.arrow::after {
  content: "";
  position: absolute;
  bottom: 0px;
  width: 0.1rem;
  height: 100%;
  -webkit-transition: all 0.45s;
  transition: all 0.45s;
}

.arrow::before {
  left: 0px;
  -webkit-transform: rotate(45deg);
          transform: rotate(45deg);
  background-color: #005fec;
}

@media (min-width: 768px) {
  .arrow::before {
    left: 7px;
  }
}

.arrow::after {
  left: -2.5px;
  -webkit-transform: rotate(-45deg);
          transform: rotate(-45deg);
  background-color: #005fec;
}

@media (min-width: 768px) {
  .arrow::after {
    left: 4.5px;
  }
}

.open .arrow::before {
  left: 0px;
  -webkit-transform: rotate(-45deg);
          transform: rotate(-45deg);
}

@media (min-width: 768px) {
  .open .arrow::before {
    left: 7px;
  }
}

.open .arrow::after {
  left: -2.5px;
  -webkit-transform: rotate(45deg);
          transform: rotate(45deg);
}

@media (min-width: 768px) {
  .open .arrow::after {
    left: 4.5px;
  }
}
/*# sourceMappingURL=filters.css.map */
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">

<div class="container">
  <section class="filter">
    <div class="filter__settings">
      <div class="custom__select-wrapper">
        <div class="custom__select year-sel selector">
          <div class="custom__select-trigger">
            <h6>Year</h6>
            <div class="arrow"></div>
          </div>
          <div class="custom__options dropdown year-selector" id="yearFilter">
            <span class="custom__option selected" data-year="all">All</span>
            <span class="custom__option" data-year="2021">2021</span>
            <span class="custom__option" data-year="2020">2020</span>
            <span class="custom__option" data-year="2019">2019</span>
            <span class="custom__option" data-year="2018">2018</span>
            <span class="custom__option" data-year="2017">2017</span>
            <span class="custom__option" data-year="2016">2016</span>
            <span class="custom__option" data-year="2015">2015</span>
            <span class="custom__option" data-year="2014">2014</span>
            <span class="custom__option" data-year="2013">2013</span>
            <span class="custom__option" data-year="2012">2012</span>
            <span class="custom__option" data-year="2011">2011</span>
            <span class="custom__option" data-year="2010">2010</span>
            <span class="custom__option" data-year="2009">2009</span>
            <span class="custom__option" data-year="2008">2008</span>
            <span class="custom__option" data-year="2007">2007</span>
            <span class="custom__option" data-year="2006">2006</span>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...