我正在创建一个项目,我的标记根据骰子的值移动。 以下功能负责该操作。
var markerMoveFunc = () => {
var incrementDice = 0;
var diceIncrementer = () => {
let dice = Math.floor(Math.random() * 6) + 1;
let diceDOM = document.querySelector('.dice');
diceDOM.style.display = 'block';
diceDOM.src = '/static' + '/images' + '/dice-' + dice + '.png';
incrementDice += dice;
};
document
.querySelector('.roll-btn')
.addEventListener('click', diceIncrementer);
var firstChild = document.querySelector('#container').childNodes[
incrementDice
];
firstChild.innerHTML += `<div class='marker'><i class="fas fa-map-marker"></i></div>`;
};
整个代码
function init() {
const cards = [
{ front: 'Apple', back: 'Яблоко' },
{ front: 'Cherry', back: 'Вишня' },
{ front: 'Melon', back: 'Арбуз' },
{ front: 'Car', back: 'Машина' },
{ front: 'Book', back: 'Книга' },
{ front: 'Pen', back: 'Ручка' },
{ front: 'Nice', back: 'Прекрасный' },
{ front: 'TV', back: 'Телевизор' },
{ front: 'Mobile', back: 'Мобильный' },
{ front: 'Curious', back: 'Любопытный' }
];
var container = document.getElementById('container');
const createCard = () => {
for (var card in cards) {
// Create an item
var item = document.createElement('div');
// Add class to item div
item.className = 'item';
// Create flip card inner elem
var flipCardInner = document.createElement('div');
// Add 'flip-card-inner' class
flipCardInner.className = 'flip-card-inner';
// Create front card elem
var frontCardElem = document.createElement('div');
// Add 'flip-card-front' class
frontCardElem.className = 'flip-card-front';
// Creat front card text
var frontCardText = document.createTextNode(cards[card].front);
// Append front card text to frontcard element
frontCardElem.appendChild(frontCardText);
// Create back card elem
var backCardElem = document.createElement('div');
// Add 'flip-card-front' class
backCardElem.className = 'flip-card-back';
// Creat back card text
var backCardText = document.createTextNode(cards[card].back);
// Append front card text to frontcard element
backCardElem.appendChild(backCardText);
// Append front card elem to flip card inner
flipCardInner.appendChild(frontCardElem);
// Append back card elem to flip card inner
flipCardInner.appendChild(backCardElem);
// Append flip card inner to item
item.appendChild(flipCardInner);
container.appendChild(item);
}
};
createCard();
var markerMoveFunc = () => {
var incrementDice = 0;
var diceIncrementer = () => {
let dice = Math.floor(Math.random() * 6) + 1;
let diceDOM = document.querySelector('.dice');
diceDOM.style.display = 'block';
diceDOM.src = '/static' + '/images' + '/dice-' + dice + '.png';
incrementDice += dice;
};
document
.querySelector('.roll-btn')
.addEventListener('click', diceIncrementer);
var firstChild = document.querySelector('#container').childNodes[
incrementDice
];
firstChild.innerHTML += `<div class='marker'><i class="fas fa-map-marker"></i></div>`;
};
markerMoveFunc();
$('.item .flip-card-inner').click(function() {
$(this)
.closest('.item')
.toggleClass('hover');
$(this).css('transform, rotateX(180deg)');
});
document.querySelector('.dice').style.display = 'none';
var radius = 250; // adjust to move out items in and out
var inclination = 0;
var fields = $('.item'),
container = $('#container'),
width = container.width(),
height = container.height();
var angle = 0,
step = (2 * Math.PI) / fields.length;
fields.each(function() {
var x = Math.round(
width / 2 + radius * Math.cos(angle) - $(this).width() / 2
);
var y = Math.round(
height / 2 + radius * Math.sin(angle) - $(this).height() / 2
);
$(this).css({
left: x + 'px',
top: y + 'px',
WebkitTransform: 'rotate(' + inclination + 'deg)'
});
angle += step;
inclination += 0;
});
}
init();
HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Ed game</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"
integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr"
crossorigin="anonymous"
/>
</head>
<body>
<div id="container"></div>
<div class="dice-holder">
<img src="static/images/dice-1.png" alt="dice" class="dice" />
<Button class="roll-btn">Roll dice</Button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/scripts/game.js"></script>
</body>
</html>
我不明白为什么增量骰子不更新。你能给我个建议吗?