我正在尝试使用CSS Grid和jQuery,我被困在这里。我尝试按箭头键的方向展开框,我可以打印if ("color" in this.props && "size" in this.props && "shape" in this.props) {
console.log('Received all props!');
} else {
console.log('There are some missing props');
}
,但它不会更新console.log
值
grid-column-start

$(document).ready(function() {
console.log("ready!");
$(document).keypress(function(event) {
console.log(("Key: " + event.which));
});
const box = {
leftSide: $('.boxA').css('grid-column-start'),
rightSide: $('.boxA').css('grid-column-end'),
topSide: $('.boxA').css('grid-row-start'),
bottomSide: $('.boxA').css('grid-row-start'),
moveLeft: function() {
if (this.leftSide > 1) {
console.log(this.leftSide);
$('.boxA').css('grid-row-start', function() {
return box.leftSide - 1;
});
}
}
}
$(document).keydown(function(e) {
switch (e.which) {
case 37:
box.moveLeft();
break;
case 38: // up
break;
case 39: // right
break;
case 40: // down
break;
default:
return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
});

.mediumboxes {
width: 100vw;
height: 100vh;
background-color: blue;
display: grid;
grid-template-columns: repeat(20, 5%);
grid-template-rows: repeat(10, 10%);
}
.boxA {
display: block;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
background-color: red;
}
.boxB {
display: block;
grid-column: 2 / 3;
grid-row: 1 / 11;
background-color: orange;
}

我想增加<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
的值,而不再在第10行再指定整个DOM路径leftSide
。
是否可以仅提及$('boxA').css('grid-column-start', box.leftSide - 1)
?
答案 0 :(得分:0)
呀。只需this.leftSide = this.leftSide - 1;
您的代码似乎就像leftSide
是一个函数一样。假设您将其更改为仅仅是一项任务,那么代码就没有任何问题。