很多時(shí)候我們會(huì)受到一些需求:
創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),先為尉犁等服務(wù)建站,尉犁等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為尉犁企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
1、div一直置頂
2、div一直置底
3、超過(guò)一定的位置之后div置頂
4、超過(guò)一定位置之后div置底
那么下面針對(duì)上面的幾個(gè)問(wèn)題寫幾個(gè)案例:
一、div一直在屏幕的上方,這個(gè)倒是容易咱們直接使用position:fixed;然后設(shè)置他的top值和left就可以了,別忘了設(shè)置寬度哦
<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>
點(diǎn)擊這里查看demo -》
二、這個(gè)跟上面的例子是一樣的,我不不多說(shuō)了
<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>
三、這個(gè)就比較有意思了,有些時(shí)候咱們的導(dǎo)航在banner的下方
如下圖:
這時(shí)候咱們的需求就出來(lái)了,當(dāng)咱們的滾動(dòng)條走到banner圖的底部的時(shí)候需要把nav的部分懸掛(position:fixed; top:0);
這時(shí)候咱們就得計(jì)算了,先獲取nav到document頂部的距離,然后在獲取滾動(dòng)條的長(zhǎng)度,相減就能得到window的頂部的距離,當(dāng)兩者的相減<=0的時(shí)候懸掛。
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>
四、還有超過(guò)一定位置之后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>
看到代碼很多人都會(huì)有一個(gè)疑問(wèn),為什么scroll和resize時(shí)間中再執(zhí)行一遍?這是因?yàn)橛行┤嗽跒g覽網(wǎng)頁(yè)的時(shí)候會(huì)改變?yōu)g覽器的大小的緣故,當(dāng)瀏覽器的大小有變化的時(shí)候咱們帶再次計(jì)算數(shù)值,然后進(jìn)行調(diào)整,好了,完畢
當(dāng)前名稱:div固定顯示的幾種方法
本文路徑:http://aaarwkj.com/article20/igscco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、小程序開發(fā)、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)