使用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>
相关推荐
英勇无比的消炎药26 分钟前
Vue 工程接入 WebMCP SDK 最佳实践:从零构建智能前端应用
vue.js
无人生还1 小时前
从 Vue3 到 React · 快速上手系列第 7 篇:副作用与生命周期 —— watch/watchEffect 如何翻译为 useEffect
前端·vue.js·react.js
带娃的IT创业者2 小时前
单文件架构的极致美学:深入解析 Bento 的 HTML 幻灯片技术实现
前端·架构·html·web开发·bento·单文件架构
游戏开发爱好者82 小时前
iOS应用加固方案全解析:源码加固与IPA在线加固对比
android·macos·ios·小程序·uni-app·cocoa·iphone
IMPYLH2 小时前
HTML 的 <dl> 元素
linux·windows·html
仿生狮子10 小时前
✂️ Nuxt 最简单的字体裁剪工具:Fontize
javascript·vue.js·nuxt.js
2301_8103131813 小时前
宠物服务APP开发宠物社交上门喂养洗护商城系统源码搭建
小程序·团队开发·软件需求·宠物
别惊醒渔人15 小时前
Vue3 Diff 优化:最长递增子序列 LIS
前端·javascript·vue.js
用户831348593069819 小时前
Vue+Three.js实现PCB电路板3D交互:元器件点击高亮、部件显隐、模型自动旋转
vue.js·webgl·three.js
GitLqr20 小时前
提升 Web 开发效率:我常用的 10 个 TypeScript 实战技巧
vue.js·react.js·typescript