div移动到某一个地方就固定在那,怎么做啊

本文介绍如何在网页滚动时,使<div>元素在达到特定位置后固定在那个位置。主要涉及CSS定位技巧,包括相对定位和固定定位的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多站长们,看到很多网站都有的效果,就是滚动页面时,当页面滚动到某一个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>

    </div>


如需转载,请注明来源:【魅网工作室

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值