Guest User

Untitled

a guest
Jan 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. $('.simplehover').each(function(){
  2. var $this = $(this);
  3. var isrc = $this[0].src, dv = null;
  4.  
  5. $this.mouseenter(function(e){
  6. dv = $('<div />')
  7. .attr('class', '__shidivbox__')
  8. .css({
  9. display: 'none',
  10. zIndex : 9999,
  11. position: 'absolute',
  12. top: e.pageY + 20,
  13. left: e.pageX + 20
  14. })
  15. .html('<img alt="" src="' + isrc + '" />')
  16. .appendTo(document.body);
  17. dv.fadeIn('fast');
  18. })
  19. .mouseleave(function(){
  20. dv.fadeOut('fast');
  21. });
  22.  
  23. });
  24.  
  25. var $document = $(document);
  26. $('.simplehover').each(function(){
  27. var $this = $(this);
  28. // make sure that element is really an image
  29. if (! $this.is('img')) return false;
  30.  
  31. var isrc = $this[0].src, ibox = null;
  32.  
  33. if (! isrc) return false;
  34. ibox = $('<img />')
  35. .attr('class', 'simpleimagehover__shidivbox__')
  36. .css({
  37. display: 'none',
  38. zIndex : 99,
  39. MozBoxShadow: '0 0 1em #000',
  40. WebkitBoxShadow: '0 0 1em #000',
  41. boxShadow: '0 0 1em #000',
  42. position: 'absolute',
  43. MozBorderRadius : '10px',
  44. WebkitBorderRadius : '10px',
  45. borderRadius : '10px'
  46. })
  47. .attr('src', isrc)
  48. .appendTo(document.body);
  49.  
  50. $this.bind('mouseenter mousemove', function(e) {
  51. $('.simpleimagehover__shidivbox__').hide();
  52.  
  53. var left = e.pageX + 5,
  54. top = e.pageY + 5,
  55. ww = window.innerWidth,
  56. wh = window.innerHeight,
  57. w = ibox.width(),
  58. h = ibox.height(),
  59. overflowedW = 0,
  60. overflowedH = 0;
  61.  
  62. // calucation to show element avoiding scrollbars as much as possible - not a great method though
  63. if ((left + w + $document.scrollLeft()) > ww)
  64. {
  65. overflowedW = ww - (left + w + $document.scrollLeft());
  66. if (overflowedW < 0)
  67. {
  68. left -= Math.abs(overflowedW);
  69. }
  70. }
  71.  
  72. // 25 is just a constant I picked arbitrarily to compensate pre-existing scrollbar if the page itself is too long
  73. left -= 25;
  74. left = left < $document.scrollLeft() ? $document.scrollLeft() : left;
  75.  
  76. // if it's still overflowing because of the size, resize it
  77. if (left + w > ww)
  78. {
  79. overflowedW = left + w - ww;
  80. ibox.width(w - overflowedW - 25);
  81. }
  82.  
  83.  
  84. if (top + h > wh + $document.scrollTop())
  85. {
  86. overflowedH = top + h - wh - $document.scrollTop();
  87. if (overflowedH > 0)
  88. {
  89. top -= overflowedH;
  90. }
  91. }
  92.  
  93. top = top < $document.scrollTop() ? $document.scrollTop() : top;
  94. ibox.css({
  95. top: top,
  96. left: left
  97. });
  98.  
  99. ibox.show();
  100. });
  101.  
  102.  
  103. $('.simpleimagehover__shidivbox__').mouseleave(function(){
  104. $('.simpleimagehover__shidivbox__').hide();
  105. });
  106.  
  107. $document.click(function(e){
  108. $('.simpleimagehover__shidivbox__').hide();
  109. });
  110.  
  111. $document.mousemove(function(e){
  112. if (e.target.nodeName.toLowerCase() === 'img') {
  113. return false;
  114. }
  115.  
  116. $('.simpleimagehover__shidivbox__').hide();
  117. });
  118. });
  119.  
  120. html{overflow-x:hidden;}
  121. html{overflow-y:hidden;}
  122.  
  123. $this.mouseenter(function () {
  124.  
  125. dv = $('<div />')
  126. .attr('class', '__shidivbox__')
  127. .css({
  128. 'display': 'none',
  129. 'z-index': 9999,
  130. 'position': 'absolute',
  131. 'box-shadow': '0 0 1em #000',
  132. 'border-radius': '5px'
  133. })
  134. .html('<img alt="" src="' + isrc + '" />')
  135. .appendTo(document.body);
  136.  
  137. var DocuWidth = window.innerWidth;
  138. var DocuHeight = window.innerHeight;
  139.  
  140. var DvImg = dv.find('img');
  141.  
  142. var TheImage = new Image();
  143. TheImage.src = DvImg.attr("src");
  144.  
  145. var DivWidth = TheImage.width;
  146. var DivHeight = TheImage.height;
  147.  
  148. if (DivWidth > DocuWidth) {
  149.  
  150. var WidthFactor = (DivWidth / DocuWidth) + 0.05;
  151. DivHeight = parseInt((DivHeight / WidthFactor), 10);
  152. DivWidth = parseInt((DivWidth / WidthFactor), 10);
  153. }
  154.  
  155. var ThumbHeight = $this.height();
  156. var ThumbWidth = $this.width();
  157. var ThumbTop = $this.position().top;
  158. var ThumbLeft = $this.position().left;
  159.  
  160. var SpaceAboveThumb = ThumbTop - $(document).scrollTop();
  161. var SpaceBelowThumb = DocuHeight - ThumbTop - ThumbHeight + $(document).scrollTop();
  162.  
  163. var MaxHeight = Math.max(SpaceAboveThumb, SpaceBelowThumb);
  164.  
  165. if (DivHeight > MaxHeight) {
  166.  
  167. var HeightFactor = (DivHeight / MaxHeight) + 0.05;
  168. DivHeight = parseInt((DivHeight / HeightFactor), 10);
  169. DivWidth = parseInt((DivWidth / HeightFactor), 10);
  170. }
  171.  
  172. var HoverImgLeft = 0;
  173. var HoverImgTop = 0;
  174.  
  175. if (SpaceBelowThumb > SpaceAboveThumb) {
  176. HoverImgTop = ThumbTop + ThumbHeight;
  177. } else {
  178. HoverImgTop = ThumbTop - DivHeight;
  179. }
  180.  
  181. HoverImgTop = (HoverImgTop < 0) ? 0 : HoverImgTop;
  182.  
  183. HoverImgLeft = (DocuWidth - DivWidth) / 2;
  184.  
  185. dv.find('img').css({
  186. 'width': DivWidth,
  187. 'height': DivHeight,
  188. 'border-radius': '5px'
  189. });
  190.  
  191. dv.css({
  192. 'left': HoverImgLeft,
  193. 'top': HoverImgTop
  194. });
  195.  
  196. dv.fadeIn('fast');
  197. });
  198.  
  199. $('.simplehover').each(function(){
  200. var $this = $(this);
  201.  
  202. // make sure that element is really an image
  203. if (! $this.is('img')) return false;
  204.  
  205. var isrc = $this[0].src, dv = null;
  206.  
  207. if (! isrc) return false;
  208.  
  209. $this.mouseenter(function(e){
  210. // mouse x position
  211. var initXPos = e.pageX;
  212. var initYPos = e.pageY+20-$(window).scrollTop();
  213. var windowWidth = $(window).width();
  214. var windowHeight = $(window).height();
  215. // load original image
  216. var $img = $('<img/>');
  217. $img.on('load',function(eload) {
  218. var widthImage = this.width;
  219. var heightImage = this.height;
  220. // set inline style for get sizes after (see problems webkit and cache)
  221. $(this).css('width',widthImage);
  222. $(this).css('height',heightImage);
  223. var ratio = widthImage/heightImage;
  224.  
  225. var finalXPos = initXPos+widthImage>windowWidth? windowWidth-widthImage-5 : initXPos;
  226. var finalYPos = initYPos;
  227.  
  228. var img = this;
  229.  
  230.  
  231. // resize image if is bigger than window
  232. if(finalXPos<0) {
  233. finalXPos = 0;
  234. $img.css('width', windowWidth-10);
  235. $img.css('height',(windowWidth-10)/ratio);
  236. }
  237.  
  238. // If overflow Y
  239.  
  240. if(finalYPos+getSize($img,'height')>windowHeight) {
  241.  
  242. // calculate where is more space (top or bottom?)
  243. var showOnTop = (windowHeight-initYPos-10)<windowHeight/2;
  244. if(showOnTop) {
  245. if(initYPos<getSize($img,'height')) {
  246. $img.height(initYPos-30);
  247. $img.width(getSize($img,'height')*ratio);
  248. }
  249. finalYPos = 0;
  250. finalXPos = initXPos+getSize($img,'width')>windowWidth? windowWidth-getSize($img,'width')-5 : initXPos;
  251.  
  252. }else {
  253. // show on bottom
  254. if(windowHeight-initYPos<getSize($img,'height')) {
  255. $img.height(windowHeight-initYPos-10);
  256. $img.width(getSize($img,'height')*ratio);
  257.  
  258. }
  259. finalXPos = initXPos+getSize($img,'width')>windowWidth? windowWidth-getSize($img,'width')-5 : initXPos;
  260. }
  261. }
  262. dv = $('<div />')
  263. .attr('class', '__shidivbox__')
  264. .css({
  265. display: 'none',
  266. zIndex : 9999,
  267. position: 'absolute',
  268. MozBorderRadius : '5px',
  269. WebkitBorderRadius : '5px',
  270. borderRadius : '5px',
  271. top: finalYPos+$(window).scrollTop(),
  272. left: finalXPos
  273. }).append($img)
  274. .appendTo(document.body);
  275. dv.fadeIn('fast');
  276. });
  277. // load the original image (now is the same, but I think is better optimize it)
  278. $img.attr("src",$this.attr("src"));
  279.  
  280. function getSize($el,widthOrHeight) {
  281. // horrible but working trick :)
  282. return +$el.css(widthOrHeight).replace("px","");
  283. }
  284. })
  285. .mouseleave(function(){
  286. dv.fadeOut('fast');
  287. });
  288.  
  289. });
  290.  
  291. $this.mouseover(function(e) {
  292. var top = e.pageY + 20,
  293. left = e.pageX + 20,
  294. img = $('<img />').attr('src', isrc).load(function(event) {
  295. var width = this.width,
  296. height = this.height;
  297. // houston we have a problem
  298. if (left + width > document.width) {
  299. // see if we can use the left side of the screen
  300. left = document.width - width - 10;
  301. }
  302.  
  303. // not much we can do about top if resizing is not an option
  304. if (top + height > document.height) {
  305. var newHeight = document.height - top - 10;
  306. $(this).width(width * (newHeight/height));
  307. }
  308.  
  309. dv = $('<div />').attr('class', '__shidivbox__').css({
  310. display: 'none',
  311. zIndex: 9999,
  312. position: 'absolute',
  313. MozBorderRadius: '5px',
  314. WebkitBorderRadius: '5px',
  315. borderRadius: '5px',
  316. top: top,
  317. left: left
  318. }).append(img).appendTo(document.body);
  319. dv.fadeIn('fast');
  320. });
  321. }).mouseout(function() {
  322. dv.fadeOut('fast');
  323. });
Add Comment
Please, Sign In to add comment