Si usted está buscando para desencadenar un evento cuando un especificado, o blanco, elemento desplaza a la vista, se puede hacer esto mediante la determinación de los valores siguientes:
- la altura de la ventana
- la valor de la distancia de desplazamiento desde la parte superior de la ventana
- el especificado, o de destino, la distancia a elemento de la parte superior de la ventana
considerar lo siguiente:
jQuery(window).on('scroll', function(){
var elValFromTop = Math.ceil(jQuery('.target-el').offset().top),
windowHeight = jQuery(window).height(),
windowScrollValFromTop = jQuery(this).scrollTop();
// if the sum of the window height and scroll distance from the top is greater than the target element's distance from the top, it should be in view and the event should fire, otherwise reverse any previously applied methods
if ((windowHeight + windowScrollValFromTop) > elValFromTop) {
console.log('element is in view!');
jQuery('.target-el').addClass('el-is-visible');
} else {
jQuery('.target-el').removeClass('el-is-visible');
}
});
Fragmento de código de demostración:
La continuación fragmento demuestra un ejemplo práctico de un tema común: la fijación de un elemento a la vista cuando se desplaza por los elementos especificados en la visión.
/* Sticky element on-scroll */
jQuery(window).on('scroll', function(){
var elValFromTop = Math.ceil(jQuery('.target-el').offset().top),
elHeight = jQuery('.target-el').outerHeight(),
windowHeight = jQuery(window).height(),
windowScrollValFromTop = jQuery(this).scrollTop(),
headerHeightOffset = jQuery('.fixed-header').outerHeight();
if ((windowHeight + windowScrollValFromTop) > elValFromTop) {
console.log('element is in view!');
console.log(headerHeightOffset);
jQuery('.will-change-el').addClass('fixed-el').css('top',headerHeightOffset).text('fixed');
} else {
jQuery('.will-change-el').removeClass('fixed-el').css('top','0').text('static');
}
// using template literals for multi-line output formatting
// comment out to observe when target element comes into view
console.log(`
how far is the target element from the top of the window: ${elValFromTop}
what is the target element's height: ${elHeight}
what is the window height: ${windowHeight}
what is the window's scroll value distance from the top: ${windowScrollValFromTop}
what is the sum of the window height and its offset value from the top: ${windowHeight + windowScrollValFromTop}
`);
});
/* || Reset & Defaults */
body {
margin: 0;
}
* {
box-sizing: border-box;
font-family: arial;
}
/* || General */
.target-el {
border-top: 1px solid #ccc;
}
.target-el,
.will-change-el {
transition: .7s;
text-align: center;
background: #ededed;
border-bottom: 1px solid #ccc;
}
.fixed-el {
position: fixed;
left: 0;
right: 0;
top: 0;
}
.fixed-header {
position: fixed;
left: 0;
right: 0;
top: 0;
height: 100px;
text-align: center;
background: #ededed;
border-bottom: 1px solid #ccc;
z-index: 9;
}
.buffer-el {
height: 1000px;
padding-top: 100px;
background: whitesmoke;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fixed-header">
<h1>Fixed Header</h1>
</div>
<div class="buffer-el">
<div class="will-change-el">static</div>
</div>
<div class="target-el">the target element</div>
recinto de seguridad Ejemplo:CodePen
NOTA:
Esto sólo se aplicará a desplazarse acciones sobre el documento y no a los elementos anidados contenidos dentro de un panel de vista de desplazamiento .
duplicado? http://stackoverflow.com/questions/487073/jquery-check-if-element-is-visible-after-scroling – awshepard
Posiblemente otro duplicado: http://stackoverflow.com/questions/1225102/jquery-event-to- trigger-action-when-a-div-is-made-visible –