Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html><!-- by [email protected] -->
- <html lang="pt-br">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Calculadora de IMC</title>
- <style>
- body{
- background-color:#e3e4e0;
- }
- .tab{
- font-family: 'Trebuchet MS', sans-serif;
- background-color:#fff;
- width: 300px;
- margin: 5px auto;
- border: 1px solid #ccc;
- border-radius: 10px;
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
- padding: 5px 20px 15px 20px;
- }
- #tabela{
- display:none;
- }
- #tabCredito{
- display:none;
- }
- #historico{
- display: none;
- }
- #tabHistorico{
- width:86%;
- height:219px;
- background-color:#eee7be;
- border:1px solid #ccc;
- padding:20px;
- overflow: auto;
- margin-bottom: 4px;
- font-size: 0.8em;
- }
- label{
- display: block;
- margin-bottom: 10px;
- text-align: center;
- }
- input[type="text"]{
- width: 84%;
- height: 25px;
- margin-bottom: 10px;
- right: 20px;
- padding: 10px;
- border: 1px solid #ccc;
- float:left;
- }
- table,td{
- border-color: #fff;
- padding:0;
- }
- table{
- width: 100%;
- }
- td{
- width: 50%;
- }
- .botao{
- color: #fff;
- height:35px;
- font-size: 16px;
- border-radius: 5px;
- cursor: pointer;
- width:48%;
- padding:0;
- margin-top:2px;
- }
- #calcular{
- background-color: #4caf50;
- border:1px solid #3e8341;
- width: 100%;
- }
- #calcular:hover{
- background-color: #3e8341;
- }
- .novoCadastro{
- background-color: #755;
- border:1px solid #000;
- }
- .novoCadastro:hover{
- background-color: rgb(85, 61, 61);
- }
- .botaoInferior{
- background-color: #3e84ee;
- border:1px solid #2220b6;
- }
- .botaoInferior:hover{
- background-color: #2220b6;
- }
- .fechar{
- background-color: #f13131;
- float:right;
- border:1px solid #991b0a;
- }
- .fechar:hover{
- background-color: #991b0a;
- }
- #resultado{
- margin-top: 10px;
- font-size: 18px;
- font-weight: bold;
- background-color: #d5dee6;
- border:1px solid #7db2e0;
- border-radius: 5px;
- padding: 15px 0px 15px 0px;
- display: none;
- }
- h2, h1{
- background-color: #eee;
- }
- .magrezaGrauIII, .obesidadeGrauII{
- background-color:#ccc;
- border:1px solid #aaa;
- }
- .magrezaGrauII,.obesidadeGrauI{
- background-color:#ddd;
- border:1px solid #bbb;
- }
- .magrezaGrauI,.sobrepeso{
- background-color:#eee;
- border:1px solid #ccc;
- }
- .normal{
- border:1px solid #ddd;
- }
- .obesidadeGrauIII{
- background-color:#bbb;
- border:1px solid #999;
- }
- .tabelaIMC{
- padding:5px;
- border-radius: 5px;
- margin: 5px 0 5px 0;
- }
- .labelPeso, .labelAltura{
- color:#ad0303;
- }
- .imagem{
- position:relative;
- float:left;
- margin-top:-60px;
- }
- img{
- width:70px;
- height:50px;
- background-color: #eee;
- border:none;
- }
- a{
- text-decoration: none;
- color:#000;
- }
- .tabelaCredito{
- width: 98%;
- border-radius: 5px;
- padding:5px 0 5px 5px;
- margin:5px 0;
- }
- .link{
- background-color: #eee;
- border:1px solid #ccc;
- }
- </style>
- </head>
- <body>
- <div id="formulario" class="tab">
- <center><h1>Calculadora de IMC</h1></center>
- <center><h2>Cláudio Brito</h2></center>
- <div id="imagem" style="display:none;" >
- <a href="https://youtu.be/mDaa9ty23e8" target="_blank"><img class="imagem" src="https://i.pinimg.com/564x/c9/ae/14/c9ae1482ccf3b4503d6c9fff41b999f1.jpg" /></a>
- </div>
- <table>
- <tr>
- <td>
- <label for="peso" class="labelPeso">Peso max. 250</label>
- <input type="text" id="peso" placeholder="Digite aqui o peso" maxlength="4" inputmode="numeric" required
- oninput="validaPeso(this.value)"
- onclick="limpaResultado();this.value = '';">
- </td>
- <td>
- <label for="altura" class="labelAltura">Altura max. 2,99</label>
- <input type="text" id="altura" placeholder="Digite aqui a altura" inputmode="numeric" maxlength="4" required
- oninput="validaAltura(this.value);"
- onkeyup="alturaMaxima(this.value);"
- onclick="limpaResultado();this.value = '';" >
- </td>
- </tr>
- <tr>
- <td>
- <label for="nome">Nome</label>
- <input type="text" id="nome" maxlength="50" placeholder="Digite aqui o nome"
- oninput="formataNome(this.value)"
- onclick="this.value = '';">
- </td>
- <td>
- <label for="idade">Idade max. 130</label>
- <input type="text" id="idade" max="3" inputmode="numeric" maxlength="3" placeholder="Digite aqui a idade"
- oninput="validaIdade(this.value)"
- onkeyup="idadeMaxima(this.value)"
- onclick="this.value = '';" >
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <button id="calcular" class="botao" >Calcular IMC</button>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <button class="botao novoCadastro" onclick="limpaConsulta()" >Limpar</button>
- <button class="botao botaoInferior" onclick="mostraHistorico()">Histórico</button>
- </td>
- <td>
- <button class="botao botaoInferior" onclick="tabCredito()">Créditos</button>
- <button class="botao botaoInferior" onclick="mostrar()">Tabela</button>
- </td>
- </tr>
- </table>
- <center><div id="resultado"></div></center>
- </div>
- <div id="tabela" class="tab">
- <center><h1>Tabela de IMC</h1></center>
- <p class="tabelaIMC obesidadeGrauIII">Acima de 40 Obesidade grau III</p>
- <p class="tabelaIMC obesidadeGrauII">Entre 35 e 40 Obesidade grau II</p>
- <p class="tabelaIMC obesidadeGrauI">Entre 30 e 34,9 Obesidade grau I</p>
- <p class="tabelaIMC sobrepeso">Entre 25 e 29,9 Sobrepeso</p>
- <p class="tabelaIMC normal">Entre 18,5 e 24,9 Normal</p>
- <p class="tabelaIMC magrezaGrauI">Entre 17 e 18.4 Magreza grau I</p>
- <p class="tabelaIMC magrezaGrauII">Entre 16 e 16,9 Magreza grau II</p>
- <p class="tabelaIMC magrezaGrauIII">Abaixo de 15,9 Magreza grau III</p>
- <table>
- <tr>
- <td></td>
- <td>
- <button class="botao fechar" onclick="fechar()">Fechar</button>
- </td>
- </tr>
- </table>
- </div>
- <div id="tabCredito" class="tab">
- <center><h1>Créditos</h1></center>
- <p class="tabelaCredito">Criado em maio/2025 por Cláudio Brito</p>
- <p class="tabelaCredito">Contato - Whatsapp (81) 9 9924-6724</p>
- <p class="tabelaCredito">Clique abaixo, e siga-me redes sociais</p>
- <p class="tabelaCredito link"><a href="https://threads.net/@xavier.brito" target="_blank">Threads - Xavier Brito</a></p>
- <p class="tabelaCredito link"><a href="https://www.instagram.com/xavier.brito/" target="_blank">Instagram - Xavier Brito</a></p>
- <p class="tabelaCredito link"></a><a href="https://m.facebook.com/claudio.xavier.37669/" target="_blank">Facebook - Cláudio Brito</a></p>
- <p class="tabelaCredito link"><a href="https://youtube.com/@cteclaudiobritocaruaru-pe9889?si=JanZY_EJXQixKK3C" target="_blank">Youtube - CTE Cláudio Brito</a></p>
- <p class="tabelaCredito link"><a href="https://www.instagram.com/oficina.c.t.eletronica/" target="_blank">Instagram - Oficina C.T.Eletrônica</a></p>
- <table>
- <tr>
- <td></td>
- <td>
- <button class="botao fechar" onclick="fechar()">Fechar</button>
- </td>
- </tr>
- </table>
- </div>
- <div id="historico"class="tab">
- <center><h1>Histórico</h1></center>
- <div id="tabHistorico"></div>
- <table>
- <tr>
- <td>
- <button class="botao novoCadastro" onclick="limparHistorico()" >Limpar </button>
- <button class="botao botaoInferior" onclick="salvarDados()" >Salvar </button>
- </td>
- <td>
- <button class="botao fechar" onclick="fechar()">Fechar</button>
- </td>
- </tr>
- </table>
- </div>
- <script>
- const botaoCalcular = document.getElementById("calcular");
- const resultado = document.getElementById("resultado");
- function limpaResultado(){
- document.getElementById('resultado').innerText = '';
- document.getElementById('resultado').style.display='none';
- }
- /******************************************************************* /
- / /
- / /
- / CAMPO PESO /
- / /
- / /
- /********************************************************************/
- function validaPeso(peso){// oninput
- let inputPeso = document.getElementById('peso');
- document.getElementById('peso').value = peso.replace(/[^0-9,]/g,'').replace(/(\..*?)\..*/g,'$1');
- if(peso>250){
- document.getElementById('peso').value = '';
- };
- if(peso.substr(0) === '0' || peso.substr(0) === ','){
- document.getElementById("peso").value = '';
- };
- if(peso.substr(3,1) === ','){
- inputPeso.maxLength = 5;
- }else{
- inputPeso.maxLength = 4;
- };
- inputPeso.addEventListener('input', function(event){
- let v = this.value;
- let m = v.match(/,/g);
- if(m && m.length >1){
- this.value = v.substring(0, v.indexOf(',')+1)
- + v.substring(v.indexOf(',')+1).replace(/,/g,'');
- }
- });
- };
- /******************************************************************* /
- / /
- / /
- / CAMPO ALTURA /
- / /
- / /
- /********************************************************************/
- function alturaMaxima(alt){// onkeyup
- if(alt.startsWith(3) || alt.startsWith(4) || alt.startsWith(5) || alt.startsWith(6) || alt.startsWith(7) || alt.startsWith(8) || alt.startsWith(9)){
- document.getElementById("altura").value = '';
- }
- if(alt.startsWith('0')){
- document.getElementById("altura").value = '0,'+ alt.substr(2);
- }
- }
- const zero = document.getElementById('altura');
- zero.addEventListener('keydown',(e)=>{
- const valor = zero.value;
- const ehZero = e.key === '0';
- if (valor.length === 2 && ehZero){
- e.preventDefault();
- }
- })
- function validaAltura(altura){// oninput
- altura = altura.replace(/[^0-9]/g,'').replace(/(\..*?)\..*/g,'$1');
- altura = altura.replace(/\B(?=(\d{1})+(?!\d))/g, ",");
- document.getElementById("altura").value = altura;
- let inputAltura = document.getElementById('altura');
- inputAltura.addEventListener('input', function(event){
- let v = this.value;
- let m = v.match(/,/g);
- if(m && m.length >1){
- this.value = v.substring(0, v.indexOf(',')+1)
- + v.substring(v.indexOf(',')+1).replace(/,/g,'');
- }
- });
- }
- /******************************************************************* /
- / /
- / /
- / CAMPO NOME /
- / /
- / /
- /********************************************************************/
- function formataNome(nome){// oninput
- nome = nome.replace(/[^a-zA-Z ]/g,'');
- nome = nome.toUpperCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/\s+/g, ' ');
- document.getElementById('nome').value = nome;
- if(nome.startsWith(' ')){
- document.getElementById('nome').value = '';
- };
- }
- /******************************************************************* /
- / /
- / /
- / CAMPO IDADE /
- / /
- / /
- /********************************************************************/
- function idadeMaxima(old){// onkeyup
- if(old.startsWith('0')){
- document.getElementById("idade").value = '0,'+ old.substr(2);
- }
- }
- const zeroIdade = document.getElementById('idade');
- zeroIdade.addEventListener('keydown',(e)=>{
- const valorIdade = zeroIdade.value;
- const ehZeroIdade = e.key === '0';
- if (valorIdade.startsWith('0') && valorIdade.length === 2 && ehZeroIdade){
- e.preventDefault();
- }
- });
- function validaIdade(idade){// oninput
- if(idade.startsWith('0')){
- idade = idade.replace(/[^0-9,]/g,'').replace(/(\..*?)\..*/g,'$1');
- }else{
- idade = idade.replace(/[^0-9]/g,'').replace(/(\..*?)\..*/g,'$1');
- }
- document.getElementById("idade").value = idade;
- if(idade>130){
- document.getElementById('idade').value = '';
- };
- let inputIdade = document.getElementById('idade');
- inputIdade.addEventListener('input', function(event){
- let v = this.value;
- let m = v.match(/,/g);
- if(m && m.length >1){
- this.value = v.substring(0, v.indexOf(',')+1)
- + v.substring(v.indexOf(',')+1).replace(/,/g,'');
- }
- });
- }
- /******************************************************************* /
- / /
- / /
- / INICIO DO PROCESSO DO CALCULAR O IMC E SALVAR AS INFORMACOES /
- / /
- / /
- /*** funcao que calcula o IMC calculando o peso e a altura *********/
- botaoCalcular.addEventListener("click", (e) =>{
- e.preventDefault();
- const peso2 = document.getElementById("peso");
- const altura2 = document.getElementById("altura");
- const nome = document.getElementById("nome").value;
- const idade = document.getElementById("idade").value;
- // pega a data atual numa variavel
- let dataAtual = new Date().toLocaleString();
- // array que vai coletar as informacoes salvas e as novas
- let arrayHistorico = [];
- if(peso2.value < 2){
- alert("Peso m\u00ednimo 2 que \u00e9 o mesmo que 2 kg");
- return false;
- }
- if(altura2.value.startsWith('0')){
- if(altura2.value.substr(0) === '0,' || altura2.value.substr(2,1) === '1' || altura2.value.substr(2,1) === '2' || altura2.value.substr(2,1) === '3'){
- alert("Altura m\u00ednima e 0,4 que \u00e9 o mesmo que 40 cm");
- return false;
- }
- }
- if(nome !== '' && nome.length < 2){
- alert("Nome precisa ter pelo menos dois caracteres");
- return false;
- }
- if(idade !== ''){
- if(idade.substr(0) === '0,'){
- alert("Idade inv\u00e1lida");
- return false;
- }
- }
- // se houver valor nos campos peso e altura, entra aqui
- if(peso2.value !== '' && altura2.value !== ''){
- // permite um valor flutuante no campo peso e troca a virgula pelo ponto
- const peso = parseFloat(document.getElementById("peso").value.replace(",","."));
- // permite um valor flutuante no campo altura e troca a virgula pelo ponto
- const altura = parseFloat(document.getElementById("altura").value.replace(",","."));
- // caucula o IMC
- const imc = peso / (altura * altura);
- // se o campo peso e altura estiverem preenchidos, mostra o resultado, segunda validacao
- if(peso2.value.length>=1 && altura2.value.length>=1){
- // mostra a div do resultado
- document.getElementById("resultado").style.display="block";
- // puxa a classificacao de acordo com o resultado do imc
- const classificacao = classificarIMC(imc);
- // mostra o resultado na div com virgula ao inves de ponto
- resultado.innerText = `IMC: ${imc.toFixed(2).replace(".",",")} - ${classificacao}`;
- // busca encontrar o local expecifico onde vai salvar as informacoes
- if(localStorage.myHistory){
- // caso o local exista, pega as informacoes contida e coloca no array que foi criado
- arrayHistorico = JSON.parse(localStorage.getItem('myHistory'));
- };
- // com as informacoes coletadas do local storage, agora acrescenta as informacoes dos campos no array
- arrayHistorico.push('<center style="color:brown;background-color:white;padding:5px 5px 5px 5px; border-radius:5px;">--- NOVO HISTÓRICO ---</center>');
- arrayHistorico.push('Nome : ' + nome);
- arrayHistorico.push('Idade : ' + idade);
- arrayHistorico.push('Peso : ' + peso.toFixed(1).replace(".",","));
- arrayHistorico.push('Altura : ' + altura.toFixed(2).replace(".",","));
- // mostra apenas as duas casas decimais do resultado
- arrayHistorico.push('IMC : ' + imc.toFixed(2).replace(".",","));
- arrayHistorico.push(classificacao);
- arrayHistorico.push('Data : ' + dataAtual) ;
- // salva tudo como uma unica informacao, substituindo as informacoes antigas no local especifico
- localStorage.myHistory = JSON.stringify(arrayHistorico);
- }else{
- // se o campo peso ou altura estiverem em branco
- alert("Campo em vermelho, em branco");
- }
- }else{
- alert("Campo em vermelho, em branco");
- }
- });
- /************************************************************************** /
- / /
- / /
- / ***** INICIO DO PROCESSO DE CLASSIFICACAO DO RESULTADO DO IMC ***** /
- / /
- / /
- /*** funcao que classifica o estado de saude de acordo com o IMC encontrado */
- function classificarIMC(imc){
- if(imc < 16){
- return "Magreza grau III";
- }else if(imc >= 16 && imc < 17){
- return "Magreza grau II";
- }else if(imc >=17 && imc < 18.5){
- return "Magreza grau I";
- }else if(imc >= 18.5 && imc < 25){
- return "Normal";
- }else if(imc >= 25 && imc < 30){
- return "Sobrepeso";
- }else if(imc >= 30 && imc < 35){
- return "Obesidade Gral I";
- }else if(imc >= 35 && imc < 40){
- return "Obesidade Gral II";
- }else{
- return "Obesidade Gral III";
- }
- }
- /************************************************************************** /
- / /
- / /
- / ************ INICIO DA FUNCAO DE LIMPAR A ACONSULTA **************** /
- / /
- / /
- /*** funcao que limpa a consulta nos campos ********************************/
- function limpaConsulta(){
- if(confirm ("Deseja limpar tudo?")){
- // seleciona todos os campos input
- const inputs = document.querySelectorAll('input');
- // faz uma varredura em todos os inputs
- inputs.forEach(input => {
- // atribui o valor branco para cada um deles
- input.value = '';
- })
- // chama a funcao que limpa o resultado
- limpaResultado();
- }else{
- // se nao for confirmada, retorna sem limpar os campos
- return false;
- }
- }
- /************************************************************************** /
- / /
- / /
- / ************** INICIO DA FUNCAO MOSTRAR HISTORICO **************** /
- / /
- / /
- /*** funcao que mostra o historico *****************************************/
- function mostraHistorico(){
- document.getElementById("formulario").style.display="none";
- document.getElementById("historico").style.display="block";
- // se houver informacao no local especifico do local storage, entra aqui
- if(localStorage.myHistory !==''){
- // cria uma variavel para mostrar o conteudo do local storage numa div
- let resultDIV = document.getElementById('tabHistorico');
- // se houver um local especifico no local storage, coleta as informacoes, segunda validacao
- if(localStorage.myHistory){
- // apaga as informacoes na div onde vai mostrar as informacoes atualizadas
- resultDIV.innerHTML = "";
- // pega as informacoes do local storage e coloca no array
- arrayHistorico = JSON.parse(localStorage.getItem('myHistory'));
- // com um loop for, cria uma lista e coloca na variavel que vai mostrar as informacoes na div
- for(let i in arrayHistorico){
- let p = document.createElement("p");
- p.innerHTML = arrayHistorico[i];
- resultDIV.append(p);
- }
- }
- }else{
- // se nao houver informacao, mostra a mensgem
- alert('Nenhuma informa\u00e7\u00e3o gravada');
- // chama a funcao fechar
- fechar();
- }
- };
- /************************************************************************** /
- / /
- / /
- / ************** INICIO DA FUNCAO LIMPAR HISTORICO ***************** /
- / /
- / /
- /*** limpa o historico no local storage ************************************/
- function limparHistorico(){
- if(confirm ("Deseja limpar o hist\u00f3rico?")){
- // se for confirmada, apaga todas as informacoes no local especifico no local storage
- localStorage.myHistory = "";
- // refresh na pagina
- location.reload();
- }else{
- // se nao for confirmada, retorna sem apagar o historico
- return false;
- }
- }
- /************************************************************************** /
- / /
- / /
- / ***************** INICIO DAS FUNCOES MENORES ********************* /
- / /
- / /
- /*** funcao do botao mostrar tabela ***************************************/
- function mostrar(){
- document.getElementById("tabela").style.display="block";
- document.getElementById("formulario").style.display="none";
- }
- // funcao do botao mostrar credito
- function tabCredito(){
- document.getElementById("tabCredito").style.display="block";
- document.getElementById("formulario").style.display="none";
- }
- // funcao do botao mostrar historico
- function mostrarHistorico(){
- document.getElementById("formulario").style.display="none";
- document.getElementById("historico").style.display="block";
- }
- // funcao do botao fechar
- function fechar(){
- document.getElementById("tabela").style.display="none";
- document.getElementById("tabCredito").style.display="none";
- document.getElementById("historico").style.display="none";
- document.getElementById("formulario").style.display="block";
- }
- /************************************************************************** /
- / /
- / /
- / ******* INICIO DO PROCEDIMENTO PARA MOSTRAR A IMAGEM ************* /
- / /
- / /
- /*** busca a imagem na internet ********************************************/
- let image = document.createElement('img');
- image.src="https://i.pinimg.com/564x/c9/ae/14/c9ae1482ccf3b4503d6c9fff41b999f1.jpg";
- // mostra a div da imagem se estiver conectado a internet
- image.onload = function(){
- document.getElementById("imagem").style.display = "block";
- }
- // esconde a div da imagem se nao estiver conectado a internet
- image.onerror = function(){
- document.getElementById("imagem").style.display = "none";
- }
- /******************************************************************* /
- / /
- / /
- / SALVAR DADOS EM ARQUIVO TXT /
- / /
- / /
- /********************************************************************/
- function salvarDados(){
- if(confirm ("Deseja salvar o hist\u00f3rico em arquivo de texto?")){
- const dados = localStorage.getItem('myHistory');
- const blob = new Blob([dados],{type: 'text/plain'});
- const link = document.createElement('a');
- link.href = URL.createObjectURL(blob);
- link.download = 'dados_imc.txt';
- link.click();
- }else{
- return false;
- }
- }
- </script>
- </body>
- </html>
Advertisement
Advertisement