jQuery簡單調色器程式碼例項

螞蟻小編發表於2017-04-14

本章節分享一段程式碼例項,它使用jquery實現了簡單的調色器功能。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
#mycolor {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  background: #000;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(function () {
  $('input').change(function () {
    var ranges = $('input');
    var arr = [];
    for (var index = 0; index < ranges.length ; index++) {
      arr[index] = ranges[index].value;
    }
    $('#mycolor').css("background", "rgb(" + arr[0] + "," + arr[1] + "," + arr[2] + ")");
    $('.show').text("rgb(" + arr[0] + "," + arr[1] + "," + arr[2] + ")");
  })
});
</script>
</head>
<body>
  <div id="mycolor"></div>
  R(紅色)<input type="range" min="0" max="255" value="0" /><span></span><br />
  G(綠色)<input type="range" min="0" max="255" value="0" /><span></span><br />
  B(藍色)<input type="range" min="0" max="255" value="0" /><span></span><br />
  <span class="show">rgb(0,0,0)</span>
</body>
</html>

相關文章