Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('.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');
- });
- });
- 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();
- });
- });
- html{overflow-x:hidden;}
- html{overflow-y:hidden;}
- $this.mouseenter(function () {
- dv = $('<div />')
- .attr('class', '__shidivbox__')
- .css({
- 'display': 'none',
- 'z-index': 9999,
- 'position': 'absolute',
- 'box-shadow': '0 0 1em #000',
- 'border-radius': '5px'
- })
- .html('<img alt="" src="' + isrc + '" />')
- .appendTo(document.body);
- var DocuWidth = window.innerWidth;
- var DocuHeight = window.innerHeight;
- var DvImg = dv.find('img');
- var TheImage = new Image();
- TheImage.src = DvImg.attr("src");
- var DivWidth = TheImage.width;
- var DivHeight = TheImage.height;
- if (DivWidth > DocuWidth) {
- var WidthFactor = (DivWidth / DocuWidth) + 0.05;
- DivHeight = parseInt((DivHeight / WidthFactor), 10);
- DivWidth = parseInt((DivWidth / WidthFactor), 10);
- }
- var ThumbHeight = $this.height();
- var ThumbWidth = $this.width();
- var ThumbTop = $this.position().top;
- var ThumbLeft = $this.position().left;
- var SpaceAboveThumb = ThumbTop - $(document).scrollTop();
- var SpaceBelowThumb = DocuHeight - ThumbTop - ThumbHeight + $(document).scrollTop();
- var MaxHeight = Math.max(SpaceAboveThumb, SpaceBelowThumb);
- if (DivHeight > MaxHeight) {
- var HeightFactor = (DivHeight / MaxHeight) + 0.05;
- DivHeight = parseInt((DivHeight / HeightFactor), 10);
- DivWidth = parseInt((DivWidth / HeightFactor), 10);
- }
- var HoverImgLeft = 0;
- var HoverImgTop = 0;
- if (SpaceBelowThumb > SpaceAboveThumb) {
- HoverImgTop = ThumbTop + ThumbHeight;
- } else {
- HoverImgTop = ThumbTop - DivHeight;
- }
- HoverImgTop = (HoverImgTop < 0) ? 0 : HoverImgTop;
- HoverImgLeft = (DocuWidth - DivWidth) / 2;
- dv.find('img').css({
- 'width': DivWidth,
- 'height': DivHeight,
- 'border-radius': '5px'
- });
- dv.css({
- 'left': HoverImgLeft,
- 'top': HoverImgTop
- });
- dv.fadeIn('fast');
- });
- $('.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, dv = null;
- if (! isrc) return false;
- $this.mouseenter(function(e){
- // mouse x position
- var initXPos = e.pageX;
- var initYPos = e.pageY+20-$(window).scrollTop();
- var windowWidth = $(window).width();
- var windowHeight = $(window).height();
- // load original image
- var $img = $('<img/>');
- $img.on('load',function(eload) {
- var widthImage = this.width;
- var heightImage = this.height;
- // set inline style for get sizes after (see problems webkit and cache)
- $(this).css('width',widthImage);
- $(this).css('height',heightImage);
- var ratio = widthImage/heightImage;
- var finalXPos = initXPos+widthImage>windowWidth? windowWidth-widthImage-5 : initXPos;
- var finalYPos = initYPos;
- var img = this;
- // resize image if is bigger than window
- if(finalXPos<0) {
- finalXPos = 0;
- $img.css('width', windowWidth-10);
- $img.css('height',(windowWidth-10)/ratio);
- }
- // If overflow Y
- if(finalYPos+getSize($img,'height')>windowHeight) {
- // calculate where is more space (top or bottom?)
- var showOnTop = (windowHeight-initYPos-10)<windowHeight/2;
- if(showOnTop) {
- if(initYPos<getSize($img,'height')) {
- $img.height(initYPos-30);
- $img.width(getSize($img,'height')*ratio);
- }
- finalYPos = 0;
- finalXPos = initXPos+getSize($img,'width')>windowWidth? windowWidth-getSize($img,'width')-5 : initXPos;
- }else {
- // show on bottom
- if(windowHeight-initYPos<getSize($img,'height')) {
- $img.height(windowHeight-initYPos-10);
- $img.width(getSize($img,'height')*ratio);
- }
- finalXPos = initXPos+getSize($img,'width')>windowWidth? windowWidth-getSize($img,'width')-5 : initXPos;
- }
- }
- dv = $('<div />')
- .attr('class', '__shidivbox__')
- .css({
- display: 'none',
- zIndex : 9999,
- position: 'absolute',
- MozBorderRadius : '5px',
- WebkitBorderRadius : '5px',
- borderRadius : '5px',
- top: finalYPos+$(window).scrollTop(),
- left: finalXPos
- }).append($img)
- .appendTo(document.body);
- dv.fadeIn('fast');
- });
- // load the original image (now is the same, but I think is better optimize it)
- $img.attr("src",$this.attr("src"));
- function getSize($el,widthOrHeight) {
- // horrible but working trick :)
- return +$el.css(widthOrHeight).replace("px","");
- }
- })
- .mouseleave(function(){
- dv.fadeOut('fast');
- });
- });
- $this.mouseover(function(e) {
- var top = e.pageY + 20,
- left = e.pageX + 20,
- img = $('<img />').attr('src', isrc).load(function(event) {
- var width = this.width,
- height = this.height;
- // houston we have a problem
- if (left + width > document.width) {
- // see if we can use the left side of the screen
- left = document.width - width - 10;
- }
- // not much we can do about top if resizing is not an option
- if (top + height > document.height) {
- var newHeight = document.height - top - 10;
- $(this).width(width * (newHeight/height));
- }
- dv = $('<div />').attr('class', '__shidivbox__').css({
- display: 'none',
- zIndex: 9999,
- position: 'absolute',
- MozBorderRadius: '5px',
- WebkitBorderRadius: '5px',
- borderRadius: '5px',
- top: top,
- left: left
- }).append(img).appendTo(document.body);
- dv.fadeIn('fast');
- });
- }).mouseout(function() {
- dv.fadeOut('fast');
- });
Add Comment
Please, Sign In to add comment