小编use*_*210的帖子

如何使用jQuery在指定的点停止浮动div

我试图阻止浮动(滑动)div到达包含div的底部,但它不起作用.变量bottom是包含div的页面上的最低点,但由于某种原因不能正常运行.谁有更好的方法?

$(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');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

6
推荐指数
1
解决办法
1185
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1