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

Untitled

A Pen By Captain Anonymous

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.

            
              <h3>Scrolling with jQuery Example</h3>
<p>Use the links below to see how the different methods work.</p><br>

<div class="scroll-links">
  <a href="javascript:;" class="scroll-webkit-only">Webkit</a>
  <a href="javascript:;" class="scroll-other-only">Non-Webkit</a>
  <a href="javascript:;" class="scroll-double">Both, double call</a>
  <a href="javascript:;" class="scroll-document">Document</a>
  <a href="javascript:;" class="scroll-window">Window</a>
  <a href="javascript:;" class="scroll-browser">$.browser</a>
  <a href="javascript:;" class="scroll-modernizr">Modernizr</a>
</div>

<div class="log-container">
  <small>Event Log</small>
  <div class="log">
    <p><span></span></p>
  </div>
</div>

<div class="bottom-links">
  <div class="scroll-links">
    <a href="javascript:;" class="scroll-webkit-only bottom">Webkit</a>
    <a href="javascript:;" class="scroll-other-only bottom">Non-Webkit</a>
    <a href="javascript:;" class="scroll-double bottom">Both, double call</a>
    <a href="javascript:;" class="scroll-document bottom">Document</a>
    <a href="javascript:;" class="scroll-window bottom">Window</a>
    <a href="javascript:;" class="scroll-browser bottom">$.browser</a>
    <a href="javascript:;" class="scroll-modernizr bottom">Modernizr</a>
  </div>
</div>
            
          
!
            
              @import "bourbon";

$link:          #3813c3;
$solution-link: #15a95c;

.scroll-links {
  a {
    color: $link;
    margin-right: 15px;
    
    &:hover {
      color: darken($link, 15%);
    }
  }
}

a.scroll-modernizr {
  color: $solution-link;
  font-size: 110%;
  font-weight: bold;
  
  &:hover {
    color: darken($solution-link, 15%);
  }
}

.log-container {
  height: 300px;
  margin-top: 30px;
  @media(min-width: 1190px) {
    height: 50%;
    position: fixed;
    right: 20px;
    top: 0;
  }
}

small {
  margin-bottom: 5px;
}

.log {
  border: 1px solid #ddd;
  color: #444;
  height: 100%;
  overflow-y: scroll;
  padding: 10px;
  width: 500px;
  
  p {
    padding: 0;
    margin: 0;
  }
}

.bottom-links {
  margin-bottom: 20px;
  margin-top: 1000px;
}
            
          
!
            
              $(function(){
  var errorMsgColor = "#e25440";
  var clickMsgColor = "#444;"
  var successMsgColor = "#25b34e";
  
  // Should only work in webkit browsers
  $('.scroll-webkit-only').click(function(){
    scroll.log('"scroll-webkit-only" clicked', clickMsgColor);
    $('body').animate({
      scrollTop: scroll.position($(this))
    }, 'slow', function(){
      scroll.log('animate function complete', successMsgColor);
    });
  });
  
  // Should only work in non-webkit browsers
  $('.scroll-other-only').click(function(){
    scroll.log('"scroll-other-only" clicked', clickMsgColor);
    $('html').animate({
      scrollTop: scroll.position($(this))
    }, 'slow', function(){
      scroll.log('animate function complete', successMsgColor);
    });
  });
  
  // Should work in both types of browsers, but the animate function will execute twice (see event log)
  $('.scroll-double').click(function(){
    scroll.log('"scroll-double" clicked', clickMsgColor);
    $('body, html').animate({
      scrollTop: scroll.position($(this))
    }, 'slow', function(){
      scroll.log('animate function complete', successMsgColor);
    });
  });
  
  // Should cause exception once, and then not work
  $('.scroll-document').click(function(){
    scroll.log('"scroll-document" clicked', clickMsgColor);
    try {
      $(document).animate({
        scrollTop: scroll.position($(this))
      }, 'slow', function(){
        scroll.log('animate function complete', successMsgColor);
      });
    }
    catch(err){
      scroll.log(err, errorMsgColor);
    }
  });
  
  // Should cause exception once, and then not work
  $('.scroll-window').click(function(){
    scroll.log('"scroll-window" clicked', clickMsgColor);
    try {
      $(window).animate({
        scrollTop: scroll.position($(this))
      }, 'slow', function(){
        scroll.log('animate function complete', successMsgColor);
      });
    }
    catch(err){
      scroll.log(err, errorMsgColor);
    }
  });
  
  // Should cause uncaught exception (unless you include the jQuery migrate file, but is bad practice)
  $('.scroll-browser').click(function(){
    scroll.log('"scroll-browser" clicked', clickMsgColor);
    try {
      $(jQuery.browser.webkit ? 'body' : 'html').animate({
        scrollTop: scroll.position($(this))
      }, 'slow', function(){
        scroll.log('animate function complete', successMsgColor);
      });
    }
    catch(err){
      scroll.log(err, errorMsgColor);
    }
  });
  
  // Best solution
  $('.scroll-modernizr').click(function(){
    scroll.log('"scroll-modernizr" clicked', clickMsgColor);
    var selector = Modernizr.testProp('webkitAppearance') ? 'body' : 'html';
    
    $(selector).animate({
      scrollTop: scroll.position($(this)) 
    }, 'slow', function(){
      scroll.log('animate function complete', successMsgColor);
    });
  });
});

var scroll = {
  position: function($context){
    var botLinkPos = $('.bottom-links').position().top + parseInt($('.bottom-links').css('margin-top')); 
    return $context.hasClass('bottom') ? '0' : botLinkPos.toString();
  },
  log: function(message, color) {
    var now = new Date();
    var time = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    var msgColor = color === undefined ? '#444' : color;
    
    $('.log > p').prepend('<span style="color: ' + msgColor + '">' + message + ' (' + time + ')</span><br/>');
  }
};



            
          
!
999px
Loading ..................

Console