Estoy intentando detener un div flotante (deslizante) cuando llega al final de un div que contiene pero no funciona. La variable bottom
es el punto más bajo en la página del div que contiene, pero por alguna razón no actúa como debería. Alguien tiene un mejor método?Cómo detener el div flotante en el punto especificado usando jQuery
$(document).ready(function() {
var top = $('#buttonsDiv').offset().top - parseFloat($('#buttonsDiv').css('margin-top').replace(/auto/, 0));
var bottom = $('#mainBody').offset().top + $('#mainBody').height();
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
var z = y + $('#buttonsDiv').height();
// whether that's below the form
if (y >= top && z <= bottom) {
// if so, add the fixed class
$('#buttonsDiv').addClass('fixed');
} else {
// otherwise remove it
$('#buttonsDiv').removeClass('fixed');
}
});
});
En su clase '.fixed', ¿ha dado' bottom: 0; '? – techfoobar
intenté agregarlo y todavía no funcionó. el problema fue encontrar una forma para que la función de desplazamiento reconociera la parte inferior del elemento flotante. Creo que he resuelto eso en el código anterior, pero lograr que se detenga en la parte inferior del div que lo contiene me desconcierta. – user1108210
¿Puede incluir su html y css y/o un ejemplo de trabajo? –