Vue实现简单九九乘法表
核心操作很简单,只需要两个v-for循环即可。
<table id="disp">
<tr v-for="i in 9">
<td v-for="j in i">
<button>{{j}}*{{i}}={{i*j}}</button>
</td>
</tr>
</table>
我们首先需要定义应用,然后将其挂载到设定的dom元素上。
注意,如果是在挂载的元素外部的话是无法访问到vue应用的方法的。
const Disp={
data(){
return {
showExpaination:false,
text:""
}
}
}
Vue.createApp(Disp).mount("#main");
后面是我自己做的一些完善:
首先设置样式:
@charset 'utf-8';
#main{
width: 700px;
height: 500px;
margin: auto;
background-image: url(imgs/星野愛.png);
border: 10px solid burlywood;
border-radius: 100px;
}
#disp{
display: block;
width: 600px;
height: 300px;
border: 5px solid burlywood;
border-radius: 50px;
margin:auto;
margin-top: 100px;
padding:30px;
}
button{
border: 2px solid black;
background-color: azure;
display: block;
border-radius: 100px;
width: 55px;
}
button:hover{
background-color: greenyellow;
border: 2px solid yellow;
}
textarea{
width: 50%;
height: 30px;
margin: auto;
margin-left: 180px;
margin-top: 20px;
text-align: center;
color:yellow;
font-weight: bolder;
font-size: 20px;
background-color: black;
opacity: 0.9;
border-radius: 100px;
}
通过 background_image 和 :hover 实现了增加背景,实现鼠标放在乘法表的某一个式子上时式子变化颜色,产生突出效果。
然后设计Vue应用的方法,实现鼠标经过乘法式子时,隐藏在下面的文本框出现,并写出对应的口令,如“三七二十一”、“一九得九”等。
const Disp={
data(){
return {
showExpaination:false,
text:""
}
},
methods:{
ShowExpaination:function(i,j){
let back="";
if(i*j>10){
let b=i*j%10;
if(b==0)b=10;
back=this.Trans2Upper(Math.floor(i*j/10))+"十"+this.Trans2Upper(b);
}
else back="得"+this.Trans2Upper(i*j);
this.text=this.Trans2Upper(j)+this.Trans2Upper(i)+back;
this.showExpaination=true;
},
Trans2Upper:function(i){
if(i==1)return '一';
if(i==2)return '二';
if(i==3)return '三';
if(i==4)return '四';
if(i==5)return '五';
if(i==6)return '六';
if(i==7)return '七';
if(i==8)return '八';
if(i==9)return '九';
if(i==10)return "十";
}
}
}
Vue.createApp(Disp).mount("#main");
增加两个属性,showExpaination表示是否显示文本框,text表示文本框内输出的内容。再定义两个方法,ShowExpaination(i,j)表示把当前的乘数i和乘数j相乘的式子转化为“XX得X”的形式。Trans2Upper(i)表示将数字转化为汉字数字。
然后设置对应对象的事件。
<button @mouseenter="ShowExpaination(i,j)" @mouseleave="showExpaination=!showExpaination">{{j}}*{{i}}={{i*j}}</button>
button是乘法式子的载体,设置两个事件:mouseenter鼠标进入事件和mouseleave鼠标离开事件。@是v-on的缩写,表示来监听 DOM 事件,当某个事件发生后,可以执行后续的操作。
这里是当鼠标移到式子上方时,调用ShowExpaination(i,j)显示文本框及其内容。当鼠标离开时将属性showExpaination置反。
<textarea readonly="readonly" v-show="showExpaination" >{{text}}</textarea>
文本框设置了v-show指令,当showExpaination为true时就会显示,false就会隐藏。内容为应用的text属性。
运行效果:
整体代码:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>九九乘法表</title>
<script src="Vue.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="main">
<table id="disp">
<tr v-for="i in 9">
<td v-for="j in i">
<button @mouseenter="ShowExpaination(i,j)" @mouseleave="showExpaination=!showExpaination">{{j}}*{{i}}={{i*j}}</button>
</td>
</tr>
</table>
<textarea readonly="readonly" v-show="showExpaination" >{{text}}</textarea>
</div>
<script>
const Disp={
data(){
return {
showExpaination:false,
text:""
}
},
methods:{
ShowExpaination:function(i,j){
let back="";
if(i*j>10){
let b=i*j%10;
if(b==0)b=10;
back=this.Trans2Upper(Math.floor(i*j/10))+"十"+this.Trans2Upper(b);
}
else back="得"+this.Trans2Upper(i*j);
this.text=this.Trans2Upper(j)+this.Trans2Upper(i)+back;
this.showExpaination=true;
},
Trans2Upper:function(i){
if(i==1)return '一';
if(i==2)return '二';
if(i==3)return '三';
if(i==4)return '四';
if(i==5)return '五';
if(i==6)return '六';
if(i==7)return '七';
if(i==8)return '八';
if(i==9)return '九';
if(i==10)return "十";
}
}
}
Vue.createApp(Disp).mount("#main");
</script>
</body>
</html>
style.css:
@charset 'utf-8';
#main{
width: 700px;
height: 500px;
margin: auto;
background-image: url(imgs/星野愛.png);
border: 10px solid burlywood;
border-radius: 100px;
}
#disp{
display: block;
width: 600px;
height: 300px;
border: 5px solid burlywood;
border-radius: 50px;
margin:auto;
margin-top: 100px;
padding:30px;
}
button{
border: 2px solid black;
background-color: azure;
display: block;
border-radius: 100px;
width: 55px;
}
button:hover{
background-color: greenyellow;
border: 2px solid yellow;
}
textarea{
width: 50%;
height: 30px;
margin: auto;
margin-left: 180px;
margin-top: 20px;
text-align: center;
color:yellow;
font-weight: bolder;
font-size: 20px;
background-color: black;
opacity: 0.9;
border-radius: 100px;
}
原代码已存放在Github:
git@github.com:Jyunnchi/Vue_99multiplicationTable.git