javascript - 上下滚动时在一个 div 中 float 一个 div(不是在整个屏幕上)

标签 javascript jquery css html

我在div中制作了一个div,我希望内部div在外部div中上下滚动页面上下 float 。我以某种方式设法让它限制不超出外部 div 形式,但是当它到达底部时,它会下降到页面底部。请帮助我,这是我的代码 CSS

CSS

#comment {
  position: absolute;
  /* just used to show how to include the margin in the effect */
}

HTML

<!--the outer div in which i have whole content -->
<div class="content">
    <!--the floating div, remains well inside form top but moves down outside div from bottom -->
    <div class="ftwrapper" id="comment">            
    </div><!--fb/twitter ends-->
</div>

JQuery

    $(function () {
        var msie6 = $.browser == 'msie' && $.browser.version < 7;
        if (!msie6) {
            var top = $('#comment').offset().top - parseFloat($('#comment').css('margin-top').replace(/auto/, 0));
            $(window).scroll(function (event) {
                // what the y position of the scroll is
                var y = $(this).scrollTop();

                // whether that's below the form
                if (y >= top) {
                    // if so, ad the fixed class
                    $('#comment').addClass('fixed');
                } else {
                    // otherwise remove it
                    $('#comment').removeClass('fixed');
                }
            });
        }  
    });

最佳答案

我根据您的要求做了 sample 测试。 如果滚动太快,效果不佳,但其他情况下还可以。稍后我会对其进行一些更改。

var prevScroll = 0;
$(window).unbind("scroll");
function reposition() {
    var contPos  = $("#container").offset();
    var comment = $('#comment');    
    contPos.bottom = contPos.top + $("#container").outerHeight();
    console.log('contPos',contPos);
    $(window).scroll(function (event) {
        // what the y position of the scroll is
        var     scroll = $(window).scrollTop()
            ,   y = scroll
            ,   pos = comment.offset()
        ;
        pos.bottom = comment.outerHeight();
        if ( scroll > prevScroll) {
            //down
        } else {
            //up
        }
        prevScroll = scroll;
        // whether that's below the form
        console.log(pos.bottom + scroll ,":", contPos.bottom);
        if (contPos.top > scroll) {
            // if so, ad the fixed class
            comment.css({
                position: 'relative',
                bottom  : '0px',
                left    : '0px'
            });
            console.log("Too High");
        } else if ( pos.bottom + scroll > contPos.bottom) {
            //comment.removeClass('fixed');
            comment.css({
                position: 'absolute',
                top      : (contPos.bottom - comment.outerHeight() )+'px',
                left     : pos.left+'px'
            });

            console.log("Too Low");
        } else {
            // middle area
            console.log("Perfect");
            comment.css({
                position: 'fixed',
                top   : '0px',
                left  : pos.left + 'px'
            });
        }
    });
}
$(document).ready(reposition);

Jsfiddle test

关于javascript - 上下滚动时在一个 div 中 float 一个 div(不是在整个屏幕上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19753678/

相关文章:

javascript - Access-Control-Allow-Origin 不允许 http ://example. com

javascript - 获取标签之间的内容

javascript - AngularJS 工厂变量变得未定义

javascript - grunt 文件 glob 文件扩展名模式

jquery - 验证后输入图标消失

CSS3 rotate3d 错误

css - 放大后全宽切断

jquery - 将 Div 从 'fixed' 更改为 'absolute' 并将 Div 放置到当前浏览器窗口位置的顶部(而不是页面顶部)

javascript - 如何按javascript中的每个元素计算数组元素

javascript - 根据用户输入有条件地显示表单字段