¿Cómo puedo determinar la altura y la posición de desplazamiento de la ventana en jQuery?


Necesito agarrar la altura de la ventana y el desplazamiento de desplazamiento en jQuery, pero no he tenido suerte encontrando esto en los documentos de jQuery o Google.

Estoy 90% seguro de que hay una forma de acceder a height y scrollTop para un elemento (presumiblemente incluyendo la ventana), pero simplemente no puedo encontrar la referencia específica.

Author: T J, 2008-11-20

4 answers

Desde jQuery Docs:

const height = $(window).height();
const scrollTop = $(window).scrollTop();

Http://api.jquery.com/scrollTop/
http://api.jquery.com/height/

 291
Author: Pim Jager,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-06 18:35:35

De http://api.jquery.com/height / (Nota: La diferencia entre el uso de la ventana y el objeto del documento)

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

De http://api.jquery.com/scrollTop/

$(window).scrollTop() // return the number of pixels scrolled vertically
 41
Author: Aidamina,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-10 13:39:32
$(window).height()

$(window).width()

También hay un plugin para jquery para determinar la ubicación del elemento y las compensaciones

Http://plugins.jquery.com/project/dimensions

Desplazamiento = offsetHeight propiedad de un elemento

 6
Author: Joey V.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-08 14:16:47

JS puro

window.innerHeight
window.scrollY

Es más de 10 veces más rápido que jquery (y el código tiene un tamaño similar):

introduzca la descripción de la imagen aquí

Aquí puede realizar la prueba en su máquina: https://jsperf.com/window-height-width

 0
Author: Kamil Kiełczewski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-02 10:00:59