温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

div固定显示的几种方法

发布时间:2020-07-07 20:46:53 来源:网络 阅读:280 作者:Gendan5 栏目:web开发

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

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>

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

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

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

html代码如下

<div class="center">
<div class="centerf">跟单www.gendan5.com</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>

四、还有超过一定位置之后div置底
Html代码:

<div class="center">
<div class="centerf">跟单www.gendan5.com</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>
看到代码很多人都会有一个疑问,为什么scroll和resize时间中再执行一遍?这是因为有些人在浏览网页的时候会改变浏览器的大小的缘故,当浏览器的大小有变化的时候咱们带再次计算数值,然后进行调整,好了,完毕

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI