div固定显示的几种方法

浏览: 2,168次 日期:2018年07月17日 21:01:11 作者:青格勒

很多时候我们会受到一些需求:

1、div一直置顶

2、div一直置底

3、超过一定的位置之后div置顶

4、超过一定位置之后div置底

 

那么下面针对上面的几个问题写几个案例:

一、div一直在屏幕的上方,这个倒是容易咱们直接使用position:fixed;然后设置他的top值和left就可以了,别忘了设置宽度哦

<div class="top">
    <div class="topf">青格勒</div>
</div>
<style>
    .top,.topf{ height:100px; width:100%;}
    .topf{ position:fixed; top:0; left:0; background:#999; text-align:center; font-size:20px; color:#fff;}
</style>

点击这里查看demo -》

 

二、这个跟上面的例子是一样的,我不不多说了

<div class="bottom">
    <div class="bottomf">青格勒</div>
</div>
<style>
    .bottom,.bottomf{ height:100px; width:100%;}
    .bottomf{ position:fixed; bottom:0; left:0; z-index:12; background:#999; text-align:center; font-size:20px; color:#fff;}
</style>

点击这里查看demo -》

 

三、这个就比较有意思了,有些时候咱们的导航在banner的下方

如下图:

这时候咱们的需求就出来了,当咱们的滚动条走到banner图的底部的时候需要把nav的部分悬挂(position:fixed; top:0);

这时候咱们就得计算了,先获取nav到document顶部的距离,然后在获取滚动条的长度,相减就能得到window的顶部的距离,当两者的相减<=0的时候悬挂。

html代码如下

<div class="center">
    <div class="centerf">青格勒2132132</div>
</div>

 

CSS代码如下:

<style>
    .center{ position:relative; z-index:12;}
    .center,.centerf{ height:100px; width:100%;}
    .centerf{ background:#666; text-align:center; font-size:20px; color:#fff;}
    .on{ position:fixed; top:0; left:0; z-index:12;}
    .onm{ position:fixed; bottom:0; left:0; z-index:12;}
</style>

 

JS代码如下:

<script type="text/javascript">
    $(function () {
        function divtop(){
            var boxTop = $('.center').offset().top;
            var scrTop = $('body,html').scrollTop();
            //头部定位
            if ((boxTop - scrTop) < 0){
                if ($('.centerf').hasClass('on')){

                }else{
                    $('.centerf').addClass('on')
                }
            }else{
                if ($('.centerf').hasClass('on')){
                    $('.centerf').removeClass('on')
                }else{

                }
            };
        };
        divtop();
        $(window).scroll(function () {
            divtop();
        });
        $(window).resize(function(){
            divtop();
        });
    });
</script>

点击这里查看demo -》

 

 

四、还有超过一定位置之后div置底

 

Html代码:

<div class="center">
    <div class="centerf">青格勒2132132</div>
</div>

 

CSS代码:

<style>
    .center{ position:relative; z-index:12;}
    .center,.centerf{ height:100px; width:100%;}
    .centerf{ background:#666; text-align:center; font-size:20px; color:#fff;}
    .onm{ position:fixed; bottom:0; left:0; z-index:12;}
</style>

 

JS代码:

<script type="text/javascript">
    $(function () {
        function divbottm(){
            var boxTop = $('.center').offset().top;
            var scrTop = $('body,html').scrollTop();
            var winHei = $(window).height();
            //头部定位
            if((boxTop - scrTop - winHei + 100) < 0){
                if ($('.centerf').hasClass('onm')){

                }else{
                    $('.centerf').addClass('onm')
                }
            }else{
                if ($('.centerf').hasClass('onm')){
                    $('.centerf').removeClass('onm')
                }else{

                }
            }
        };
        divbottm();
        $(window).scroll(function () {
            divbottm();
        });
        $(window).resize(function(){
            divbottm();
        })
    });
</script>

点击这里查看demo -》

 

看到代码很多人都会有一个疑问,为什么scroll和resize时间中再执行一遍?这是因为有些人在浏览网页的时候会改变浏览器的大小的缘故,当浏览器的大小有变化的时候咱们带再次计算数值,然后进行调整,好了,完毕

 

文章链接:http://cenggel.com/js/133.html

版权声明:文章《div固定显示的几种方法》由青格勒编写,转载请带上文章链接。

本章内容纯属乱讲,如有雷同,纯属巧合。如有借鉴之处已表明出处。

点击任意位置关闭窗口,感谢您的支持。


打赏规则 - 为了避免对于打赏模式产生误解,说明一下几点内容:

  • 1.打赏纯粹自愿,金额不限,别太多,1块就行;
  • 2.打赏不能作为解答疑问的理由;
  • 3.打赏不能帮你做毕业设计或论文;
  • 4.打赏不能帮你做工作中的项目;
  • 5.打赏就是这不能那不能,只是单纯打赏而已。
  • 5.最后谢谢支持。
关闭
相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注