使用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>
相关推荐
2503_9284115628 分钟前
12.15 element-plus的一些组件(上)
前端·vue.js
m0_740043731 小时前
Vue2 语法糖简洁指南
前端·javascript·vue.js
CV_J1 小时前
淘汰赛对阵图生成demo
vue.js
Java.熵减码农1 小时前
基于VueCli自定义创建项目
前端·javascript·vue.js
史上最菜开发1 小时前
Ant Design Vue V1.7.8版本,a-input 去空格
javascript·vue.js·anti-design-vue
前端不太难2 小时前
Vue Router 权限系统设计实战
前端·javascript·vue.js
醉挽清风7832 小时前
Vue+Djiango基础用法
前端·javascript·vue.js
精神病不行计算机不上班2 小时前
[Java Web]在IDEA中完整实现Servlet的示例
java·servlet·tomcat·html·intellij-idea·web
顾安r3 小时前
12.17 脚本工具 自动化全局跳转
linux·前端·css·golang·html
菠菜盼娣3 小时前
Eslint 用法
vue.js