I am trying to create an image hover effect, but I am encountering an issue. When I hover over certain images, unwanted scrollbars appear, and I would like to find a way to prevent this from happening. It seems to be related to the viewport and calculations, but I am unsure of the solution.
Here is the code snippet I am working with:
$('.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 someone please assist me in positioning the hovered image in a way that prevents scrollbars from appearing? (Unless the image size is exceptionally large)
I want to display the original image on zoom without causing scrollbars to show as much as possible.
Please note: I intend to convert this into a jQuery plugin, so I cannot require users of the plugin to set overflow
to hidden
. The solution should involve properties like viewport, left, top, scroll width, height, window width/height for future incorporation.
Update:
I have made some progress with this solution:
However, it still feels like a hacky fix and not entirely perfect. I am seeking a better algorithm or solution. There are hover plugins that handle this elegantly, but due to my limited expertise, I am struggling. For example, the Hover Zoom Chrome Plugin does an excellent job of positioning hovered images based on their size.