Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:23:15+00:00 2026-06-11T00:23:15+00:00

Basicly i wanted to create a magnifying glass effect on my image while i

  • 0

Basicly i wanted to create a magnifying glass effect on my image while i move my mouse and hold left-mouse-button. But it acts like im dragging the picture.

I used this magnifying example

It works perfectly when i use mousemove or mousedown action alone, but i wanna add both actions to it.

html:

<div class="magnify">
    <div class="large"></div> 
    <img class="small" title="Halten Sie die linke Maustaste-Taste für einen genaueren Blick!" src="../../Content/pictures/mypicture.png" alt="mypicture.png" />
</div>

css:

/* Some CSS */
.magnify { position:relative; }
/* Lets create the magnifying glass */
.large { width:200px; height:200px; position:absolute; border-radius:100%;
/* Multiple box shadows to achieve the glass effect */
/* rgba(160, 195, 219, 0.85) = #a0c3db,  rgba(73, 151, 205, 0.85) = #4997cd */
/* rgba(234, 243, 250, 0.85) = #eaf3fa,  rgba(57, 136, 191, 0.85) = #3988bf */
box-shadow:0 0 0 9px rgba(73, 151, 205, 0.75), 
           0 0 7px 9px rgba(0, 0, 0, 0.25), 
     inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
/* Lets load up the large image first */
background:url(/Content/pictures/mypicture.png) no-repeat;
/* hide the glass by default */
display:none; cursor:pointer;
}
/* To solve overlap bug at the edges during magnification */
.small { display:block; width:800px; border-radius:20px 20px 20px 20px; }

jquery:

var leftButtonDown = false;
$(document).ready(function ()
{   //define zero
    var native_width = 0;
    var native_height = 0;
    //Now the mousemove function
    $(".magnify").mousemove(function (e)
    {
        $(".magnify").bind('mousedown', function (en)
        {
            if (en.which === 1) { leftButtonDown = false; }
        }).bind('mouseup', function (en)
        {
            if (en.which === 1) { leftButtonDown = true; }
        })

        if (leftButtonDown == true) {
            //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(.magnify)
                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) {
                    $(".large").fadeIn(100);
                } else {
                    $(".large").fadeOut(100);
                }
                if ($(".large").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 / $(".small").width() * native_width - $(".large").width() / 2) * -1;
                    var ry = Math.round(my / $(".small").height() * native_height - $(".large").height() / 2) * -1;
                    var bgp = rx + "px " + ry + "px";
                    //Time to move the magnifying glass with the mouse
                    var px = mx - $(".large").width() / 2;
                    var py = my - $(".large").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
                    $(".large").css({ left: px, top: py, backgroundPosition: bgp });
                }
            }
        } //endif leftButtonDown == true
    })
})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:23:16+00:00Added an answer on June 11, 2026 at 12:23 am

    The problems are as follows:

    1. You have your leftButtonDown = true/false the wrong way round in the mousedown and mouseup events.

    2. It is also worthwhile to convert the mouseup event to bind to the document (in case the we have mousedown inside the image, and mouseup outside the image).

    3. Add en.preventDefault to mousedown to stop it causing Firefox or some browsers from trying to drag the image instead.

    **4. Add fadeOut to mouseup to tweak behaviour (so that it fades even if the mouse doesn’t move immediately after mouseup).

    $(".magnify").bind('mousedown', function (en)
    {
        if (en.which === 1) {
            leftButtonDown = true;
            en.preventDefault();
        }
    })
    $(document).bind('mouseup', function (en)
    {
        if (en.which === 1) {
          leftButtonDown = false;
          $(".large").fadeOut(100);
        }
    })
    

    5. Move the above block of code outside of your mousemove event handler. You only need to run that code once. So leave it just inside $(document).ready where it belongs.

    6. Your if ( leftButtonDown ) { ... } guard needs to be moved. Remove it and move the condition check into your fadeIn and fadeOut condition.

    Notice the && leftButtonDown at the end of the condition.

    if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0
        && leftButtonDown )
    {
      $(".large").fadeIn(100);
    } else {
      $(".large").fadeOut(100);
    }
    

    For more details, see this fiddle.


    I assume you haven’t tried adding the code to magnify the image inside the magnifying glass yet. I will leave that for you to complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basicly i'm trying to create image cropping script, and it's almost done, here is
basicly, I'm trying to show the result of a listview (created with QSLite)but in
Basicly I'm making an image gallery that downloads from the web. So - when
What I basicly need is a big image that is clickable and links to
basicly i need my page to respond to left and right arrow keys. so
I want to create an app which basicly, should launch itself at some particular
Here is my code: http://jsfiddle.net/sZKeM/1/ So basicly it shows box when I hover button
I wanted to do some research but i could not find any information about
Basicly theres 2 divs open by default, the one of the left is always
Basicly, I want to write a C++ program and with it, pass messages to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.