Ver Mensaje Individual
  #5 (permalink)  
Antiguo 08/07/2012, 23:39
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: como detectar que el scroll del navegador está en el top (o casi en el top

Hay algo de esta historia que no me cierra.
La página en cuestión, cambia el alto de su contenido en forma dinámica?, es decir, puede que se agreguen elementos que la lleven de no tener a tener scroll?
Si este no es el caso, no tiene mucha gracia,
Dicho esto, oportuna la apreciación de @furoya.

Con su aporte(lo hice algo más compatible) más lo que propongo, y si de no usar Jquery se trata, les dejo esto

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <style type="text/css">
  7. /*<![CDATA[*/
  8. body{
  9. background: #AFAE85;
  10. font-weight: bold;
  11. }
  12. .relleno{
  13. display: none;
  14. }
  15. .b_scroll{
  16. display: none;
  17. }
  18. /*]]>*/
  19. <script type="text/javascript">
  20. //<![CDATA[
  21. // uso
  22. // scrollSuave('ddd',3000) --> id de la capa,tiempo en milisegundos, si id de capa es 0, va a la coordenada top 0
  23.  
  24. var altura,winH,timerY;
  25. var tags_botones = new Array();
  26. onscroll = function(){
  27. altura = document.body.clientHeight-window.innerHeight;
  28. winH = document.documentElement.offsetHeight;
  29. if (document.body && document.body.offsetHeight) {
  30. winH = document.body.offsetHeight;
  31. }
  32. if (document.compatMode=='CSS1Compat' &&
  33. document.documentElement &&
  34. document.documentElement.offsetHeight ) {
  35. winH = document.documentElement.offsetHeight;
  36. }
  37. if (window.innerWidth && window.innerHeight) {
  38. winH = window.innerHeight;
  39. }
  40. if((altura-10) > winH){
  41. tags_botones=document.getElementsByTagName('button');
  42. var i;
  43. for (i=0; i<tags_botones.length; i++) {
  44. if (tags_botones[i].className=='b_scroll') {
  45. tags_botones[i].style.display = 'block';
  46. }
  47. }
  48. }
  49. } // fin onscroll
  50.  
  51. function scrollSuave(capa,tiempo){
  52. var PosObjeto = document.getElementById(capa);
  53. if(capa == 0){
  54. var PosObjeto = document.body;
  55. }
  56. var PosY = 0;
  57. while(PosObjeto) {
  58. PosY += (PosObjeto.offsetTop - PosObjeto.scrollTop + PosObjeto.clientTop);
  59. PosObjeto = PosObjeto.offsetParent;
  60. }
  61. hacerScroll(document.documentElement,PosY, tiempo);  
  62. } // fin scroll suave
  63.  
  64. function hacerScroll(elementoY, destinoY, demora) {
  65. if (demora < 0) return;
  66. var diferencia = destinoY - elementoY.scrollTop;
  67. var pixelsY = diferencia / demora * 10;
  68. timerY= setTimeout(function() {
  69. elementoY.scrollTop = elementoY.scrollTop + pixelsY;
  70. hacerScroll(elementoY, destinoY, demora - 10);
  71. if(elementoY.scrollTop == destinoY){
  72. clearTimeout(timerY);
  73. }
  74. }, 10);
  75. } // fin scroll suave
  76.  
  77. // solo para la demo, genera contenido extra y scroll
  78. var tags_div = new Array();
  79. function rellenar(estado){
  80. tags_div=document.getElementsByTagName('div');
  81. tags_botones=document.getElementsByTagName('button');
  82. var i;
  83. var b;
  84. for (i=0; i<tags_div.length; i++) {
  85. if (tags_div[i].className=='relleno') {
  86. if(estado == 'mostrar'){
  87. tags_div[i].style.display = 'block';
  88. }
  89. if(estado == 'ocultar'){
  90. tags_div[i].style.display = 'none';
  91. for (b=0; b<tags_botones.length; b++) {
  92. if (tags_botones[b].className=='b_scroll') {
  93. tags_botones[b].style.display = 'none';
  94. }
  95. }
  96.  
  97. }
  98. }
  99. }
  100. } // fin rellenar
  101.  
  102. //]]>
  103. </head>
  104. <span>top</span>
  105. <form action="#">
  106. <input type="button" onclick="rellenar('mostrar')" value="rellenar" /> <input type="button" onclick="rellenar('ocultar')" value="ocultar" />
  107. </form>
  108. <div class="relleno" style="height: 100px"><!-- relleno alto --></div>
  109. <div id="aaa" style="color: green;">Demo scroll suave. Demo scroll  suave. Demo scroll suave.</div>
  110. <div class="relleno" style="height: 1500px"><!-- relleno alto --></div>
  111. <div id="ddd" style="color: darkred;">Demo scroll suave. Demo scroll  suave. Demo scroll suave.</div>
  112. <div class="relleno" style="height: 800px"><!-- relleno alto --></div>
  113. <button class="b_scroll" onclick="scrollSuave('ddd',3000);">Rojo</button>
  114. <button class="b_scroll" onclick="scrollSuave('aaa',100);">Verde</button>
  115. <button class="b_scroll" onclick="scrollSuave(0,3000);">Top</button>
  116. </body>
  117. </html>

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.