Advertisement
Guest User

Spun manipulate circle text

a guest
Nov 25th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery( document ).ready( function() {
  2.  
  3.  $target = '.no-thumbnail';
  4.  
  5.  manipulateCircleText( $target );
  6.  
  7. });
  8.  
  9. /*
  10.  * This function sets up the HTML of the circles ready for CSS to
  11.  * manipulate the text so that the first letter shows by default
  12.  * and only on-hover the rest of the text shows
  13. */
  14. function manipulateCircleText( $target ) {
  15.  
  16.     $ = jQuery;
  17.  
  18.     // For each circle
  19.     $( 'article.post' ).each(function() {
  20.  
  21.      // Start off by wrapping a <span> around all other characters other than the first one
  22.      // So that you can style those other characters (hide them off screen) with CSS
  23.      wrapSpan( this, $target  );
  24.  
  25.      // On-hover of the circle
  26.      $( this ).find( 'a' ).hover(
  27.      
  28.       // Hover-in
  29.       function() {
  30.        // Delete the span tag we've wrapped to the circle title
  31.        deleteSpan( this, $target );
  32.       },
  33.       // Hover-out
  34.       function() {
  35.        // Return the <span> back to all the characters but the first
  36.        wrapSpan( this, $target  );
  37.       }
  38.  
  39.      );
  40.  
  41.      // Do the same on-focus (for keyboard users) of the circle
  42.      $( this ).find( 'a' ).focus(
  43.       // Focus-in
  44.       function() {
  45.        // Delete the span tag we've wrapped to the circle title
  46.        deleteSpan( this, $target );
  47.       }  
  48.      )
  49.      // Focus-out
  50.      .blur( function() {
  51.       // Return the <span> back to all the characters but the first
  52.       wrapSpan( this, $target  );
  53.      });
  54.  
  55.     });
  56.  
  57.      /*
  58.      * Wrap all but the first character with a span
  59.     */
  60.     function wrapSpan( $this ) {
  61.      $title           = $( $this ).find( $target );
  62.      $countTitleChars = $title.text().length;
  63.      $firstChar       = $( $title ).text().substr( 0, 1 );
  64.      $otherChars      = $( $title ).text().substr( 1, $countTitleChars );
  65.      
  66.      $( $this ).find( $target ).empty().html( '<span>' + $firstChar + '</span>' + '<span class="otherChars">' + $otherChars + '</span>' );
  67.     }
  68.      
  69.     /*
  70.      * Delete the span we've wrapped
  71.     */
  72.     function deleteSpan( $this, $target ) {
  73.      // Get the words of the title
  74.      $titleWords = $( $this ).find( $target ).text();
  75.      
  76.      // Get rid of the <span> we just added by erasing the title
  77.      $( $this ).children( 'span' ).empty()
  78.        // Then add the original title back in, clean of our modifications.
  79.        .text( $titleWords );
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement