• div固定显示的几种方法


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

    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; 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; 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; 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;www.gendan5.com
    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; 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>

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

  • 相关阅读:
    android81 多线程下载和断电续传
    android80 HttpClient框架提交数据 get方式
    CentOS 7最小化安装后找不到‘ifconfig’命令——修复小提示
    Eclipse4.5在线安装Aptana插件及配置代码提示教程
    centos7 yum源
    ESXi License过期解决办法
    mysql grant 用户权限总结
    Unable to locate package错误解决办法
    华为USG6350防洪墙SNMP最简单功能配置
    ssl_error_weak_server_ephemeral_dh_key
  • 原文地址:https://www.cnblogs.com/gendan5/p/11454338.html
Copyright © 2020-2023  润新知