当鼠标具有特定 class 时更改 div 的背景图像
Change background image of div when mouse has specific class
我正在创建一个交互式艺术作品,人们可以在其中进行协作。我想要的是有一个人们可以使用的图标库。当点击它们时,鼠标会得到一个 class,因此当点击 table 中的特定单元格时,该单元格将获得图标作为其背景图像。
(现在我知道我需要一个数据库等,但那是以后的事情。要事第一。)
我很久以前做过类似的事情,但我不知道如何编辑它才能为这个项目工作:
function nextPic() {
if ( $('html,body').css('cursor').indexOf('cursor2.png') > 0 )
{
counter += 1;
if (counter > myPictures.length - 1) {
// counter = 0;
}
else {
element.style.backgroundImage = "url(" + myPictures[counter] + ")";
}
}
它只是根据光标上的图像工作。一点也不理想,它一直在窃听。
我看过像素艺术的代码,它(某种程度上)完成了需要完成的工作。但不完全是。
像素艺术 fiddle 此处:
我不知道如何使用 class 来设置不同的颜色; a class 会推断它们都是一样的。你必须使用 ids 来区分。您可以将要将背景更改为的 id 和颜色传递给函数并以这种方式工作。或者您可能会选择将颜色放在一个数组中并使用索引号?
我使用您 fiddle 中的方块做了一个基本示例,以提供一个想法。如您所见,我只使用 css 为两者之一分配了背景值。当然,如果您想使用图像而不是颜色,可以调整该功能以更改背景图像..
希望对您有所帮助
function setPenColour(id, pixel) {
var myDiv = document.getElementById(id);
myDiv.style.backgroundColor = pixel;
}
#p1 {
background-color: grey;
}
#art {
display: table;
border-spacing: 1px;
background-color: black;
border: 1px solid black;
}
.row {
display: table-row;
}
.pixel {
display: table-cell;
background-color: white;
width: 25px;
margin: 1px;
height: 25px;
}
.pen {
display: inline-block;
width: 40px;
height: 40px;
border: 2px solid black;
}
<div id="p1" class="pen" onclick="setPenColour(id, '#00FF00')" ;></div>
<div id="p2" class="pen" style="background-color:blue;" onclick="setPenColour(id, 'red')"></div>
已修复!
<div id="p1" class="pen"></div>
<div id="p2" class="pen"></div>
<div id="art">
<div class="row">
<div class="pixel"></div>
<div class="pixel"></div>
</div>
</div>
icon = '(icon.png)'
$('#p1').click(function (){
icon = "icon2.png";
alert(icon);
});
$('#p2').click(function (){
icon = "icon3.png";
alert(icon);
});
// onclick function classes
$(".pixel").click(function() {
//function that changes background to url
// $(this).css('background-image', var(icon));
// $(this).css("background-image", "url(" + icon")");
$(this).css("background-image", "url(" +icon +")");
});
#art {
display: table;
border-spacing: 1px;
background-color: black;
border: 1px solid black;
}
.row {
display: table-row;
}
.pixel {
display: table-cell;
background-color: white;
width: 25px;
margin: 1px;
height: 25px;
background-size: contain;
}
.pen {
display: inline-block;
width: 40px;
height: 40px;
border: 2px solid black;
}
所以这是一个人做你想做的事的例子,而不是在 canvas 中的 table 网格中。疯狂支持他们把这个放出来:
http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/
在 canvas 上进行基本绘图后,他们添加了挑选颜色的功能。请注意他们如何将选取的颜色保存到一个变量中,然后为 canvas reDraw() 函数调用该变量。
同样的事情也可以用图标来完成。只需为单击任何图标设置一个 onClick 事件,就像他们对颜色所做的那样(在他们的情况下,他们选择了 mousedown 事件):
$('#choosePurpleSimpleColors').mousedown(function(e){
curColor_simpleColors = colorPurple;
});
例如,这是他们放在紫色按钮上的事件处理程序。您可以通过检查元素(按钮)然后右键单击事件处理程序然后转到 mousedown 事件来查看这一点,然后您可以单击此代码所在的 javascript 文件位置。 (所有这些都在 chrome 检查{右键单击,然后选择检查})
现在,如果您不想在 canvas 上执行此操作,而是在 table/grid 上执行此操作,您只需为单元格设置 onClick 事件。然后他们会调用 color/icon 变量并为该单元格设置它们。
如果您想要 grid/table 的示例,请告诉我。 canvas 是一个更复杂的答案,但我怀疑这是您真正喜欢的。第二个示例及以后的示例正是您想要的,因此您也可以选择要插入的图标。
context = document.getElementById('canvas').getContext("2d");
$('#canvas').mousedown(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
redraw();
});
$('#canvas').mousemove(function(e){
if(paint){
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
redraw();
}
});
$('#canvas').mouseup(function(e){
paint = false;
});
$('#canvas').mouseleave(function(e){
paint = false;
});
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var paint;
function addClick(x, y, dragging)
{
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
}
function redraw(){
context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
context.strokeStyle = "#df4b26";
context.lineJoin = "round";
context.lineWidth = 5;
for(var i=0; i < clickX.length; i++) {
context.beginPath();
if(clickDrag[i] && i){
context.moveTo(clickX[i-1], clickY[i-1]);
}else{
context.moveTo(clickX[i]-1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.stroke();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="canvas" width="490" height="220" style="border: solid 1px black;"></canvas>
我正在创建一个交互式艺术作品,人们可以在其中进行协作。我想要的是有一个人们可以使用的图标库。当点击它们时,鼠标会得到一个 class,因此当点击 table 中的特定单元格时,该单元格将获得图标作为其背景图像。
(现在我知道我需要一个数据库等,但那是以后的事情。要事第一。)
我很久以前做过类似的事情,但我不知道如何编辑它才能为这个项目工作:
function nextPic() {
if ( $('html,body').css('cursor').indexOf('cursor2.png') > 0 )
{
counter += 1;
if (counter > myPictures.length - 1) {
// counter = 0;
}
else {
element.style.backgroundImage = "url(" + myPictures[counter] + ")";
}
}
它只是根据光标上的图像工作。一点也不理想,它一直在窃听。
我看过像素艺术的代码,它(某种程度上)完成了需要完成的工作。但不完全是。
像素艺术 fiddle 此处:
我不知道如何使用 class 来设置不同的颜色; a class 会推断它们都是一样的。你必须使用 ids 来区分。您可以将要将背景更改为的 id 和颜色传递给函数并以这种方式工作。或者您可能会选择将颜色放在一个数组中并使用索引号?
我使用您 fiddle 中的方块做了一个基本示例,以提供一个想法。如您所见,我只使用 css 为两者之一分配了背景值。当然,如果您想使用图像而不是颜色,可以调整该功能以更改背景图像..
希望对您有所帮助
function setPenColour(id, pixel) {
var myDiv = document.getElementById(id);
myDiv.style.backgroundColor = pixel;
}
#p1 {
background-color: grey;
}
#art {
display: table;
border-spacing: 1px;
background-color: black;
border: 1px solid black;
}
.row {
display: table-row;
}
.pixel {
display: table-cell;
background-color: white;
width: 25px;
margin: 1px;
height: 25px;
}
.pen {
display: inline-block;
width: 40px;
height: 40px;
border: 2px solid black;
}
<div id="p1" class="pen" onclick="setPenColour(id, '#00FF00')" ;></div>
<div id="p2" class="pen" style="background-color:blue;" onclick="setPenColour(id, 'red')"></div>
已修复!
<div id="p1" class="pen"></div>
<div id="p2" class="pen"></div>
<div id="art">
<div class="row">
<div class="pixel"></div>
<div class="pixel"></div>
</div>
</div>
icon = '(icon.png)'
$('#p1').click(function (){
icon = "icon2.png";
alert(icon);
});
$('#p2').click(function (){
icon = "icon3.png";
alert(icon);
});
// onclick function classes
$(".pixel").click(function() {
//function that changes background to url
// $(this).css('background-image', var(icon));
// $(this).css("background-image", "url(" + icon")");
$(this).css("background-image", "url(" +icon +")");
});
#art {
display: table;
border-spacing: 1px;
background-color: black;
border: 1px solid black;
}
.row {
display: table-row;
}
.pixel {
display: table-cell;
background-color: white;
width: 25px;
margin: 1px;
height: 25px;
background-size: contain;
}
.pen {
display: inline-block;
width: 40px;
height: 40px;
border: 2px solid black;
}
所以这是一个人做你想做的事的例子,而不是在 canvas 中的 table 网格中。疯狂支持他们把这个放出来: http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/
在 canvas 上进行基本绘图后,他们添加了挑选颜色的功能。请注意他们如何将选取的颜色保存到一个变量中,然后为 canvas reDraw() 函数调用该变量。
同样的事情也可以用图标来完成。只需为单击任何图标设置一个 onClick 事件,就像他们对颜色所做的那样(在他们的情况下,他们选择了 mousedown 事件):
$('#choosePurpleSimpleColors').mousedown(function(e){
curColor_simpleColors = colorPurple;
});
例如,这是他们放在紫色按钮上的事件处理程序。您可以通过检查元素(按钮)然后右键单击事件处理程序然后转到 mousedown 事件来查看这一点,然后您可以单击此代码所在的 javascript 文件位置。 (所有这些都在 chrome 检查{右键单击,然后选择检查})
现在,如果您不想在 canvas 上执行此操作,而是在 table/grid 上执行此操作,您只需为单元格设置 onClick 事件。然后他们会调用 color/icon 变量并为该单元格设置它们。
如果您想要 grid/table 的示例,请告诉我。 canvas 是一个更复杂的答案,但我怀疑这是您真正喜欢的。第二个示例及以后的示例正是您想要的,因此您也可以选择要插入的图标。
context = document.getElementById('canvas').getContext("2d");
$('#canvas').mousedown(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
redraw();
});
$('#canvas').mousemove(function(e){
if(paint){
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
redraw();
}
});
$('#canvas').mouseup(function(e){
paint = false;
});
$('#canvas').mouseleave(function(e){
paint = false;
});
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var paint;
function addClick(x, y, dragging)
{
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
}
function redraw(){
context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
context.strokeStyle = "#df4b26";
context.lineJoin = "round";
context.lineWidth = 5;
for(var i=0; i < clickX.length; i++) {
context.beginPath();
if(clickDrag[i] && i){
context.moveTo(clickX[i-1], clickY[i-1]);
}else{
context.moveTo(clickX[i]-1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.stroke();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id="canvas" width="490" height="220" style="border: solid 1px black;"></canvas>