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.

+ 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.

            
              <!--  transcript--locked -->
<div class="transcript">
  <div class="transcript__mask"></div>
  <div class="transcript__player">
    <audio crossOrigin="anonymous" controls id="customplayer" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/81793/freesoftware.mp3" captions="https://codepen.io/scottcarver/pen/d017ffedd3a5ecda3d4d4e0fdfe47d10.js"></audio>
  </div>
  <div class="transcript__readout" id="readout"></div>
  <!--
  <div class="transcript__lock"><label><input type="checkbox" id="locked" checked />Unlock</label></div> -->
</div>


<!-- this is hidden temporarily -->
<div id="log" style="display:none;"></div>
            
          
!
            
              html {
  box-sizing: border-box;
  height:100%;
}
body{
  height:100%;
  display:flex;
  align-items:center;
}
*, *:before, *:after {
  box-sizing: inherit;
}


.transcript{
  max-width:800px;
  width:100%;
  margin:0px auto;
  border:solid 1px grey;
  height:200px;
  overflow:hidden;
  position:relative;
  
  &.transcript--locked{
    .transcript__mask{
     display:block; 
    }
  }
  
  .transcript__lock{
    position:absolute;
    z-index: 20;
    background:white;
    right:0;
    padding:3px;
  }
  .transcript__mask{
    display:none;
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    background:rgba(50,50,50,.1);
    z-index:10;
    // mix-blend-mode:color-burn;
  }
  .transcript__player{
    position:absolute;
    bottom:0;
    width:100%;
    left:0;
    // background:pink;
    z-index:20;
    
    audio{
      width:100%;
      vertical-align:top;
    }
  }
  
  .transcript__readout{
    position:absolute;
    height:160px;
    top:0px;
    overflow-y:auto;
    width:100%;
     padding:20px;
    
    span{
     cursor:pointer;
     font-size:1.5em;
      color:#999;
      &:hover{
        background-color:yellow;
        color:black;
      }
      &.active{
        background-color:yellow;
        color:black;
      }
    } 
  }
  
  
  
}
            
          
!
            
              
var customplayer = document.getElementById("customplayer");
var captions = $(customplayer).attr('captions');
var mostrecentIndex = null;

$.ajax({
  dataType: "json",
  url: captions,
  success: function(json){
    customplayer.captions = json.script;
    console.log(customplayer.captions);
    showCaptions();
  }
});


// When the time changes, highlight nearest caption
customplayer.ontimeupdate = function(){
  getNearestCaption(); // customplayer.currentTime
}


// Setup Captions
function showCaptions(){
  
 for(var j=0; j < customplayer.captions.length; j++){
   var timecode = customplayer.captions[j].timecode;
   var markup = '<span id="transcripthandle' + j + '" data-starttime="'+timecode+'">';
       markup += customplayer.captions[j].text;
       markup += ' </span>';
   $(".transcript__readout").append(markup);
  
 } 
  
  enableReverseSeek();
}


// When A caption is clicked, set the timecode and active/scroll
function enableReverseSeek(){
  $('.transcript__readout span').on("click", function(){
    var starttime = $(this).data("starttime");
    customplayer.currentTime = starttime;
    // Clear Old highlight
    // $(".transcript__readout span").removeClass('active');
    // Set New Highlight
    // $(this).addClass('active');
     getNearestCaption();
  });
}


// Using only a timecode, highlight the nearest caption
function getNearestCaption(){
  
  // Clear Old highlight
  $(".transcript__readout span").removeClass('active');
  
  var captionIndex = null;
  var currentTime = customplayer.currentTime;
  console.log("currently " + customplayer.currentTime);
  // Iterate over all the captions
  
  for(i=0;i<customplayer.captions.length;i++){
    var startTime = customplayer.captions[i].timecode;
    var endTime = customplayer.captions[i].endcode;
    
    
    // When we find a match;
    if(currentTime >= startTime && currentTime < endTime ){
      // Save Caption Index 
      captionIndex = i;
    }
  }
  

  
  
  
  
  
  
  // Make sure captionIndex and mostrecentIndex do not match
  if(captionIndex != null){
    
    var targetElem = $(".transcript__readout span").eq(captionIndex);
    var containerElem = $('.transcript__readout');
    var containerHeight = containerElem.height();
    var caption = {
      height : targetElem.height(),
      offset : targetElem.offset()
    }
    
    
 
    
    console.log("caption");
     console.log(targetElem);
    console.log("container");
     console.log(containerElem.scrollTop());
    
    
    $("#log").empty();
    $("#log").append('<p>Container is ' + containerElem.scrollTop() + " From top" + '</p>');
    $("#log").append('<p>Caption height is ' + targetElem.height() +  '</p>');
    $("#log").append('<p>Caption  is ' + targetElem.offset().top +  ' from top</p>');
    $("#log").append('<p>Caption distFromTop is ' + (targetElem.offset().top + containerElem.scrollTop()) +  ' from top</p>');
    /*
    if (rectElem.bottom > rectContainer.bottom){
      console.log('no scroll');
    } 
    if (rectElem.top < rectContainer.top){
      console.log('do scroll');
    } 
    */
    
    
    // Set New Highlight
    targetElem.addClass('active');
    // And Scroll there
    var dynamicID = "#transcripthandle"  + captionIndex;
    TweenLite.to($(".transcript__readout"), .2, {
      scrollTo:{  y: dynamicID,
                offsetY:20,
                autoKill:false
               }
      
    });
    // Okay, now set mostrecentIndex
    mostrecentIndex = captionIndex;
  }
  
  
}


// Enable/Disable click mask on interface
$('#locked').change(function() {
   if($(this).is(":checked")) {
      $('.transcript').addClass('transcript--locked');
   } else {
     $('.transcript').removeClass('transcript--locked');
   }
});
            
          
!
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.
Loading ..................

Console