Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Doushio image hover
  3. // @namespace    http://your.homepage/
  4. // @version      0.1
  5. // @description  enter something useful
  6. // @author       You
  7. // @match        http://*.doushio.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function(){
  12.     $('head').append('<style>#hover_overlay{width:100%;height:100%;top:0;right:0;position:fixed;pointer-events:none;display:flex;z-index:300}#hover_overlay_image{max-height:100%;max-width:100%;margin:auto;display:none}</style>');
  13.     $('body').append('<div id="hover_overlay"></div>');
  14.    
  15.     var mouseoverTarget;
  16.    
  17.     function imageHoverPreview(e){
  18.         // Check if hovering over image or image is expanded by clicking
  19.         var $target = $(mouseoverTarget);
  20.         if (!$target.is('img') || $target.data('thumbSrc'))
  21.             return fadeOutHoverOverlay();
  22.         var src = $target.closest('a').attr('href');
  23.         var isWebm = /\.webm/i.test(src);
  24.         var $img  = $(isWebm ? '<video />' : '<img />', {
  25.             id: 'hover_overlay_image',
  26.             'src': src,
  27.             autoplay: true,
  28.             loop: true,
  29.         });
  30.         // Gracefully fade out previous image
  31.         var $old = $('#hover_overlay_image');
  32.         if ($old.length){
  33.             fadeOutHoverOverlay(function(){
  34.                 fadeInHoverOverlay($img);
  35.             });
  36.         }
  37.         else
  38.             fadeInHoverOverlay($img);
  39.     }
  40.    
  41.     function fadeInHoverOverlay($img){
  42.         $('#hover_overlay').append($img);
  43.         $('#hover_overlay_image').fadeIn({duration: 200});
  44.     }
  45.    
  46.     function fadeOutHoverOverlay(cb){
  47.         // Do nothing, if image is already removed
  48.         var $img = $('#hover_overlay_image');
  49.         if (!$img.length)
  50.             return;
  51.         $img.fadeOut({duration: 200, complete: function(){
  52.             $img.remove();
  53.             // More responsive transition with fast pointer movements
  54.             imageHoverPreview();
  55.         }});
  56.         if (typeof cb === 'function')
  57.             cb();
  58.     }
  59.    
  60.     $DOC.on('mouseover', function(e){
  61.         mouseoverTarget = e.target;
  62.     });
  63.     $DOC.on('mouseover', imageHoverPreview).on('click', 'img, video', fadeOutHoverOverlay);
  64. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement