滚动距离大于到元素本身($("demo").offset().top)时,元素fixed到顶部,小于则恢复原来的状态

<!DOCTYPE html>  
    <html>  
    <head>  
    <meta charset="utf-8">  
    <title>demo(滚到top2的位置时元素fixed)</title>  
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>  
    <style type="text/css">  
    *{margin: 0;padding: 0;border: 0;}  
    .sd{
      position: fixed;top: 0;left: 0;z-index: 999;
    }
    .as{
      visibility: hidden;height: 100px;
    }
    </style>  
    </head>  
    <body>  
    <div style="width: 100px;height: 100px;background-color: #ccc;text-align: center;font-size: 24px;position: fixed;top: 2%;right: 1%;z-index: 9999;">scroll: <p></p></div>   
    <div class="divtop" style="width: 1349px;height: 1000px;background: #666;">top1</div>  
      <a class="top" href="https://www.baidu.com" target="_blank" >scrollTop</a> 
      <div class="divtop3"></div>  
    <div class="divtop2" style="width: 1349px;height: 100px;background: #777;">top2</div> 
    
      <a class="scrollTarget" href="https://www.baidu.com" target="_blank" >scrollTarget</a>  
    <div style="width: 1349px;height: 100px;background: #888;">top3</div>  
      <a class="bottom" href="https://www.baidu.com" target="_blank" >scrollBottom</a>  
    <div style="width: 1349px;height: 1000px;background: #999;display:table-cell;    
        vertical-align:bottom;">bottom</div>  
    <script>  
    $(document).ready(function(){  
      
      $("a.top").click(function() {  
        $(window).scrollTop(0);  
        //或$("body,html").animate({"scrollTop":0},200);  
        return false;  
      });  
      
      $("a.scrollTarget").click(function(event) {  
        event.preventDefault();  
        $('html,body').stop().animate({scrollTop: $("a.scrollTarget").offset().top}, 200);  
        //return false;阻止href  
        //或者js  href="javascript:void(0)"   
        //或event.preventDefault();  
      });  
      
      function bottom() {$('html,body').animate({scrollTop: $(document).height()}, 200);return false;}  
      $("a.bottom").on("click",bottom);  
      
      $(window).scroll(function(){    
        var sh,ph,wh;  
        sh = $(window).scrollTop();    
        ph = $(".divtop3").offset().top;  
        wh = $(window).height();  
        $("p").text($(window).scrollTop()); 
        if (sh>ph) {
          //$(".divtop2").addClass("sd");
          $(".divtop3").addClass("as").next().addClass("sd");//添加一个class为as的类,使一个div顶住位置防止突变
        } else {
          //$(".divtop2").removeClass("sd");
          $(".divtop3").removeClass("as").next().removeClass("sd");
        } 
        if (sh+wh>ph) {  
          $(".divtop").css({"background-color":"red"});  
        } else {  
          $(".divtop").css({"background-color":"yellow"});  
        };  
      })  
    });  
      
    (function(){})();  
    $(function(){});  
    </script>  
      
    </body>  
    </html>  

没滚动

滚动到top2时情况。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、遇顶固定的例子 我一直以为是某个div或层随屏幕滚动,遇顶则固定,离开浏览器顶部又还原这样的例子其实不少,其实它的名字叫“层的智能浮动效果”。目前我们在国内的商业网站上就常常看到这样的效果了。例如淘宝网的搜索结果页的排序水平条,在默认状态,该工具条是跟随页面滚动的,如下图: 而当我们下拉滚动条,随着页面向下滚动,当此工具条接触到浏览器的上边缘,这就会独立出来固定在顶部,不跟随页面滚动滚动了,如下图: 类似的例子效果我们在别的网站上都有看到过,例如腾讯微博首页上,当我们下拉屏幕浏览最新微博,工具条也会出现这个效果,如下图: 这个效果看上去方便,贴心,也许还带推动广告的促销呢。原理其实很简单,本文展示两种方法。 二、智能浮动效果的实现原理 它分两种状态,一是默认状态,二是浮动固顶状态。 默认状态就是默认状态,什么也不用做,保持原有的CSS就好。无论有没有对它做定位,做了absolute也好,没做也好,都行。 关键是当浏览器屏幕滚动,该对象div层要移除浏览器界面视区的候,是要修改它的position属性,让它浮动在窗口的上沿显示就行了。最好的position属性是fixed,可以在IE6+和其他浏览器浮动层平滑的固定定位,由于IE6老大哥不支持fixed属性,所以可以另外赋予它absolute属性。当然也会产生副作用——滚动不平滑。不过也无所谓了,在微软公布不再修补更新XP漏洞,在中国360安全卫士的帮助下虽然大家仍然使用XP,但IE的用户已经是少之又少了。 那接下来,如何判断当前div层与浏览器窗口的上边缘接触了呢?遇到浏览器顶部了呢? 当浮动层div与浏览器窗口上边缘接触的一瞬间,其页面垂直偏移值与页面的滚动高度是一致的,所以我们就用这个进行判断。那如何获得页面上元素距离页面的垂直距离呢? 这里则是使用javaScript库实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值