[英]Why isn't the background image showing?
我想制作一個游戲,你必須翻牌才能露出背面,但我無法顯示背景圖像。 我還是 vue 的新手,所以我不知道我是否做了一些愚蠢的事情。 我認為這與我通過腳本添加背景有關。
這是 App.vue:
<template>
<div class="griglia">
<Card num="1" path="../assets/squalo.png" />
<Card num="2" path="../assets/squalo.png" />
<Card num="3" path="../assets/squalo.png" />
<Card num="4" path="../assets/squalo.png" />
<Card num="5" path="../assets/squalo.png" />
<Card num="6" path="../assets/squalo.png" />
<Card num="7" path="../assets/squalo.png" />
<Card num="8" path="../assets/squalo.png" />
<Card num="9" path="../assets/squalo.png" />
</div>
</template>
<script>
import Card from "./components/Card.vue"
export default {
name: 'App',
components: {
Card,
},
};
</script>
<style>
* {
box-sizing: border-box;
user-select: none;
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100%;
background-image: linear-gradient(#7c00e1, #4f009e);
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
color: white;
}
div.griglia {
width: 30vw;
height: 30vw;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
</style>
這是 Card.vue 組件:
<template>
<div class="el">
<div class="card" :id="'el'+num" @click="flip">
<div class="front">{{num}}</div>
<div class="back" :id="'b'+num"></div>
</div>
</div>
</template>
<script>
export default {
props: {
num: String,
path: String
},
methods: {
flip() {
document.getElementById("b" + this.num).style = 'background-image: url("' + this.path + '"); background-size: cover'
document.getElementById("el" + this.num).style = "transform: rotateY(180deg);"
}
}
};
</script>
<style scoped>
div.el {
width: 30%;
height: 30%;
cursor: pointer;
position: relative;
}
div.card {
box-shadow: 0 0 15px black;
position: absolute;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all 0.3s ease;
}
div.front {
position: absolute;
background-color: #111111;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
font-size: 3.5vw;
backface-visibility: hidden;
}
div.back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
transform: rotateY(180deg);
background-image: none;
}
</style>
document.getElementById("b" + this.num).style = 'background-image: url("' + this.path + '");
是不正確的。
這將產生typeerror
。
請改用document.getElementById("b" + this.num).style.backgroundImage = 'url("' + this.path + '")'
。
在這里查看更多
我已經能夠通過更改“squalo.png”之類的路徑並在最終的 dist 文件夾中添加 png 文件來顯示它。 可能不是最好的方法,但它確實有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.