Efek Kaca Pembesar Menggunakan jQuery

Magnify glass for image zoom using Jquery and CSS3. Yaitu efek kaca pembesar untuk memperbesar gambar.

Efek kaca pembesar atau lup ini akan memberi efek memperbesar gambar saat di-hover. Persis seperti saat Anda menggunakan kaca pembesar untuk mengamati sebuah benda. Efeknya benar-benar nyata. Agar lebih jelas lihat demo dibawah-arahkan mouse ke atas gambar.






Prinsipnya, properti box-shadow dan border-radius CSS3 digunakan untuk membuat kaca pembesar. Jquery digunakan untuk mengatur koordinat posisi kursor dan memposisikan gambar (yang berukuran sebenarnya) tepat sesuai koordinat tadi.

Gambar yang tampil pada halaman ini berukuran 640px, merupakan gambar kecilnya-diatur melalui CSS-, inilah gambar yang tampil saat tidak di-hover. Namun, pada saat di-hover, gambar dalam ukuran asli-gambar besarnya (1280px)-ditampilkan. Gambar besar yang ditampilkan tentu hanya sebagian yaitu seukuran kaca pembesar yang menutupi gambar kecil. Jadi tidak benar-benar memperbesar gambar dalam arti yang sesungguhnya :)

Anda akan lebih mengerti jika telah mempraktikkannya. Silahkan.

Ingin mencoba?

HTML, CSS, dan jQuery untuk menciptakan efek kaca pembesar atau magnify glass


HTML

<div class="pembesaran">
<!-- Ini efek kaca pembesar, akan menampilkan gambar yang besar/ukuran aslinya -->
<div class="besar"></div>
 
<!-- Ini gambar kecilnya -->
<img class="kecil" src="https://wellcomecollection.files.wordpress.com/2013/04/ns-sem-head.jpg" width="500"/>    
</div>

CSS
.pembesaran {width: 500px; margin: 50px auto; position: relative;}

/*kaca pembesar*/
.besar{
 width: 175px; height: 175px;
 position: absolute;
 -webkit-border-radius: 100%;
 -moz-border-radius: 100%;
 border-radius: 100%;
 /*Multiple box shadows to achieve the glass effect*/
 -webkit-box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 
 0 0 7px 7px rgba(0, 0, 0, 0.25), 
 inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
 
  -moz-box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 
 0 0 7px 7px rgba(0, 0, 0, 0.25), 
 inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
 
  box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 
 0 0 7px 7px rgba(0, 0, 0, 0.25), 
 inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
 
 /*Muat gambar ukuran asli*/
 background: url('https://wellcomecollection.files.wordpress.com/2013/04/ns-sem-head.jpg') no-repeat;
 
 /*sembunyikan lup*/
 display: none;
}

/*Mengatasi masalah overlap saat pembesaran*/
.kecil { display: block; }


jQuery
// JavaScript Document

