123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <form id="survey-form">
  <div id="start-survey">
    <div class="start-survey-items">
      <h1>Pets Preferences Survey</h1>
      <div>Start Survey</div>
    </div>
  </div>
  <div id="previous-question">Previous Question</div>
  <div id="survey-wrapper" class="owl-carousel">
    <div class="survey-question">
      <div class="question-items">
        <h2>I prefer:</h2>
        <input type="radio" name="group1" id="group1-option1" value="dogs"/>
        <label for="group1-option1">Dogs</label>
        <input type="radio" name="group1" id="group1-option2" value="cats"/>
        <label for="group1-option2">Cats</label>
      </div>
    </div>
    <div class="survey-question">
      <div class="question-items">
        <h2>I prefer:</h2>
        <input type="radio" name="group2" id="group2-option1" value="long fur"/>
        <label for="group2-option1">Long Fur</label>
        <input type="radio" name="group2" id="group2-option2" value="short fur"/>
        <label for="group2-option2">Short Fur</label>
        <input type="radio" name="group2" id="group2-option3" value="no fur"/>
        <label for="group2-option3">No Fur</label>
      </div>
    </div>
    <div class="survey-question">
      <div class="question-items">
        <h2>I prefer:</h2>
        <input type="radio" name="group4" id="group4-option1" value="large"/>
        <label for="group4-option1">Large Pets</label>
        <input type="radio" name="group4" id="group4-option2" value="medium-sized"/>
        <label for="group4-option2">Medium-sized Pets</label>
        <input type="radio" name="group4" id="group4-option3" value="small"/>
        <label for="group4-option3">Small Pets</label>
      </div>
    </div>
    <div class="survey-results">
      <div class="results-items">
        <h2>Results</h2>
        <p>
          You prefer <span class="response-span" id="group4-response"></span> <span class="response-span" id="group1-response"></span> with <span class="response-span" id="group2-response"></span>.
        </p>
        <button id="start-over" type="reset">Start Over</button><button id="submit" type="submit">Submit Results</button>
      </div>
    </div>
  </div>
  <div id="progress-wrapper">
    <div id="progress-bar"></div>
  </div>
</form>
            
          
!
            
              *,*:before,*:after {
  box-sizing:border-box;
}

/* Owl Styles */

.owl-stage,.owl-stage-outer,.owl-item {
  height:100%;
}

.owl-controls {
  position:absolute;
  bottom:0;
  width:100%;
  padding:20px;
}

body,html,#survey-form {
  height:100%;
  width:100%;
  font-family:helvetica neue,helvetica,arial,sans-serif;
  font-size:18px;
  font-weight:200;
}

body {
  position:relative;
}

#start-survey {
  height:100%;
  width:100%;
  display:flex;
  align-items: center;
  justify-content: center;
  background:white;
  position:absolute;
  z-index:2;
  left:0;
  text-align:center;
  transition:.5s ease-in-out;
  transform:translateX(0);
  &.remove {
    transform:translateY(-100%);
    opacity:0;
  }
  .start-survey-items {
    h1 {
      font-size:30px;
      margin:0 0 20px 0;
    }
    div {
      display:inline-block;
      padding:20px;
      border-radius:5px;
      background:#207bb4;
      color:white;
      box-shadow:inset 0 0 0 #08598c;
      transition:.25s ease-in-out;
      cursor:pointer;
      &:hover {
        box-shadow:inset 200px 0 0 #08598c;
      }
    }
  }
}

#previous-question {
  position:absolute;
  top:20px;
  left:0;
  text-align:center;
  z-index:2;
  display:none;
  width:100%;
  font-size:14px;
  line-height:14px;
  cursor:pointer;
  &:hover {
    color:#207bb4;
  }
}

#survey-wrapper {
  height:100%;
  width:100%;
  overflow:hidden;
  position:relative;
  z-index:1;
  background:#dfdfdf;
  .survey-question,.survey-results{
    height:100%;
    width:100%;
    padding:20px;
    display:flex;
    align-items: center;
    justify-content: center;
    text-align:center;
    h2 {
      width:100%;
      padding:20px;
      font-size:25px;
      line-height:20px;
    }
    p {
      margin:0 0 20px 0;
    }
    label {
      display:inline-block;
      padding:20px;
      margin:0 5px;
      background:white;
      border-radius:5px;
      box-shadow:inset 0 0 0 #207bb4;
      transition:.25s ease-in-out;
      cursor:pointer;
      &:hover {
        box-shadow:inset 300px 0 0 #207bb4;
        color:white;
      }
    }
    input[type="radio"]{
      position:absolute;
      visibility:hidden;
      &:checked {
        + label {
          background:#08598c;
          color:white;
        }
      }
    }
    button {
      padding:20px;
      margin:20px 10px;
      border-radius:5px;
      background:#207bb4;
      color:white;
      box-shadow:inset 0 0 0 #08598c;
      transition:.25s ease-in-out;
      cursor:pointer;
      font-family:helvetica neue,helvetica,arial,sans-serif;
      font-size:18px;
      font-weight:200;
      line-height:18px;
      border:none;
      &:hover {
        box-shadow:inset 200px 0 0 #08598c;
      }
    }
  }
}

