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

  • SEARCH
  • Home
  • 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 8588405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:47:42+00:00 2026-06-11T22:47:42+00:00

I am creating an image hover effect but I am having problem with it.

  • 0

I am creating an image hover effect but I am having problem with it. When I hover over certain images, the scrollbars appear which I want to avoid but don’t know how to do so. I believe it has to do with viewport and calculations but am not sure how that is done.

Example Here

JSBin Code

Here is the code:

$('.simplehover').each(function(){
    var $this = $(this);        
    var isrc = $this[0].src, dv = null;
                        
    $this.mouseenter(function(e){
        dv = $('<div />')
            .attr('class', '__shidivbox__')
            .css({
                display: 'none',
                zIndex : 9999,
                position: 'absolute',
                top: e.pageY + 20,
                left: e.pageX + 20
            })
            .html('<img alt="" src="' + isrc + '" />')
            .appendTo(document.body);           
        dv.fadeIn('fast');
    })
    .mouseleave(function(){
        dv.fadeOut('fast');
    });         

});

Can anyone please help me how do I make it so that hovered image appears at such place that scrollbars dont appear? (Of course we can’t avoid scrollbar if image size is very very big)

I just want to show original image on zoom while avoiding scrollbars as much as possible.

Please note that I am planning to convert it into jQuery plugin and therefore I can’t force users of plugin to have overflow set to hidden. The solution has do with viewport, left, top, scroll width and height, window width/height properties that I can incorporate into plugin later on.


Update:

I have come up with this:

http://jsbin.com/upuref/14

However, it is very very hacky and not 100% perfect. I am looking for a better algorithim/solution. I have seen many hover plugins that do this very nicely but since I am not that good at this, I can’t do it perfectly well. For example Hover Zoom Chrome Plugin does great job of showing hovered images at appropriate place based on their size.

  • 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-11T22:47:43+00:00Added an answer on June 11, 2026 at 10:47 pm

    Well, this looks fun. Anyway, here’s my answer. I’ve been watching this for a few days and though I’d chip in too. The following will make sure that the hovering images do not go out of the viewport and in the event that the width of the image is bigger than the available space for display, the display of the image will be resized (You can comment out the code that does this if you don’t want it. Just look for the word “resize” in the code).

    var $document = $(document);
    $('.simplehover').each(function(){
        var $this = $(this);
        // make sure that element is really an image
        if (! $this.is('img')) return false;
    
        var isrc = $this[0].src, ibox = null;
    
        if (! isrc) return false;
        ibox = $('<img />')
                .attr('class', 'simpleimagehover__shidivbox__')
                .css({
                    display: 'none',
                    zIndex : 99,
                    MozBoxShadow: '0 0 1em #000', 
                    WebkitBoxShadow: '0 0 1em #000',
                    boxShadow: '0 0 1em #000',
                    position: 'absolute',
                    MozBorderRadius : '10px',
                    WebkitBorderRadius : '10px',
                    borderRadius : '10px'
                })
                .attr('src', isrc)
                .appendTo(document.body);          
    
        $this.bind('mouseenter mousemove', function(e) {
            $('.simpleimagehover__shidivbox__').hide();
    
            var left = e.pageX + 5, 
                top = e.pageY + 5,
                ww = window.innerWidth,
                wh = window.innerHeight,
                w = ibox.width(),
                h = ibox.height(),
                overflowedW = 0,
                overflowedH = 0;
    
            // calucation to show element avoiding scrollbars as much as possible - not a great method though
            if ((left + w + $document.scrollLeft()) > ww)
            {
                overflowedW = ww - (left + w + $document.scrollLeft());
                if (overflowedW < 0)
                {
                   left -= Math.abs(overflowedW);
                }
            }
    
            // 25 is just a constant I picked arbitrarily to compensate pre-existing scrollbar if the page itself is too long
            left -= 25;
            left = left < $document.scrollLeft() ? $document.scrollLeft() : left;
    
            // if it's still overflowing because of the size, resize it
            if (left + w > ww)
            {
                overflowedW = left + w - ww;
                ibox.width(w - overflowedW - 25);
            }
    
    
            if (top + h > wh + $document.scrollTop())
            {
                overflowedH = top + h - wh - $document.scrollTop();
                if (overflowedH > 0)
                {
                    top -= overflowedH;
                }
            }
    
            top = top < $document.scrollTop() ? $document.scrollTop() : top;
            ibox.css({
                top: top,
                left: left
            });
    
            ibox.show();
        }); 
    
    
        $('.simpleimagehover__shidivbox__').mouseleave(function(){
            $('.simpleimagehover__shidivbox__').hide();
        });
    
        $document.click(function(e){
            $('.simpleimagehover__shidivbox__').hide();
        });
    
        $document.mousemove(function(e){
            if (e.target.nodeName.toLowerCase() === 'img') {
                return false;
            }
    
            $('.simpleimagehover__shidivbox__').hide();
        });
    });
    

    While my solution itself is not perfect, you might find something useful in there that can help you determine the viewport. Also, you might want to think about the performance of the code. Since this is a plugin that you’re building, you’ll want to make some optimizations before releasing it to public. Basically, just make sure it’s not slow.

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

Sidebar

Related Questions

I am creating a light box. When you hover over an image, the accompanying
I'm creating a drag and drop image environment, where if you hover over an
I am creating a site where you hover the mouse over an image and
the tutorial here http://www.edumobile.org/android/android-beginner-tutorials/creating-image-gallery/ teaches how to put images from resources in gallery here's
I have been creating an image gallery with jQuery, all is done. The images
I'm currently having trouble creating an image from a binary string of data in
You can check this website I am creating: http://www.tinybigstudio.com/proyectos/golfycia/ If you hover over PRECIO
I am creating a quick view effect for my website. When someone hovers over
I am using this image effect for my icon links http://bavotasan.com/2009/creating-a-jquery-mouseover-fade-effect/ What I plan
are there any tutorials for creating image effects in iphone? like glow,paper effect etc

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.