Pada artikel ini, kami akan memperbesar gambar saat diklik. Ada dua metode yang umum digunakan yang dapat digunakan untuk mengubah ukuran gambar saat diklik menggunakan javascript. Fungsi javascript dapat dipanggil dengan menentukan title=”function_name()”
Metode 1. mengubah. skala().
Properti transform digunakan untuk memodifikasi bentuk, ukuran, atau posisi suatu elemen. Dengan menentukan nilai skala, ukuran elemen dapat dimodifikasi sesuai rasio yang diberikan.
Sintaksis
object.style.transform = scale(sx); object.style.transform = scale(sx, sy);Jika sy tidak disediakan, nilai defaultnya adalah sx, yang menghasilkan penskalaan seragam dan mempertahankan rasio aspek elemen
Mendekati
- Dapatkan pemilih gambar yang diperlukan menggunakan. getElementById(pemilih)
- Atur rasio di mana gambar perlu diperbesar menggunakan. gaya. transformasi = “skala(nilai)”
- Efek animasi dapat ditambahkan menggunakan. gaya. transisi untuk memberikan tampilan yang menarik
- Ketika fungsi dipanggil menggunakan the. title() pada tab gambar, ukuran gambar akan meningkat secara proporsional sesuai skala yang diberikan.
Contoh. Contoh ini menunjukkan pendekatan yang dijelaskan di atas
html
<div class="container"
style=object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);0object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);3
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);6 object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);7=
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_9
________56______0<1=<3 <4=<6 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<div0 div1div0 div1div0 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2div7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2________56__58_______0 <1=class3class4class0object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
________58______7divobject.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
=0
=1
<=3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_2=6
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2=8
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2"container"0
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2"container"2
"container"3"container"4
"container"3"container"6
"container"3"container"8
"container"3 0
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 2
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 4
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 6
"container"3 8
"container"3style0
"container"3 0
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 2
class7=3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
Keluaran
Ubah ukuran gambar menggunakan transformasi. skala()
Metode 2. Tinggi dan Lebar CSS
Sintaksis
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_Properti tinggi dan lebar CSS menentukan tinggi dan lebar elemen. Dengan menggunakan ini kita dapat secara eksplisit menentukan dimensi yang kita inginkan dalam gambar yang diperbesar (atau diperkecil).
Mendekati
- Dapatkan pemilih gambar yang diperlukan menggunakan. getElementById(pemilih)
- Tetapkan nilai lebar/tinggi yang seharusnya digunakan oleh gambar yang diperbesar. gaya. lebar
- Disarankan untuk mempertahankan nilai dimensi lain secara otomatis, untuk menjaga rasio aspek gambar
- Efek animasi dapat ditambahkan menggunakan. gaya. transisi untuk memberikan tampilan yang menarik
- Ketika fungsi dipanggil menggunakan the
Contoh. Contoh ini menunjukkan penggunaan pendekatan yang dijelaskan di atas
html
<div class="container" =3
=4=5
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2=7
=8=9
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);6 object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);7=
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_9
________56______0<1=<3 <4=<6 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<div0 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<div0 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<div0 <7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2div7
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2<class0 <1=class3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);34
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2class7class0object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
________58______7divobject.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
=0
=1
<=3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2=8
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_2object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);50
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2"container"2
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);54
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);56
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);58
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 2
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);_2object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);62
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 6
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);66
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);56
"container"3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);58
object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);2 2
class7=3object.style.width = value(%, px, em, auto, etc); object.style.height= value(%, px, em, auto, etc);1
Keluaran
Ubah ukuran gambar menggunakan gaya. lebar
Browser yang Didukung.
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
CSS digunakan untuk pengembangan halaman web dengan menata situs web dan aplikasi web. JavaScript terkenal untuk pengembangan halaman web tetapi juga digunakan di berbagai lingkungan non-browser. Anda dapat mempelajari CSS dan Javascript dari tautan yang diberikan di bawah ini