[Tugas 5 Pemrograman Web - Membuat Halaman Counter dengan JQuery]
- Hilmi Zharfan Rachmadi
- 5025201268
- Pemrograman Web Kelas B
Deskripsi Tugas
Pada pertemuan 6 yang dilaksanakan secara asinkron, kami diberikan tugas latihan untuk membuat website counter dengan JQuery berdasarkan contoh berikut
Hasil Tugas
Tampilan
- + Untuk menambah nilai
- - Untuk mengurangi nilai
- Reset untuk mereset nilai menjadi nol
Ketiga button ini saya implementasikan menggunakan jQuery.
Kode HTML beserta JQuery
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Fonts -->
<link href='https://fonts.googleapis.com/css?family=Montserrat:100,200,300,400,500,600,700,800,900' rel='stylesheet'>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<title>Counter Program</title>
<link rel="icon" href="assets/img/icon.png">
</head>
<body>
<div class="centered">
<!-- Buat h1 untuk menampilkan nilai ketika tombol tekan di klik-->
<center>
<!--Header-->
<div class="header">
<h1 id="title">Counter Program</h1>
<h2 class="subtitle">Let's Count!</h1>
</div>
<h1 class="counter">0</h1>
</center>
<!-- Buat tombol-->
<center>
<button type="button" class="btn btn-sub btn-primary">-</button>
<button type="button" class="btn btn-reset btn-primary">Reset</button>
<button type="button" class="btn btn-add btn-primary">+</button>
</center>
</div>
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
<script>
//Ketika dokumen HTML sudah siap di reload
$(document).ready(function() {
let nilai = $(".counter").text();
let subtitle = $(".subtitle").text();
//ketika tombol tekan di klik
$(".btn-add").on("click", function() {
//tambah nilai dan tampilkan pada counter
$(".counter").html(++nilai);
$(".subtitle").html("Let's Count Up!");
});
//ketika tombol tekan di klik
$(".btn-sub").on("click", function() {
//kurangi nilai dan tampilkan pada counter
if (nilai >= 1) {
$(".counter").html(--nilai);
if (nilai == 0)
$(".subtitle").html("Let's Count!");
else
$(".subtitle").html("Let's Count Down!");
};
});
//ketika tombol tekan di klik
$(".btn-reset").on("click", function() {
//kurangi nilai dan tampilkan pada counter
$(".counter").html(0);
$(".subtitle").html("Let's Count!");
});
});
</script>
</body>
</html>
Kode CSS
* {
padding: 0;
margin: 0;
font-family: 'Montserrat', sans-serif;
box-sizing: border-box;
color: white;
}
body {
background-image: url(../img/BGCounter.png);
}
.centered {
position: fixed;
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
}
#title {
font-size: 2.3rem;
font-weight: 600;
}
.subtitle {
font-size: 1.5rem;
font-weight: 400;
}
.counter {
font-size: 10rem;
font-weight: 600;
}
.btn:hover {
transition: 0.7s;
border-color: transparent;
}
.btn-add {
border-color: transparent;
font-weight: 600;
}
.btn-reset {
border-color: transparent;
font-weight: 600;
}
.btn-sub {
border-color: transparent;
font-weight: 600;
}
Tautan
- Situs: Counter Program (hilmizr.github.io)
- Repository: hilmizr/jquery-counter (github.com)
- Situs: Counter Program (hilmizr.github.io)
- Repository: hilmizr/jquery-counter (github.com)
Comments
Post a Comment