将颜色选择器选择应用于Div字体

时间:2018-04-12 07:53:17

标签: javascript jquery html css color-picker

  1. 如何将选择颜色从颜色选择器应用到这些div:#header,#subHeader,#button?

  2. 如何在用户将文字输入<input id="text3" class="textInput">

  3. 之前隐藏div #button的边框

    &#13;
    &#13;
    $(document).ready(function(){
        var div1 = $('#header')[0];
    
        $('#text1').bind('keyup change', function() {
            div1.innerHTML = this.value;
        }); 
    
        var div2 = $('#subHeader')[0];
    
        $('#text2').bind('keyup change', function() {
            div2.innerHTML = this.value;
        });
    
        var div3 = $('#button')[0];
    
        $('#text3').bind('keyup change', function() {
            div3.innerHTML = this.value;
        });
    });
    
    
    $(".basic").spectrum();
    $(".override").spectrum({
        color: "yellow"
    });
    $(".startEmpty").spectrum({
        allowEmpty: true
    });
    &#13;
    .wrapper {
      padding: 10px;
      border: 2px solid black;
    }
    
    h2 {
      margin: 20px 0 5px;
      font-size: 16px;
      font-family: "helevtica", sans-serif;
      font-weight: 400;
    }
    
    #header {
      font-size: 60px;
      color: black;
      font-family: "helevtica", sans-serif;
      font-weight: 400;
      margin: 10px 0;
      padding: 20px;
    }
    
    #subHeader {
      font-size: 24px;
      color: black;
      font-family: "helevtica", sans-serif;
      font-weight: 300;
      margin: 10px 0;
      padding: 20px;
      display: block;
    }
    
    #button {
      font-size: 16px;
      color: black;
      font-family: "helevtica", sans-serif;
      font-weight: 400;
      margin: 30px 0;
      padding: 16px 24px;
      border: 2px solid #000;
      border-radius: 3px;
      text-align: center;
    }
    &#13;
    <link href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.js"></script>
    
    <div class="wrapper">
    <div id="header"></div> 
    <div id="subHeader"></div>
    <div id="button"></div>
    </div>
    
    <h2>Header</h2><input id="text1" class="textInput">
    <h2>Subheader</h2><input id="text2" class="textInput">
    <h2>Button</h2><input id="text3" class="textInput">
    <h2>Color</h2>
    <input type='text' class="basic" />
    &#13;
    &#13;
    &#13;

2 个答案:

答案 0 :(得分:2)

添加:

$(".basic").spectrum({
    change: function(color) {
       console.log(color.toHexString());
       //add your code here
    }
});

然后,您应该根据所选颜色添加代码以更改所需内容。如果这有助于您,或者您有任何其他问题,请告诉我。

答案 1 :(得分:2)

这会改变您的#subHeader颜色。

$(document).ready(function(){
    var div1 = $('#header')[0];

    $('#text1').bind('keyup change', function() {
        div1.innerHTML = this.value;
    }); 

    var div2 = $('#subHeader')[0];

    $('#text2').bind('keyup change', function() {
        div2.innerHTML = this.value;
    });

    var div3 = $('#button')[0];

    $('#text3').bind('keyup change', function() {
        div3.innerHTML = this.value;
        if(this.value.length > 0) {
            $('#button').css('display', 'block')
        } else {
           $('#button').css('display', 'none')
        }
    });
});


$(".basic").spectrum({
   change: function(color) {
       console.log(color.toHexString());
       $('#subHeader').css('color',color.toHexString());
       var s = '2px solid ' + color.toHexString();
       $('#button').css('border', s);
}
});
$(".override").spectrum({
    color: "yellow"
});
$(".startEmpty").spectrum({
    allowEmpty: true
});
.wrapper {
  padding: 10px;
  border: 2px solid black;
}

h2 {
  margin: 20px 0 5px;
  font-size: 16px;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
}

#header {
  font-size: 60px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 10px 0;
  padding: 20px;
}

#subHeader {
  font-size: 24px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 300;
  margin: 10px 0;
  padding: 20px;
  display: block;
}

#button {
  display:none;
  font-size: 16px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 30px 0;
  padding: 16px 24px;
  border: 2px solid #000;
  border-radius: 3px;
  text-align: center;
}
<link href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.js"></script>

<div class="wrapper">
<div id="header"></div> 
<div id="subHeader"></div>
<div id="button"></div>
</div>

<h2>Header</h2><input id="text1" class="textInput">
<h2>Subheader</h2><input id="text2" class="textInput">
<h2>Button</h2><input id="text3" class="textInput">
<h2>Color</h2>
<input type='text' class="basic" />