$(document).ready(function(){

 var native_width = 0;
 var native_height = 0;

 //Fungsi pergerakan mouse
 $(".pembesaran").mousemove(function(e){
  //When the user hovers on the image, the script will first calculate
  //the native dimensions if they don't exist. Only after the native dimensions
  //are available, the script will show the zoomed version.
  if(!native_width && !native_height)
  {
   //This will create a new image object with the same image as that in .small
   //We cannot directly get the dimensions from .small because of the 
   //width specified to 200px in the html. To get the actual dimensions we have
   //created this image object.
   var image_object = new Image();
   image_object.src = $(".small").attr("src");
   
   //This code is wrapped in the .load function which is important.
   //width and height of the object would return 0 if accessed before 
   //the image gets loaded.
   native_width = image_object.width;
   native_height = image_object.height;
  }
  else
  {
   //x/y coordinates of the mouse
   //This is the position of .magnify with respect to the document.
   var magnify_offset = $(this).offset();
   //We will deduct the positions of .magnify from the mouse positions with
   //respect to the document to get the mouse positions with respect to the 
   //container(.pembesaran)
   var mx = e.pageX - magnify_offset.left;
   var my = e.pageY - magnify_offset.top;
   
   //Finally the code to fade out the glass if the mouse is outside the container
   if(mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0)
   {
    $(".besar").fadeIn(100);
   }
   else
   {
    $(".besar").fadeOut(100);
   }
   if($(".besar").is(":visible"))
   {
    //The background position of .large will be changed according to the position
    //of the mouse over the .small image. So we will get the ratio of the pixel
    //under the mouse pointer with respect to the image and use that to position the 
    //large image inside the magnifying glass
    var rx = Math.round(mx/$(".kecil").width()*native_width - $(".besar").width()/2)*-1;
    var ry = Math.round(my/$(".kecil").height()*native_height - $(".besar").height()/2)*-1;
    var bgp = rx + "px " + ry + "px";
    
    //Time to move the magnifying glass with the mouse
    var px = mx - $(".besar").width()/2;
    var py = my - $(".besar").height()/2;
    //Now the glass moves with the mouse
    //The logic is to deduct half of the glass's width and height from the 
    //mouse coordinates to place it with its center at the mouse coordinates
    
    //If you hover on the image now, you should see the magnifying glass in action
    $(".besar").css({left: px, top: py, backgroundPosition: bgp});
   }
  }
 })
})
 
Efek kaca pembesar untuk memperbesar gambar menggunakan jQuery dan CSS3 aslinya dikodekan oleh ruby_on_tails untuk TheCodePlayer dan dihaturkan kembali kepada Anda oleh jelasinblog.

Selamat mencoba :)

COMMENTS

BLOGGER: 7
  1. Can i use this on every article?

    ReplyDelete
  2. Keren. Makasih bang. :-d
    langsung ane coba nih :D

    ReplyDelete
  3. perjelas dong itu taruhnya dimana saya tidak mengerti , untuk blog kan

    ReplyDelete
    Replies
    1. - HTML diletakkan dalam postingan blog (mode HTML bukan Compose)
      - CSS letakkan di dalam b:skin
      - JQuery letakkan diatas tag /head

      Semoga cukup jelas. Jika belum tanya kembali.
      Selamat mencoba.

      Delete

Name

Adsense,2,Blockquote,1,Blogger,17,Blogger Tool,1,Blogspot,1,Contact Form,3,CSS,23,CSS3,1,Domain,1,Font,2,Halaman,3,HTML,5,JavaScript,17,jQuery,6,Komentar,2,Page Loading,1,SEO,1,Share Button,1,Sidebar,1,Tag Kondisional,1,Tema,11,Trik,1,Website,1,WordPress,2,
ltr
item
Jelas in Blog: Efek Kaca Pembesar Menggunakan jQuery
Efek Kaca Pembesar Menggunakan jQuery
Magnify glass for image zoom using Jquery and CSS3. Yaitu efek kaca pembesar untuk memperbesar gambar.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP2P-6z2HsvXJhM31uOWaOX0wjuXuOcw8fiD1U0eG3cZpuLrEojUQ6Cn7F0h8uH_lDKUgerfHED_ByGmoW70RtXWkdLibhFDmA3qH2N_qhWYWMU98yUFBZeImWuhUu6CHQ0OIOMmdo5Cfx/s1600/ns-sem-head.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP2P-6z2HsvXJhM31uOWaOX0wjuXuOcw8fiD1U0eG3cZpuLrEojUQ6Cn7F0h8uH_lDKUgerfHED_ByGmoW70RtXWkdLibhFDmA3qH2N_qhWYWMU98yUFBZeImWuhUu6CHQ0OIOMmdo5Cfx/s72-c/ns-sem-head.jpg
Jelas in Blog
https://jelasinblog.blogspot.com/2014/05/efek-kaca-pembesar-menggunakan-jquery.html
https://jelasinblog.blogspot.com/
https://jelasinblog.blogspot.com/
https://jelasinblog.blogspot.com/2014/05/efek-kaca-pembesar-menggunakan-jquery.html
true
2350744749749285503
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content