1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta http-equiv="cache-control" content="public, max-age=60">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<meta name="author" content="Daniel Hagnoul">
<title>Test</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/sunny/jquery-ui.css">
<style>
#demo {
background: #ccc none repeat scroll 0 0;
border: 3px solid #666;
margin: 5px;
padding: 5px;
position: relative;
width: 200px;
height: 100px;
overflow: auto;
}
#demo p {
margin: 10px;
padding: 5px;
border: 2px solid #666;
width: 1000px;
height: 1000px;
}
</style>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
"use strict";
$( function(){ // forme abrégée de $(document).ready(function(){
});
$( window ).load( function(){
// valeur max du scroll après chargement de la page
$( "#demo" ).scrollTop( 999999 );
var scrollMax = $( "#demo" ).scrollTop();
$( "#demo" ).scrollTop( 0 );
$( "body" ).prepend( "<p>scrollMax = " + scrollMax + " px.</p>" );
// tests
function showHeight( element, height ){
$( "#height" ).append( "<p>La hauteur de " + element + " est de " + height + "px.</p>" );
}
showHeight( "#demo height", $( "#demo" ).height() );
showHeight( "#demo innerHeight", $( "#demo" ).innerHeight() );
showHeight( "#demo outerHeight", $( "#demo" ).outerHeight() );
var h = 0,
hi = 0,
ho = 0;
$( "#demo" ).children().each( function( i, item ){
h += $( item ).height();
hi += $( item ).innerHeight();
ho += $( item ).outerHeight();
});
showHeight( "#demo children height", h );
showHeight( "#demo children innerHeight", hi );
showHeight( "#demo children outerHeight", ho );
$( "#demo" ).on( "scroll", function( ){
$( "#scroll" ).text( "scrollTop div demo " + $( this ).scrollTop() + " px." );
});
$( "#demo" ).scrollTop( 300 );
});
</script>
</head>
<body>
<div id="scroll"></div>
<div id="demo">
<h1>lalala</h1>
<p>Hello</p>
</div>
<div id="height"></div>
</body>
</html> |
Partager