從表單中選擇選項時變更影像不透明度
P粉244155277
P粉244155277 2024-04-03 23:00:48
0
2
450

當我選擇值為 1(萬事達卡)的選項時,如何才能將 id Master 的圖像的不透明度更改為 1?並將其放入適用於這 4 個值的函數中,每個值都有自己的 id?我希望預設影像為 0.3,當選擇其中一個選項時,影像的不透明度為 1。

.itemForm2 {
  width: 100%;
  position: relative;
  margin-bottom: 18px;
  margin-top: 10px;
  display: flex;
  flex-direction: row;
}

.escolhaCartao {
  display: flex;
  flex-direction: row;
  width: 218px!important;
  gap: 10px;
}

@media(min-width:768px) {
  .escolhaCartao {
    width: 258px!important;
    height: 34.32px;
    gap: 5px;
  }
  .lineFormCartao {
    display: flex;
    flex-direction: row;
    justify-content: inherit;
    gap: 16px;
  }
  .escolhaCartao img {
    opacity: .3;
  }
}
<div class="lineForm lineFormCartao">
  <div class="itemForm">
    <select id="OpcoesCartao" name="OpcoesCartao">
      <option value="1">Mastercard</option>
      <option value="2">Visa</option>
      <option value="3">Elo</option>
      <option value="4">Amex</option>
    </select>
  </div>
  <div class="itemForm2 escolhaCartao">
    <img id="master" src="images/master.png" alt="Logo cartão Mastercard">
    <img id="visa" src="images/visa.png" alt="Logo cartão Visa">
    <img id="elo" src="images/elo.png" alt="Logo cartão Elo">
    <img id="amex" src="images/amex.png" alt="Logo cartão Amex">
  </div>
</div>

P粉244155277
P粉244155277

全部回覆(2)
P粉521748211

只使用 CSS 和 has

.escolhaCartao img {
 opacity: .2;
}

.itemForm:has(option[value="1"]:checked) + .itemForm2 > #master,
.itemForm:has(option[value="2"]:checked) + .itemForm2 > #visa,
.itemForm:has(option[value="3"]:checked) + .itemForm2 > #elo,
.itemForm:has(option[value="4"]:checked) + .itemForm2 > #amex
{
  opacity: 1;
}
Logo cartão Mastercard Logo cartão Visa Logo cartão Elo Logo cartão Amex

使用 JavaScript

var sel = document.querySelector("#OpcoesCartao");
const imgs =  document.querySelectorAll(".escolhaCartao img");

sel.addEventListener("change", function (e) {
  const val = this.value;
  const selected = document.querySelector(".escolhaCartao img.selected");
  if (selected) {
    selected.classList.remove('selected');
  }
  imgs[+val - 1].classList.add('selected');
});

sel.dispatchEvent(new Event('change'));
.escolhaCartao img {
 opacity: .2;
}

.escolhaCartao img.selected
{
  opacity: 1;
}
Logo cartão Mastercard Logo cartão Visa Logo cartão Elo Logo cartão Amex
P粉799885311

你可以這樣做。這適用於大多數瀏覽器。 Firefox 應該很快就會支援它 (https://caniuse.com/?search=has) p>

.itemForm2 {
    width: 100%;
    position: relative;
    margin-bottom: 18px;
    margin-top: 10px;
    display: flex;
    flex-direction: row;
}

.escolhaCartao {
    display: flex;
    flex-direction: row;
    width: 218px!important;
    gap: 10px;
}

.lineForm:has( option[value="1"]:checked ) #master {
  opacity: 1;
}
.lineForm:has( option[value="2"]:checked ) #visa {
  opacity: 1;
}
.lineForm:has( option[value="3"]:checked ) #elo {
  opacity: 1;
}
.lineForm:has( option[value="4"]:checked ) #amex {
  opacity: 1;
}


    .escolhaCartao {
        width: 258px!important;
        height: 34.32px;
        gap: 5px;
    }

    .lineFormCartao {
        display: flex;
        flex-direction: row;
        justify-content: inherit;
        gap: 16px;
    }
    .escolhaCartao img {
        opacity: .3;
    }
Logo cartão Mastercard Logo cartão Visa Logo cartão Elo Logo cartão Amex
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板