Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="aa-body-bg">
</div>
<header>
  <h1>Vertical Scroll UI with changing background</h1>
</header>
<section class="aa-panel" data-imgsrc="https://hbr.org/resources/images/article_assets/2017/06/jun17-06-501911931.jpg">
  <div class="aa-panel-inner">
    <h1>解決</h1>
    <p class="aa-pronounce">jiě jué</p>
    <p class="aa-define">meaning to solve, to resolve, or to settle (as in a dispute)</p>
  </div>
</section>
<section class="aa-panel" data-imgsrc="http://www.campusconsult.com/wp-content/uploads/2015/07/EOS_Yale_FirstSession015.jpg">
  <div class="aa-panel-inner">
    <h1>校園</h1>
    <p class="aa-pronounce">xiào yuán</p>
    <p class="aa-define">meaning campus (literally "school garden")</p>
  </div>
</section>
<section class="aa-panel" data-imgsrc="https://cdn-images-1.medium.com/max/1500/1*m2gDBT_nc-iE7R4AM3sHBQ.jpeg">
  <div class="aa-panel-inner">
    <h1>問題</h1>
    <p class="aa-pronounce">wèn tí</p>
    <p class="aa-define">meaning problem, topic or issue</p>
  </div>
</section>
<section class="aa-panel" data-imgsrc="https://static1.squarespace.com/static/57153f6ec2ea51502e130441/t/5744a64e044262fce84fdb53/1464116820113/crown-heighst-restaurant-bkw-reservations-page.jpg?format=1500w">
  <div class="aa-panel-inner">
    <h1>訂位</h1>
    <p class="aa-pronounce">dìng wèi</p>
    <p class="aa-define">meaning to reserve or book something</p>
  </div>
</section>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Overpass:400,400i,700,700i');

$grid:0.875rem;
$scalar:1.2;
$base:1rem;
@function grid($x) {
  @return $grid * $x;
}
@function scale($x) {
	$i:0;
	$step:$base;
	@if $x < 0 {
		@while $i > $x {
			$step:$step/$scalar;
			$i:$i - 1;
		}
	}
	@else if $x > 0 {
		@while $i < $x {
			$step:$step*$scalar;
			$i:$i + 1;
		}
	}
	@return $step;
}
@function lead($s) {
  @return ceil($s/($grid*0.9)) * $grid;  
}
* {
  box-sizing:border-box;
}
body {
  font-family:"Overpass",Helvetica,Arial,sans-serif;
  font-size:$base;
  line-height:lead($base);
  margin:0;
  padding:grid(7) 0 0 0;
}
.aa-body-bg {
  height:100%;
  left:0;
  opacity:0.2;
  position:fixed;
  top:0;
  width:100%;
  .aa-body-bg-image {
    background-position:center center;
    background-repeat:no-repeat;
    background-size:cover;
    height:100%;
    left:0;
    opacity:0;
    position:absolute;
    top:0;
    transition:all 1s;
    width:100%;
  }
}
header {
  background-color:rgba(black,0.75);
  color:white;
  left:0;
  padding:grid(2);
  position:fixed;
  top:0;
  width:100%;
  h1 {
    margin:0;
    font-size:scale(4);
    line-height:lead(scale(4));
  }
}
ul.aa-scroll-control {
  display:block;
  font-size:0;
  left:0;
  padding:0px grid(1);
  position:fixed;
  top:50%;
  transform:translateY(-50%);
  li {
    display:block;
    margin:grid(1) 0;
    padding:0;
    a:link,
    a:visited {
      display:block;
      width:grid(1);
      height:grid(1);
      background-color:rgba(black,0.5);
      transition:all .3s;
    }
    a:hover,
    a:active {
      background-color:red;
    }
    a.aa-selected {
      background-color:blue !important;
      width:grid(3);
      transition:all .3s;
    }
  }
}
section {
  &.aa-panel {
    align-items:center;
    border-bottom:grid(0.25) solid transparent;
    display:flex;
    height:calc(100vh - #{grid(7)});
    justify-content: center;
    overflow:hidden;
    padding:grid(2);
    transition:border-bottom-color 1s;
  }
  .aa-panel-inner {
    opacity:0;
    padding:grid(2);
    transition:opacity 1s;
    // the following lines of css can be added to get around the issue of 
    //position:fixed;
    //top:grid(7);
    //left:0;
  }
  &.aa-panel.aa-selected {
    border-bottom:grid(0.25) solid black;
    .aa-panel-inner {
      opacity:1;
    }
  }
  h1 {
    font-size:scale(6);
    line-height:lead(scale(6));
    margin:0;
  }
  p.aa-pronounce {
    font-size:scale(2);
    line-height:lead(scale(2));
    margin:grid(2) 0;
  }
  p.aa-define {
    margin:0;
  }
}
              
            
!

JS

              
                $(function(){

  $('header').after('<ul class="aa-scroll-control"></ul>');
  
  var $panels = $('.aa-panel');
  var $control = $('.aa-scroll-control');

  var LoadBg = function(i){
    bg_src = $('#panel-' + i).attr('data-imgsrc').trim();
    bg_img = '<div data-target="#panel-' + i + '" class="aa-body-bg-image" style="background-image:url(' + bg_src + ');"></div>'
    $('.aa-body-bg').append(bg_img);
  };
  
  var scrollFx = function() {
    var ds = $(document).scrollTop();
    var of = $(window).height() / 2;
    
    // if the panel is in the viewport, indicate it is the active panel.
    for (var i = 0; i < $panels.length; i++) {
      if ($panels.eq(i).offset().top < ds+of && $panels.eq(i).offset().top + $(window).height() - $('header').outerHeight() > ds+of) {
        if (!($panels.eq(i).hasClass('aa-selected'))) {
          $panels.eq(i).addClass('aa-selected');
          $control.find('a').eq(i).addClass('aa-selected');
          $('.aa-body-bg-image[data-target="#panel-'+i+'"]').css('opacity',1);
          scrollTo('#'+$panels.eq(i).attr('id'),500);
        }
      } else {
        if ($panels.eq(i).hasClass('aa-selected')) {
          $panels.eq(i).removeClass('aa-selected');
          $control.find('a').eq(i).removeClass('aa-selected');
          $('.aa-body-bg-image[data-target="#panel-'+i+'"]').css('opacity',0);
        }
      }
    }
  };
  
  var scrollTo = function(panelID,speed) {
    if ((!$('body').hasClass('panelscrolling'))) {
      var dest;
      if ( $(panelID).offset().top > $(document).height()-$(window).height() ){
        dest = $(document).height()-$(window).height();
      }
      else {
        dest = $(panelID).offset().top - $('header').outerHeight();
      }
      $('html,body').addClass('panelscrolling').animate({scrollTop:dest}, speed,'swing',function(){
        console.log('dest: '+dest);
        $('html,body').scrollTop(dest);
        $('html,body').removeClass('panelscrolling');
      });
    }
  };

  $panels.each(function(i){
    // create an indicator for this panel
    $(this).attr('id','panel-' + i);
    var listItem = '<li><a href="#panel-' + i + '"></a></li>' ;
    $control.append(listItem);
    
    // add a background for this panel
    var imagetoload = new Image();
    imagetoload.onload = LoadBg(i);
    imagetoload.src = $(this).attr('data-imgsrc');    
  });
  
  $(".aa-scroll-control a").on('click', function(e) {
    e.preventDefault();
    scrollTo(this.hash,1000);
  });
  
  scrollFx();
  $(window).on('scroll', function(e){
    scrollFx();
  });
  
});
              
            
!
999px

Console