Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 定位固定相关父容器_Javascript_Css - Fatal编程技术网

Javascript 定位固定相关父容器

Javascript 定位固定相关父容器,javascript,css,Javascript,Css,我需要解决方案来模拟固定位置,但相对父div,而不是整个视口。JS解决方案是滞后的。我需要固定的相关父容器,因为若窗口的高度很小,固定位置的div将进入页脚区域 那么这不是位置问题:修复了,也许你可以定义你身体的最小高度(或者在主包装上,如果有的话),以避免短页问题另一种更新方法 尝试给出固定的divz-index:10 页脚div位置:相对;z指数:11 这将使页脚与固定分区重叠。我将css和js组合在一起: $(document).ready(function () {

我需要解决方案来模拟固定位置,但相对父div,而不是整个视口。JS解决方案是滞后的。我需要固定的相关父容器,因为若窗口的高度很小,固定位置的div将进入页脚区域


那么这不是位置问题:修复了,也许你可以定义你身体的最小高度(或者在主包装上,如果有的话),以避免短页问题

另一种更新方法

尝试给出固定的
div
z-index:10

页脚div
位置:相对;z指数:11


这将使页脚与固定分区重叠。

我将css和js组合在一起:

$(document).ready(function () {
                var $sidebar = $(".register-box"),
                $window = $(window),
                $content = $("#content"),
                docHeight = $(document).height();
                var entered = false;

                $window.scroll(function () {
                    if ($window.height() < 795 && docHeight - $window.scrollTop() < 785) {
                        entered = true;
                        var pos = $sidebar.offset();
                        $sidebar.css('position', 'absolute').css('top', ($content.height() - ($sidebar.height() + 40)) + 'px');
                    }
                    else {
                        if (entered) {
                            entered = false;
                            $sidebar.css({
                                top: "",
                                left: "",
                                position: "fixed"
                            });
                        }
                    }
                });
            });
$(文档).ready(函数(){
变量$sidebar=$(“.register box”),
$window=$(window),
$content=$(“#content”),
docHeight=$(document).height();
输入的var=假;
$window.scroll(函数(){
如果($window.height()<795&&docHeight-$window.scrollTop()<785){
输入=真;
var pos=$sidebar.offset();
$sidebar.css('position','absolute').css('top',($content.height()-($sidebar.height()+40))+'px');
}
否则{
如果(输入){
输入=假;
$sidebar.css({
顶部:“,
左:“,
位置:“固定”
});
}
}
});
});

代码不是最终的,数字是硬编码的,但它可以工作,足够平滑。

我添加了示例,这是一个很好的解决方案,但我想停止在页脚附近滚动小部件。@INs没有Javascript绝对不可能。也许你知道平滑滚动的好JS解决方案,作为固定位置?jquery插件scrollfollow很落后。@INs scrollfollow将是我所知道的唯一推荐,对不起!