#progress-wrapper {
  width:calc(100% - 100px);
  max-width:960px;
  height:8px;
  background:#f5f5f5;
  position:absolute;
  z-index:1;
  bottom:20px;
  left:50%;
  margin-left:-480px;
  border-radius:3px;
  &:hover {
    #progress-bar {
      &:after {
        top:-38px;
        opacity:1;
      }
    }
  }
  @media all and (max-width:1060px){
    left:50px;
    margin-left:0px;
  }
  #progress-bar {
    background:#28a8f8;
    height:100%;
    width:0%;
    transition:.5s ease-in-out;
    border-radius:3px;
    position:relative;
    &:after {
      content:attr(data-progress)'%';
      position:absolute;
      text-align:center;
      font-size:12px;
      line-height:12px;
      display:inline-block;
      background:white;
      padding:10px;
      width:50px;
      top:-28px;
      right:-25px;
      border-radius:5px;
      opacity:0;
      transition:.25s ease-in-out;
    }
  }
}
            
          
!
            
              $(document).ready(function() {
  // Randomize the survey questions
  $('#survey-wrapper').randomize('.survey-question');
  // Initialize Progress Bar
  var totalQuestions = ($(".survey-question").length);
  var incrementWidth = 100 / totalQuestions;
  
  // Progress Bar Update
  $('.survey-question input').click(function() {
    setTimeout(function() {
      owl.trigger('next.owl');
      var wrapperWidth = $('#progress-wrapper').width();
      var progressWidth = $('#progress-bar').width();
      var currentWidth = (progressWidth / wrapperWidth) * 100;
      var newWidth = currentWidth + incrementWidth;
      $('#progress-bar').css('width', newWidth + '%').attr('data-progress',newWidth);
    }, 500);
    
    // generate responses
    setTimeout(function(){
      var lastSlide = $('.owl-item:last-of-type');
    	if (lastSlide.hasClass('active')){
      	var resultsContent = $('.results-items > h2 + p').text();
        // large long haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Lions are not great pets.</p>')
				}
        // large short haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Tigers are not great pets.</p>')
				}
        // large no haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Good luck shaving a puma.</p>')
				}
        // medium long haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Snow leopards are great!</p>')
				}
        // medium short haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">I agree. Ocelots are particularly pretty.</p>')
				}
        // medium no haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Good luck shaving a bobcat!</p>')
				}
        // small long haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">I recommend a Maine Coon. They are amazing.</p>')
				}
        // small short haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Tonkanese are beautiful and even tempered.</p>')
				}
        // small no haired cats response
        if (resultsContent.indexOf('cats') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Sphinx are amazing - but <strong>very</strong> affectionate.</p>')
				}
        // large short-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Great danes are awesome.</p>')
				}
        // large no-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Perhaps a Mexican Hairless?</p>')
				}
        // large long-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('large') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Well, St. Bernards are great, but they drool a lot.</p>')
				}
        // medium short-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Boxers are the best dogs in the world. Hands down.</p>')
				}
        // medium no-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">American Hairless Terriers are good dogs, but hard to find.</p>')
				}
        // medium long-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('medium') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Border Collies are very smart and fun.</p>')
				}
        // small short-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('short fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">You cannot go wrong with a beagle.</p>')
				}
        // small no-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('no fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Chinese Cresteds are fun-looking, but a little annoying.</p>')
				}
        // small long-haired dogs response
        if (resultsContent.indexOf('dogs') > -1
           && resultsContent.indexOf('small') > -1
           && resultsContent.indexOf('long fur') > -1) {
  				$('.results-items > h2 + p').after('<p class="response">Fancy daschunds are fun little dogs.</p>')
				}
    	}
    },501)
  });
  $('#previous-question').click(function(){
    owl.trigger('prev.owl');
      var wrapperWidth = $('#progress-wrapper').width();
      var progressWidth = $('#progress-bar').width();
      var currentWidth = (progressWidth / wrapperWidth) * 100;
      var newWidth = currentWidth - incrementWidth;
      $('#progress-bar').attr('data-progress', newWidth).css('width', newWidth + '%');
  });
  
  // Initialize Owl Carousel
  var owl = $('#survey-wrapper');

  owl.owlCarousel({
    items: 1,
    mouseDrag: false,
    touchDrag: false,
    pullDrag: false,
    rewindNav: false,
    rewindSpeed: 500
  });
  
  // remove default Owl Carousel Bullets
  $('.owl-controls').remove();

  // begin survey
  $("#start-survey").click(function() {
    $(this).addClass('remove');
  });

  // reset carousel and progress bar on "start over"
  $('#start-over').click(function() {
    owl.trigger('goTo.owl', [0]);
    $('#progress-bar').css('width','0%');
    $('p.response').remove();
    $('span.response-span').empty();
  });
  
  // show previous question button once user has gotten into the quiz
  $('.owl-item:first-of-type .survey-question input').change(function(){
    $('#previous-question').fadeIn(1000);
  });
  
  // hide previous question button once user has finished the quiz
  $('.owl-item:nth-last-child(2) .survey-question input').click(function(){
    $('#previous-question').fadeOut(500);
  });
  // hide previous question button if user backtracks to first slide
  $('#previous-question').click(function(){
    var firstSlide = $('.owl-item:first-of-type');
    if ($(firstSlide).hasClass('active')){
      $(this).fadeOut(500);
    }
  });
  
  // display results 
  $('input').click(function(){
    var inputName = $(this).attr('name');
    var inputValue = $(this).attr('value');
    $('#'+inputName+'-response').html(inputValue);
  });
});

// randomize function
(function($) {

  $.fn.randomize = function(childElem) {
    return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);

      elems.sort(function() {
        return (Math.round(Math.random()) - 0.5);
      });

      $this.remove(childElem);

      for (var i = 0; i < elems.length; i++)
        $this.prepend(elems[i]);

    });
  }
})(jQuery);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console