使用VUE语法的HTML小程序(计数器)

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="js/vue.js"></script>
    <style>
        .screen {
            width: 100px;
            height: 60px;
            background-color: #FEDFE1;
            position: relative;
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
        }
    </style>
</head>
<body>
<div id="app">
    <div class="screen">{{ number }}</div>
	<input type="button" :value="buttonText" @click="toggleCounter" />
	<input type="button" value="复位" @click="resetCounter" />
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            number: 0,
            timer: null,
            isRunning: false,
            buttonText: '开始'
        },
        methods: {
            toggleCounter: function() {
                if (!this.isRunning) {
                    this.isRunning = true;
                    this.buttonText = '停止';
                    // 从1开始计数
                    this.number = 1;
                    this.timer = setInterval(() => {
                        // 计数递增
                        this.number++;
                    }, 1000);
                } else {
                    clearInterval(this.timer);
                    this.isRunning = false;
                    this.buttonText = '开始';
                }
            },
            resetCounter: function() {
                clearInterval(this.timer);
                this.number = 0;
                this.isRunning = false;
                this.buttonText = '开始';
            }
        }
    });
</script>
</body>
</html>
相关推荐
Howrun7776 小时前
VSCode烦人的远程交互UI讲解
ide·vue.js·vscode
2501_916008898 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
webYin8 小时前
解决 Uni-App 运行到微信小程序时 “Socket合法域名校验出错” 问题
微信小程序·小程序·uni-app
小迷糊的学习记录8 小时前
Vuex 与 pinia
前端·javascript·vue.js
利刃大大9 小时前
【Vue】Element-Plus快速入门 && Form && Card && Table && Tree && Dialog && Menu
前端·javascript·vue.js·element-plus
小毛驴85010 小时前
Vue 路由示例
前端·javascript·vue.js
TT哇12 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
星光不问赶路人14 小时前
vue3使用jsx语法详解
前端·vue.js
weixin79893765432...14 小时前
Vue 组件的更新过程(编译系统 + 响应式系统 + 虚拟 DOM & Diff)
vue.js
我是伪码农15 小时前
Vue 智慧商城项目
前端·javascript·vue.js