很多站长们,看到很多网站都有的效果,就是滚动页面时,当页面滚动到某一个DIV时,DIV就固定在页面顶部!这种效果其实很简单的
我这是以我的网站导航为例,所以代码有点多,自己可以自行修改,效果的主要代码是js部分
效果可以查看:魅网工作室--仿站教程 http://cqmeiwang.com/plus/list.php?tid=2
代码附下:
$(function (){
var ie6 = document.all;
//$('填写ID或者类')
var dv = $('.logo_daohang'), st;
dv.attr('otop', dv.offset().top);
$(window).scroll(function () {
st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
if (st > parseInt(dv.attr('otop'))) {
if (ie6) {//relative
dv.css({ position: 'absolute', top: st });
}
else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 });
} else if (dv.css('position') != 'static') dv.css({ 'position': 'static' });
});
});
<style type="text/css">
.logo_daohang{ width:1213px; height:84px; margin:0 auto; background:#fff; border-bottom:1px solid #dedede;}
.logo_daohang_logo{ width:30%; float:left;}
.logo_daohang_daohang{ width:59%; float:right; height:84px;}
.logo_daohang_daohang ul{ width:696px;margin:auto auto auto 25px; }
.logo_daohang_daohang ul li{ text-align:center; height:45px; float:left; list-style:none; width:110px; margin-top:3%;}
.logo_daohang_daohang a{ color:#333333; font-size:14px; font-weight:bolder; line-height:45px; width:110px; height:45px; display:block;}
.logo_daohang_daohang a:hover{ text-decoration:none; color:#ffffff; background:#f5a100; border-radius:5px;}
.logo_daohang_daohang a:active{ background:#f5a100; border-radius:5px; text-decoration:none; color:#ffffff;}
</style>
<div class="logo_daohang">
<div class="logo_daohang_logo">
<img src="{dede:global.cfg_templets_skin/}/images/logo.png" alt="魅网工作室,网站建设,网页设计" />
</div>
<div class="logo_daohang_daohang">
<ul>
<li><a href="/">网站首页</a></li>
<li><a href="/fzjc">仿站教程</a></li>
<li><a href="/alzs">案例展示</a></li>
<li><a href="/tesefuwu">特色服务</a></li>
<li><a href="/yingxiaoyutuiguang">营销与推广</a></li>
<li><a href="/gywm">关于我们</a></li>
</ul>
</div>
我这是以我的网站导航为例,所以代码有点多,自己可以自行修改,效果的主要代码是js部分
效果可以查看:魅网工作室--仿站教程 http://cqmeiwang.com/plus/list.php?tid=2
代码附下:
$(function (){
var ie6 = document.all;
//$('填写ID或者类')
var dv = $('.logo_daohang'), st;
dv.attr('otop', dv.offset().top);
$(window).scroll(function () {
st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
if (st > parseInt(dv.attr('otop'))) {
if (ie6) {//relative
dv.css({ position: 'absolute', top: st });
}
else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 });
} else if (dv.css('position') != 'static') dv.css({ 'position': 'static' });
});
});
<style type="text/css">
.logo_daohang{ width:1213px; height:84px; margin:0 auto; background:#fff; border-bottom:1px solid #dedede;}
.logo_daohang_logo{ width:30%; float:left;}
.logo_daohang_daohang{ width:59%; float:right; height:84px;}
.logo_daohang_daohang ul{ width:696px;margin:auto auto auto 25px; }
.logo_daohang_daohang ul li{ text-align:center; height:45px; float:left; list-style:none; width:110px; margin-top:3%;}
.logo_daohang_daohang a{ color:#333333; font-size:14px; font-weight:bolder; line-height:45px; width:110px; height:45px; display:block;}
.logo_daohang_daohang a:hover{ text-decoration:none; color:#ffffff; background:#f5a100; border-radius:5px;}
.logo_daohang_daohang a:active{ background:#f5a100; border-radius:5px; text-decoration:none; color:#ffffff;}
</style>
<div class="logo_daohang">
<div class="logo_daohang_logo">
<img src="{dede:global.cfg_templets_skin/}/images/logo.png" alt="魅网工作室,网站建设,网页设计" />
</div>
<div class="logo_daohang_daohang">
<ul>
<li><a href="/">网站首页</a></li>
<li><a href="/fzjc">仿站教程</a></li>
<li><a href="/alzs">案例展示</a></li>
<li><a href="/tesefuwu">特色服务</a></li>
<li><a href="/yingxiaoyutuiguang">营销与推广</a></li>
<li><a href="/gywm">关于我们</a></li>
</ul>
</div>
</div>
如需转载,请注明来源:【魅网工作室】