cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div id="traffic-light" data-state="green">
  <div class="light" data-show="red"></div>
  <div class="light" data-show="yellow"></div>
  <div class="light" data-show="green"></div>
</div>

<button id="timer" type="button">Trigger timer</button>
            
          
!
            
              $states: green yellow red;
$colors: (
  green: #34CA4A,
  yellow: yellow,
  red: red
);

@each $state in $states {
  [data-state="#{$state}"] [data-show="#{$state}"] {
    opacity: 1;
  }
  
  [data-show="#{$state}"] {
    color: map-get($colors, $state);
  }
}

#traffic-light {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  height: 20vh;
}

.light {
  opacity: 0.3;
  width: 5vmin;
  height: 5vmin;
  border-radius: 50%;
  background-color: currentColor;
  box-shadow: 0 0 1vmin currentColor;
  transition: opacity .3s;
}

#timer {
  appearance: none;
  background: #FDFDFD;
  color: #1A1A1A;
  border: none;
  border-radius: 1vmin;
  font-weight: bold;
  font-size: 1rem;
  padding: 2vmin;
  margin-left: 2rem;
  transition: transform .2s ease-in-out;
  
  &:active {
    transform: scale(0.9);
  }
  
  &:focus {
    outline: none;
  }
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #121212;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

*, *:before, *:after {
  box-sizing: border-box;
  position: relative;
}
            
          
!
            
              const machine = {
  green: {
    TIMER: 'yellow'
  },
  yellow: {
    TIMER: 'red'
  },
  red: {
    TIMER: 'green'
  }
};

let currentState = 'green';

// The finite state machine transition function
function transition(state, action) {
  return machine[state][action];
}

// Any side effects
function update(state) {
  currentState = state;
  document.getElementById('traffic-light')
    .setAttribute('data-state', state);
}

setInterval(() => {
  const nextState = transition(currentState, 'TIMER');
  update(nextState);
}, 2000);

document.getElementById('timer')
  .addEventListener('click', () => {
    const nextState = transition(currentState, 'TIMER');
    update(nextState);
  });
            
          
!
999px
Loading ..................

Console