You So Crazy - a silly, paranoia-inducing jQuery plugin

Posted by Tom on 2011-05-02 18:56

Have you ever wanted a jQuery plugin that causes your users to question their sanity? Then look no further!

You So Crazy inserts phrases into the paragraphs of an HTML document, which then disappear when they are scrolled out of view, only to reappear in a different place.

Demo

And here, as always, is the source:

(function($){
    $.you_so_crazy = function(customPhrases, options) {
    
        var phrases = [
            'feed me a stray cat'
        ];
        var settings = {
            'highlight'     : false
        };
        
        if (options)
            $.extend(settings, options);
        if (customPhrases)
            phrases = phrases.concat(customPhrases);
        
        var currentElement = null;
        var spotted = false;
  
        $(window).scroll(function() {
            if (currentElement)
            {
                if (element_on_screen(currentElement))
                {
                    if (spotted)
                    {
                        select_element();
                        spotted = false;
                    }
                }
                else
                {
                    if (!spotted)
                        spotted = true;
                }
            }
        });
        
        var select_element = function() {
        
            if (currentElement)
                currentElement.remove();
 
            var target = null;
            while(target == null)
            {
                var paragraphs = $('p');
                var index = Math.floor(Math.random() * paragraphs.length);
                target = $(paragraphs[index]);
                
                if (element_on_screen(target))
                    break;
                    
                target = null;
            }
            
            var tokens = target.html().split(' ');
            var strIndex = Math.floor(Math.random() * tokens.length);
            var phraseIndex = Math.floor(Math.random() * phrases.length);
            var finalPhrase = '<span class="psycho42"' 
                + (settings.highlight ? ' style="background-color: #f00;">' : '>') 
                + phrases[phraseIndex] + '</span>';
            tokens.splice(strIndex, 0, finalPhrase);
            target.html(tokens.join(' '));
            currentElement = target.find('span.psycho42');
        };
        
        var element_on_screen = function(element)
        {
            var screen_position = element.offset();
            screen_position.top -= $(document).scrollTop();
            screen_position.left -= $(document).scrollLeft();
            var windowSize = { 'left': $(window).width(), 'top': $(window).height() }
            
            return screen_position.top + element.height() < 0 
                || screen_position.left + element.width() < 0
                || screen_position.top > windowSize.top 
                || screen_position.left > windowSize.left;
        }
        
        select_element();
    };
})(jQuery);

This is a strong contender for the title of most pointless code I've ever written, but I had a spare hour and felt it was about time I added my own contribution to the swath of jQuery plugins out there.