jQuery custom magnifying glass effect

jQuery custom magnifying glass effect

This article example shares the specific code of jQuery's custom magnifying glass effect for your reference. The specific content is as follows

jQuery plugin definition:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script src="js/jquery3.6.0.js"></script>
  <style type="text/css">
   div{
    width: 200px;
    height: 200px;
   }
  </style>
 </head>
 <body>
  <div></div>
  <div></div>
  <script>
   // 1. jQuery plugin, defined on the basis of jQuery.fn // 2. Naming conflict resolution // 3. Loop through each element in the jQuery object // 4. In the function, return jQuery (this)
   
   (function($){
    $.fn.extend({
     randomColor:function(){
      // this refers to the pseudo array consisting of all the elements we selected through the $selector function random(){
       var r = Math.floor(Math.random()*256);
       var g = Math.floor(Math.random()*256);
       var b = Math.floor(Math.random()*256);
       
       return 'rgb('+ r +','+ g +','+ b +')';
      }
      this.each(function(index){
       $(this).css({
        backgroundColor:random()
       })
      })
      return this;
     }
    })
   })(jQuery);
   $('div').randomColor();
  </script>
 </body>
</html> 

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  
  <style type="text/css">
   *{
    margin: 0;
    padding: 0;
   }
   .magnifier .left{
    width: 240px;
    height: 150px;
    float: left;
    position: relative;
   }
   .magnifier .left img{
    width: 240px;
    height: 150px;
   }
   .magnifier .left .mask{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background-color: black;
    opacity: 0.4;
   }
   .magnifier .float{
    width: 50px;
    height: 50px;
    background: hotpink;
    opacity: 0.5;
    position: absolute;
    left: 0;
    top: 0;
   }
   .magnifier .right{
    height: 200px;
    width: 200px;
    background-image: url(img/2.jpg) ;
    float: left;
    margin-left: 50px;
   }
  </style>
 </head>
 <body>
  
  <div class="magnifier">
   <div class="left">
    <img src="./img/2.jpg" >
    <div class="float">
     
    </div>
    <div class="mask"></div>
   </div>
   <div class="right"></div>
  </div>
  <script src="js/jquery3.6.0.js"></script>
  <script>
   (function($){
    $.fn.extend({
     magnifier:function(){
      this.each(function(){
       var that=this;
       $('.left',this).mousemove(function (evt){
        var x=evt.offsetX;
        var y=evt.offsetY;
        var float=$('.float',this);
        
        x=x-float.width/2;
        y=y-float.height/2;
        
        if(x<0){
         x=0;
        }
        if(y<0){
         y=0;
        }
        if(x > $(this).width()-float.width()){
         x = $(this).width()-float.width();
        }
        if(y > $(this).height()-float.height()){
         y = $(this).height()-float.height();
        }
        float.css({
         left:x,
         top:y
        });
        $('.right',that).css({
         backgroundPosition:x*-4+'px' + y*-4+'px'
        })
       }).mouseover(function(){
        
       }).mouseout(function(){
        
       })
      });
     }
    })
   })(jQuery)
   $('.magnifier').magnifier();
  </script>
 </body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Use JQuery to imitate Taobao's image magnifying glass display effect
  • Image magnifying glass effect based on Jquery plug-in development (imitating Taobao)
  • Example code of jquery image magnifier function
  • About the use of Jqzoom jquery magnifying glass effect plug-in
  • Magnifying glass effect based on jquery
  • Ideas and codes for implementing image magnifying glass effect with Jquery (self-written)
  • Product display magnifying glass based on jQuery
  • jQuery implements magnifying glass effect example code
  • jQuery magnifying glass effect is very beautiful
  • Using jQuery to implement magnifying glass effect

<<:  Resolving MySQL implicit conversion issues

>>:  Example code for using Nginx to implement 301 redirect to https root domain name

Recommend

How to install and uninstall open-vswitch in Linux

1. Compile and install ovs from source code: Inst...

Detailed usage of Linux text search command find

The find command is mainly used to find directori...

Vue+node realizes audio recording and playback function

Result: The main part is to implement the code lo...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

How to mark the source and origin of CSS3 citations

I am almost going moldy staying at home due to th...

A brief discussion on order reconstruction: MySQL sharding

Table of contents 1. Objectives 2. Environmental ...

MySQL optimization connection optimization

In the article MySQL Optimization: Cache Optimiza...

CSS form validation function implementation code

Rendering principle In the form element, there is...

JavaScript canvas to achieve scratch lottery example

This article shares the specific code of JavaScri...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

Mysql 5.7.19 free installation version encountered pitfalls (collection)

1. Download the 64-bit zip file from the official...

How to enable or disable SSH for a specific user or user group in Linux

Due to your company standards, you may only allow...

How to configure Nginx to support ipv6 under Linux system

1. Check whether the existing nginx supports ipv6...