Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body class="random_background">
  <div class="container-fluid">
    <section class="platform center-block">
      <p id="quote" class="random_color text-center"><i class="fa fa-quote-left"></i></p>
      <p id="author" class="random_color text-muted text-right"></p>
      <hr />
      <a id="twitter" class="random_background btn" target="_blank" href="#" title="Tweet this ?"><i class="fa fa-twitter fa-lg"></i></a>
      <button id="new_quote" class="random_background btn pull-right"><i class="fa fa-refresh"></i></button>
    </section>
    <p class="text-center">Inspired by <a class="inspiration" target="_blank" href="https://codepen.io/hezag/pen/ZGxOLX"><strong>hezag</strong></a></p>
  </div>
</body>
              
            
!

CSS

              
                .inspiration {
  color: white !important;
  text-decoration: none !important;
}

.btn {
  color: #FFF !important;
  outline: none !important;
}

.btn:hover {
  opacity: 0.9;
}

.btn:disabled {
  cursor: pointer !important;
  opacity: 0.4 !important;
}

.random_background {
  transition: background-color 2s ease;
  background-color: #333;
  color: #FFF;
}

.random_color {
  transition: color 2s ease;
  color: #333;
}

.platform,
#history {
  background-color: #FFF;
  color: initial;
  width: 40em;
  margin: 7vw auto;
  margin-bottom: 0.5em;
  padding: 2em 4em;
  border-radius: 3px;
}

#quote {
  font-size: 1.6em;
}

#author {
  font-size: 1.1em;
}
              
            
!

JS

              
                var randomQuoteGenerator = (function IIFE () {
  var $variables = {}

  function preventImmediateRefresh () {
    $variables.newQuote.attr('disabled', true)
    setTimeout(function () {
      $variables.newQuote.attr('disabled', false)
    }, 1200)
  }

  function setTweet (quote, author) {
    return $variables.tweet.attr('href', 'https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=\'' + quote + '\' - ' + author)
  }

  function randomColor () {
    var randomColor = 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')'
    $($variables.randomColorClass).css('color', randomColor)
    $($variables.randomBackgroundColorClass).css('background-color', randomColor)
  }

  function getQuote () {
    return $.getJSON('https://api.icndb.com/jokes/random?limitTo=[nerdy]&escape=javascript', function (json) {
      setTweet(json.value.joke, 'Entry ' + json.value.id)
      $variables.quote.fadeOut('slow', function () {
        this.innerHTML = '<i class="fa fa-quote-left fa-lg"></i> ' + json.value.joke
      }).fadeIn('slow')
      $variables.author.fadeOut('slow', function () {
        this.innerHTML = '- Entry ' + json.value.id
      }).fadeIn('slow')
    })
  }

  function archiveQuote () {
    var previousQuote = $('<div class="row">' +
      '<div class="col-xs-2"></div>' +
      '<div class="col-xs-10"><p class="' + $variables.randomColorClass.slice(1) + '">' + $variables.quote.text().slice(1) + '</p></div>' +
      '</div>')

    if (!$variables.history.length) {
      $('<section id="' + $variables.history.selector.slice(1) + '" class="center-block"></section>').hide().delay('slow').appendTo($variables.container).slideDown('slow')
      $variables.history = $($variables.history.selector)
    } else {
      // TODO: delete temporary fix and make a proper one
      if ($variables.history.children().length >= 7) {
        $variables.history.children().slice(-2).remove()
      }
      if ($variables.history.children().length === 5) {
        $variables.history.children().slice(0, 2).delay('slow').slideUp('slow', function () {
          $(this).remove()
        })
      }
      $('<hr>').hide().delay('slow').appendTo($variables.history).slideDown('slow')
    }

    previousQuote.children('.col-xs-2').append($variables.tweet.clone().addClass('random_background'))
    previousQuote.hide().delay('slow').appendTo($variables.history).slideDown('slow')
  }

  function handleDocumentReady (event) {
    getQuote().then(randomColor)
    preventImmediateRefresh()
  }

  function handleQuoteButton (event) {
    archiveQuote()
    getQuote().then(randomColor)
    preventImmediateRefresh()
  }

  function init (options) {
    $variables.quote = $(options.quote)
    $variables.author = $(options.author)
    $variables.newQuote = $(options.newQuote)
    $variables.tweet = $(options.tweet)
    $variables.container = $(options.container)
    $variables.history = $(options.history)
    $variables.randomColorClass = options.randomColorClass // To include dynamically added elements
    $variables.randomBackgroundColorClass = options.randomBackgroundColorClass

    $(document).bind('ready', handleDocumentReady)
    $variables.newQuote.bind('click', handleQuoteButton)
  }

  return {
    init: init
  }
}())

$(document).ready(function () {
  randomQuoteGenerator.init({
    author: '#author',
    quote: '#quote',
    newQuote: '#new_quote',
    tweet: '#twitter',
    container: '.container-fluid',
    history: '#history',
    randomColorClass: '.random_color',
    randomBackgroundColorClass: '.random_background'
  })
})

              
            
!
999px

Console