开发一个计算器网页,卡通风格昆山

2026-10-26 12:06:14      点击:
一、项目概述

我们将使用HTML、CSS和JavaScript开发一款‌卡通风格的交互式网页计算器‌,它不仅能实现基础的加减乘除运算,还拥有圆润可爱的视觉设计、活泼的色彩搭配和流畅的交互动效,整体风格充满童趣感。

二、完整实现代码
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>卡通趣味计算器</title>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#FF9A8B',
                        secondary: '#FFCDD2',
                        accent: '#FF6B6B',
                        light: '#FFF5F7',
                        dark: '#5D4037',
                    },
                    fontFamily: {
                        cartoon: ['Comic Sans MS', 'cursive', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .btn-cartoon {
                @apply rounded-2xl border-4 border-white shadow-lg transition-all duration-200 transform hover:scale-105 active:scale-95 font-cartoon font-bold text-dark;
            }
            .calculator-shadow {
                box-shadow: 0 15px 0 #E57373, 0 20px 20px rgba(0,0,0,0.1);
            }
            .display-shadow {
                box-shadow: inset 0 4px 8px rgba(0,0,0,0.1);
            }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-pink-100 via-purple-100 to-blue-100 min-h-screen flex items-center justify-center p-4">
    <!-- 卡通计算器主体 -->
    <div class="bg-primary p-6 rounded-3xl calculator-shadow w-full max-w-md">
        <!-- 顶部可爱装饰 -->
        <div class="flex justify-center mb-4">
            <div class="w-16 h-16 bg-white rounded-full flex items-center justify-center shadow-md">
                <i class="fa fa-calculator text-3xl text-accent"></i>
            </div>
        </div>
        
        <!-- 显示区域 -->
        <div class="bg-light rounded-2xl p-4 mb-6 display-shadow border-4 border-white">
            <div id="previous-operand" class="text-right text-dark/70 text-xl min-h-[30px] font-cartoon">0</div>
            <div id="current-operand" class="text-right text-dark text-3xl font-bold font-cartoon overflow-x-auto">0</div>
        </div>
        
        <!-- 按钮区域 -->
        <div class="grid grid-cols-4 gap-3">
            <button class="btn-cartoon bg-secondary text-accent py-4 text-xl" data-action="clear">AC</button>
            <button class="btn-cartoon bg-secondary text-accent py-4 text-xl" data-action="delete">DEL</button>
            <button class="btn-cartoon bg-secondary text-accent py-4 text-xl" data-operator="/">÷</button>
            <button class="btn-cartoon bg-accent text-white py-4 text-xl" data-operator="*">×</button>
            
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>7</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>8</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>9</button>
            <button class="btn-cartoon bg-accent text-white py-4 text-xl" data-operator="-">−</button>
            
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>4</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>5</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>6</button>
            <button class="btn-cartoon bg-accent text-white py-4 text-xl" data-operator="+">+</button>
            
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>1</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>2</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>3</button>
            <button class="btn-cartoon bg-white py-4 text-xl" data-number>.</button>
            
            <button class="btn-cartoon bg-white py-4 text-xl col-span-2" data-number>0</button>
            <button class="btn-cartoon bg-accent text-white py-4 text-xl col-span-2" data-action="calculate">=</button>
        </div>
    </div>

    <script>
        class CartoonCalculator {
            constructor(previousOperandTextElement, currentOperandTextElement) {
                this.previousOperandTextElement = previousOperandTextElement
                this.currentOperandTextElement = currentOperandTextElement
                this.clear()
            }

            clear() {
                this.currentOperand = '0'
                this.previousOperand = ''
                this.operation = undefined
            }

            delete() {
                this.currentOperand = this.currentOperand.toString().slice(0, -1)
                if(this.currentOperand === '') this.currentOperand = '0'
            }

            appendNumber(number) {
                if (number === '.' && this.currentOperand.includes('.')) return
                if(this.currentOperand === '0' && number !== '.') {
                    this.currentOperand = number
                } else {
                    this.currentOperand = this.currentOperand.toString() + number.toString()
                }
            }

            chooseOperation(operation) {
                if (this.currentOperand === '') return
                if (this.previousOperand !== '') {
                    this.compute()
                }
                this.operation = operation
                this.previousOperand = this.currentOperand
                this.currentOperand = '0'
            }

            compute() {
                let computation
                const prev = parseFloat(this.previousOperand)
                const current = parseFloat(this.currentOperand)
                if (isNaN(prev) || isNaN(current)) return
                switch (this.operation) {
                    case '+':
                        computation = prev + current
                        break
                    case '-':
                        computation = prev - current
                        break
                    case '*':
                        computation = prev * current
                        break
                    case '/':
                        if(current === 0) {
                            this.currentOperand = '错误'
                            this.previousOperand = ''
                            this.operation = undefined
                            return
                        }
                        computation = prev / current
                        break
                    default:
                        return
                }
                this.currentOperand = computation
                this.operation = undefined
                this.previousOperand = ''
            }

            getDisplayNumber(number) {
                const stringNumber = number.toString()
                const integerDigits = parseFloat(stringNumber.split('.')[0])
                const decimalDigits = stringNumber.split('.')[1]
                let integerDisplay
                if (isNaN(integerDigits)) {
                    integerDisplay = ''
                } else {
                    integerDisplay = integerDigits.toLocaleString('zh', { maximumFractionDigits: 0 })
                }
                if (decimalDigits != null) {
                    return `${integerDisplay}.${decimalDigits}`
                } else {
                    return integerDisplay
                }
            }

            updateDisplay() {
                this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand)
                if(this.operation != null) {
                    this.previousOperandTextElement.innerText = 
                        `${this.getDisplayNumber(this.previousOperand)} ${this.operation.replace('*', '×').replace('/', '÷')}`
                } else {
                    this.previousOperandTextElement.innerText = ''
                }
            }
        }

        const numberButtons = document.querySelectorAll('[data-number]')
        const operationButtons = document.querySelectorAll('[data-operator]')
        const equalsButton = document.querySelector('[data-action="calculate"]')
        const deleteButton = document.querySelector('[data-action="delete"]')
        const allClearButton = document.querySelector('[data-action="clear"]')
        const previousOperandTextElement = document.querySelector('#previous-operand')
        const currentOperandTextElement = document.querySelector('#current-operand')

        const calculator = new CartoonCalculator(previousOperandTextElement, currentOperandTextElement)

        numberButtons.forEach(button => {
            button.addEventListener('click', () => {
                calculator.appendNumber(button.innerText)
                calculator.updateDisplay()
            })
        })

        operationButtons.forEach(button => {
            button.addEventListener('click', () => {
                calculator.chooseOperation(button.getAttribute('data-operator'))
                calculator.updateDisplay()
            })
        })

        equalsButton.addEventListener('click', () => {
            calculator.compute()
            calculator.updateDisplay()
        })

        allClearButton.addEventListener('click', () => {
            calculator.clear()
            calculator.updateDisplay()
        })

        deleteButton.addEventListener('click', () => {
            calculator.delete()
            calculator.updateDisplay()
        })
    </script>
</body>
</html>

三、卡通风格设计亮点
色彩体系‌:采用粉粉嫩嫩的马卡龙色系,以珊瑚粉为主色调,搭配浅粉、亮红等辅助色,营造出活泼可爱的视觉氛围,完全区别于传统计算器的冰冷风格。
造型设计‌:所有元素都使用大圆角设计,计算器主体、按钮、显示区域都做了圆润处理,搭配卡通字体Comic Sans MS,强化童趣感。
立体效果‌:通过多层阴影模拟卡通3D效果,计算器底部的下沉阴影让整体看起来像一个真实的卡通玩具,按钮点击时的缩放动效也增强了交互反馈。
细节装饰‌:顶部加入可爱的计算器图标装饰,背景使用渐变色,整体画面层次丰富又不会显得杂乱。
四、核心功能说明
支持加减乘除基础四则运算,自动处理小数输入和运算结果格式化
具备AC全清和DEL删除功能,操作逻辑符合日常使用习
亲!欢迎您阅读昆山热水器维修公司,专注于昆山太阳能维修、昆山空气能维修、昆山热水工程维修/安装/清洗/维护,真诚欢迎您-_-
QQ客服
企业官方客服QQ
QQ客服
企业官方客服QQ
加盟合作微信

| 数据统计→: