我试图让特定的.div在点击时更改背景颜色。 由于用户可以更改颜色,因此颜色必须是变量,并且应该在.ready()时运行。
我正在使用CSS变量' - main-color'作为我想要更改背景颜色的变量,但是在使用'$(this)'时我似乎无法找到解决它的jQuery方法。
是否有使用.click()函数应用自定义CSS属性/变量的特定方法?
$(document).ready(function(){
// CREATE GRID
for (i = 0; i<1152; i++) {
var board = document.createElement('div');
board.className = 'grid';
board.addId = 'color'
document.getElementById('container').appendChild(board);
};
// END CREATE GRID
// HOVER AND CLICK FUNCTION //
$('#color-picker').colorpicker().on('changeColor', function(ev){
var choice = ev.color.toHex();
document.body.style.setProperty('--main-color', choice);
});
$('.grid').hover(function(){
$(this).click(function(){
$(this).css('background-color', 'var(--main-color)');
});
});
// END HOVER AND CLICK FUNCTION
});
body {
background-color: #222;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family:'Patua One', cursive;
color: #FFF;
--main-color: #000;
}
*{
background-color: #222;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family:'Patua One', cursive;
color: #FFF;
}
#title {
font-family:'Revalia', cursive;
font-size: 3em;
color: #FFF;
text-align: center;
}
.menu {
float:left;
clear: left;
width: 300px;
margin-top: 5%;
margin-left:5%;
}
#container {
height: 800px;
width: 800px;
border: 1px solid #424242;
margin-left: 30%;
margin-top: 5%;
background-color: #FFF;
}
.grid {
height: 20px;
width: 20px;
border: 1px dotted #424242;
background-color: #FFF;
display: inline-grid;
}
div.grid:hover{;
background-color: var(--main-color);
}
.active {
background-color: var(--main-color);
}
.form-control {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #FFF;
background-color: #222;
background-image: none;
border: 1px solid #F85658;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Sketch</title>
<!--Javascript & jQuery Imports /-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<script src='js/main.js'></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!--CSS Imports /-->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link href="CSS/style.css" rel="stylesheet" type="text/css">
<!--Font Imports /-->
<link href="https://fonts.googleapis.com/css?family=Patua+One|Revalia" rel="stylesheet">
<!--BOOTSTRAP IMPORTS /-->
<script src='js/bootstrap-colorpicker.min.js'></script>
<link rel='stylesheet' href='CSS/bootstrap-colorpicker.min.css'></link>
<link rel='stylesheet' href='CSS/bootstrap-colorpicker.min.css.map'></link>
</head>
<body>
<div id = 'title'>
ZACH<span style='color: #F85658'>'</span>S PIXELMAKER</div>
</div>
<div class = 'menu'>
<div class = 'color-section'>
<p class="ui-corner-all" style="padding:12px;"> Color:
</p>
</div>
<div id="color-picker" class="input-group colorpicker-component">
<input type="text" value="#000000" class="form-control" />
<span class="input-group-addon"><i></i></span>
<script>
</script>
</div>
</div>
<div id = 'container'>
</div>
</body>
</html>
答案 0 :(得分:1)
将颜色指定给一个类,然后切换该类。
document.querySelector('div').addEventListener('click',function() {
this.classList.toggle('color');
})
&#13;
body {
--main-color: #000;
}
.color {
background-color: var(--main-color);
color: white;
}
&#13;
<div>div</div>
&#13;
或者使用jquery ......
$(function() {
$('div').on('click',function() {
$(this).toggleClass('color');
});
});
&#13;
body {
--main-color: #000;
}
.color {
background-color: var(--main-color);
color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>div</div>
&#13;