Advertisement
Nixsy

popup image windows

Nov 18th, 2023
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Image Hover Pop-out
  3. // @namespace    nixsy.comm
  4. // @version      1.0
  5. // @description  Show larger image in a pop-out when hovering over an image
  6. // @author       Nixsy
  7. // @match        http://*/*
  8. // @match        https://*/*
  9. // @require      http://code.jquery.com/jquery-latest.min.js
  10. // @grant        GM_addStyle
  11. // ==/UserScript==
  12.  
  13. // Add styles for the pop-out window
  14. GM_addStyle(`
  15.     .hoverPopup {
  16.         position: absolute;
  17.         max-width: 500px;
  18.         max-height: 700px;
  19.         padding: 5px;
  20.         background-color: #fff;
  21.         border: 1px solid #ccc;
  22.         box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  23.         z-index: 9999;
  24.         display: none;
  25.     }
  26. `);
  27.  
  28. // Create pop-out div
  29. const popupDiv = $('<div class="hoverPopup"></div>').appendTo('body');
  30.  
  31. // Function to show image in pop-out
  32. function showPopup(imgSrc, mouseX, mouseY) {
  33.     popupDiv.html(`<img src="${imgSrc}" style="max-width:100%; max-height:100%;" />`);
  34.     popupDiv.css({ top: mouseY + 10, left: mouseX + 10 }).fadeIn('fast');
  35. }
  36.  
  37. // Function to hide pop-out
  38. function hidePopup() {
  39.     popupDiv.hide();
  40. }
  41.  
  42. // Attach hover event to all images
  43. $('img').hover(function(e) {
  44.     const imgSrc = $(this).attr('src');
  45.     const mouseX = e.pageX;
  46.     const mouseY = e.pageY;
  47.     showPopup(imgSrc, mouseX, mouseY);
  48. }, function() {
  49.     hidePopup();
  50. });
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement