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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="question">
  <p>Match the following</p>
  <p>(Click an item on the LHS to select, then click an item on RHS to select the option)</p>
  <div class="stems">
    <ol>
      <li id="s1">Rabbit</li>
      <li id="s2">Peacock</li>
      <li id="s3">Turtle</li>
    </ol>
  </div>
  <div class="options">
    <ol start="a">
      <li id="o1">Reptile</li>
      <li id="o2">Mammal</li>
      <li id="o3">Bird</li>
    </ol>
  </div>
</div>
              
            
!

CSS

              
                .stems,
.options {
  li {
    list-style: none;
    padding: 0.5rem 1.5rem;
    margin: 1rem;
    &:hover {
      padding: 0.4rem 1.5rem 0.6rem;
    }
  }
}

.stems,
.options {
  width: 40%;
  margin-right: 10%;
  float: left;
}

.stems li,
.options li {
  background: #e5e5e5;
  cursor: pointer;
  &.matched,
  &.matched:hover {
    background: #aaccaa;
  }
  &.selected,
  &.selected:hover {
    background: #a9ffa9;
    font-weight: bold;
    box-shadow: 1px 2px 1px #666;
  }
  &:hover {
    background: #a9cca9;
  }
}

.line {
  width: 100%;
  height: 2px;
  background-color: #e5e5e5;
  z-index: -10;
  &.highlighted {
    background-color: green;
    box-shadow: 1px 2px 1px #666;
    height: 3px;
    z-index: 99;
    .point {
      border-left: 16px solid green;
    }
  }
  .point {
    width: 0;
    height: 0;
    margin: -6px -3px 0 0;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-left: 16px solid #e5e5e5;
    float: right;
  }
}

              
            
!

JS

              
                (function($) {
  function lineDistance(x, y, x0, y0) {
    return Math.sqrt((x -= x0) * x + (y -= y0) * y);
  }

  function line_exists(stem) {
    var $exists = false;
    $(".line").each(function() {
      if (
        $(this).data("stem") === stem.attr("id")
      ) {
        $exists = true;
      }
    });
    return $exists;
  }

  function drawLine(stem, option) {
    var pointA = stem.offset();
    pointA.left = pointA.left + stem.outerWidth();
    pointA.top = pointA.top + stem.outerHeight() / 2;
    var pointB = option.offset();
    pointB.top = pointB.top + option.outerHeight() / 2;
    var angle =
      Math.atan2(pointB.top - pointA.top, pointB.left - pointA.left) *
      180 /
      Math.PI;
    var distance = lineDistance(
      pointA.left,
      pointA.top,
      pointB.left,
      pointB.top
    );

    var line = $('<div class="line highlighted"/>');
    line.append($('<div class="point"/>'))
    line.attr("data-stem", stem.attr("id"));
    line.attr("data-option", option.attr("id"));
    $(".question").append(line);
    line.css("transform", "rotate(" + angle + "deg)");

    // Set Width
    line.css("width", distance + "px");

    // Set Position
    line.css("position", "absolute");

    if (pointB.top > pointA.top) {
      $(line).offset({ top: pointA.top, left: pointA.left });
    } else {
      $(line).offset({ top: pointB.top, left: pointA.left });
    }
  }

  $(".stems li").on("click", function() {
    stem = $(this);
    stem.toggleClass("selected");

    $(".stems li")
      .not(stem)
      .removeClass("selected");

    $(".options li").removeClass("selected");
    $(".line").removeClass("highlighted");

    if (stem.hasClass("selected")) {
      var stem_lines = $('.line[data-stem="' + stem.attr("id") + '"]');
      stem_lines.addClass("highlighted");

      stem_lines.each(function() {
        var $option = $(this).data("option");
        $('.options li[id="' + $option + '"]').addClass("selected");
      });
      $(".options").addClass("ready");
    } else {
      $(".options").removeClass("ready");
      $('.line[data-stem="' + stem.attr("id") + '"]').removeClass(
        "highlighted"
      );
    }
  });

  $(".options li").on("click", function() {
    if ($(".options").hasClass("ready")) {
      $(".options li").removeClass("selected");
      $(this).toggleClass("selected");
      var stem = $(".stems li.selected");
      var option = $(this);
   
        
        $(
          '.line[data-stem="' +
            stem.attr("id") +
            '"]'
        ).remove();
      drawLine(stem, option);
      
       var stem_lines = $('.line[data-stem="' + stem.attr("id") + '"]');
      if(stem_lines.length>0){
        stem.addClass('matched');
      } else{
        stem.removeClass('matched');
      }
      
    }
  });
})(jQuery);

              
            
!
999px

Console