jQuery Page Widget How to - Stick text to left top corner when Scroll








Question

We would like to know how to stick text to left top corner when Scroll.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
#Menu {<!-- w  w  w .j  a v a  2  s . c o  m-->
  position: fixed;
  top: 0px;
  width: 1000px;
  height: 50px;
  background-color: #EEE;
}

body {
  margin: 0px;
  padding: 0px;
  width: 50000px;
  height: 3000px;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $(window).on('scroll', function () {
        $("#Menu").css('left', '-' + $(this).scrollLeft() + 'px');
    });
});
</script>
</head>
<body>
  <div id="Menu" style="color: white;">This Text Stays Here!</div>
</body>
</html>

The code above is rendered as follows: