Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีตั้งค่าความโปร่งแสง (Opacity) ขององค์ประกอบด้วย JavaScript

ใช้คุณสมบัติ opacity เพื่อกำหนดความโปร่งแสงขององค์ประกอบทั้งหมด (รวมลูก) ค่า 0 (โปร่งใส) ถึง 1 (ทึบแสง) หรือเปอร์เซ็นต์

ตัวอย่าง

<!DOCTYPE html>
<html>
  <head>
    <style>
      #box {
        width: 450px;
        background-color: gray;
      }
    </style>
  </head>
  <body>
    <p>Click below to set Opacity.</p>
    <button type="button" onclick="display()">Set Opacity</button>
    <div id="box">
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
    </div>
    <br>
    <script>
      function display() {
        document.getElementById("box").style.opacity = "0.5";
      }
    </script>
  </body>
</html>