zoukankan      html  css  js  c++  java
  • 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时间中再执行一遍?这是因为有些人在浏览网页的时候会改变浏览器的大小的缘故,当浏览器的大小有变化的时候咱们带再次计算数值,然后进行调整,好了,完毕

  • 相关阅读:
    php-数据类型
    php常量
    php变量
    loadrunner11安装教程
    ThumbProcess.exe错误的解决方法
    jmeter 测试MD5加密登录接口
    jmeter 上传附件脚本报Non HTTP response code: java.io.FileNotFoundException
    jmeter中提取json串
    Jmeter 非 GUI 命令行执行脚本文件
    jmeter 报错Error in NonGUIDriver java.lang.IllegalArgumentException: Report generation requires csv output format, check 'jmeter.save.saveservice.output_format' property
  • 原文地址:https://www.cnblogs.com/gendan5/p/11454338.html
Copyright © 2011-2022 走看看