ドキュメント

CSS/scrollTop

From jQuery JavaScript Library

Jump to: navigation, search

« Back to CSS

scrollTop( )

Gets the scroll top offset of the first matched element.
This method works for both visible and hidden elements.
Examples:

Get scrollTop

var p = $("p:first");
$("p:last").text( "scrollTop:" + p.scrollTop() );

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    var p = $("p:first");
$("p:last").text( "scrollTop:" + p.scrollTop() );

  });
  </script>
  <style>
  p { margin:10px;padding:5px;border:2px solid #666; }
  </style>
</head>
<body>
  <p>Hello</p><p></p>
</body>
</html>

NameType

scrollTop( val )

When a value is passed in, the scroll top offset is set to that value on all matched elements.
This method works for both visible and hidden elements.
Arguments:
valNumber
A positive number representing the desired scroll top offset.
Examples:

Get scrollTop

$("div.demo").scrollTop(300);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("div.demo").scrollTop(300);

  });
  </script>
  <style>
div.demo {
background:#CCCCCC none repeat scroll 0 0;
border:3px solid #666666;
margin:5px;
padding:5px;
position:relative;
width:200px;
height:100px;
overflow:auto;
}
  p { margin:10px;padding:5px;border:2px solid #666;width:1000px;height:1000px; }
  </style>
</head>
<body>
  <div class="demo"><h1>lalala</h1><p>Hello</p></div>
</body>
</html>

NameType