如何使用 JavaScript 設定影像的亮度和對比度?


要設定亮度,請使用 brightness 屬性,而對於對比度,請使用 contrast 屬性。

示例

你可以嘗試執行下面的程式碼,使用 JavaScript 裡的影像濾鏡 −

線上演示

<!DOCTYPE html>
<html>
   <body>
      <p>Click below to change the brightness and contrast of the image.</p>
      <button onclick="display()">Edit Image</button><br><br>
      <img id="myID" src="https://tutorialspoint.tw/videotutorials/images/tutorial_library_home.jpg"
         alt="Tutorials Library" width="320" height="320">
      <script>
         function display() {
            document.getElementById("myID").style.filter = "brightness(50%)";
            document.getElementById("myID").style.filter = "contrast(50%)";
         }
      </script>
   </body>
</html>

更新於: 2020-6-23

3K+ 瀏覽量

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.