我想通过点击并拖动颜色输入的颜色值来更改我的div的背景颜色。
HTML:
<div class="center">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<div class="center" id="colorDiv">
<input type="color" id="colorPicker">
</div>
使用Javascript:
var div = $('.square');
div.mousedown(function() {
$(this).css("background-color", $("#colorPicker").val());
});
答案 0 :(得分:2)
使用您的jQuery执行此操作:
var div = $('.square');
div.mousedown(function() {
$(this).css("background-color", $("#colorPicker").val());
//add this line
div.on("mouseenter.square",function(){
$(this).css("background-color", $("#colorPicker").val());
});
});
答案 1 :(得分:0)
<script type="text/javascript">
$(document).ready(function(){
$('input').on('change', function() {
var color = $("#colorPicker").val();
$(".square").css("background-color", color);
});
});
</script>
<div class="center">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<div class="center" id="colorDiv">
<input type="color" id="colorPicker">
</div>
<style type="text/css">
.square
{
width: 100%;
height: 20%;
float: left;
position: absolute;
}
#colorPicker
{
top: 20%;
position: relative;
}
</style>
答案 2 :(得分:0)
这可能是你想要的。
var div = $('.square')
div.mousedown(function() {
$(this).css("background-color", $("#colorPicker").val())
div.mouseenter(function() {
$(this).css("background-color", $("#colorPicker").val())
})
})
$(document).mouseup(function() {
div.off('mouseenter')
})