zoukankan      html  css  js  c++  java
  • bootstrap滚动监视原理实现

    最近在公司实习,刚好写了一个静态的网页,用到了滚动监视,就自己写了个监视,话不多说直接进入正题

      1 $(function () {
      2     var $root = $("html,body");
      3     var location = [];//存储item active属性状态 true表示有class="active"
      4     location["position_1"] = false;
      5     location["position_2"] = false;
      6     location["position_3"] = false;
      7     location["position_4"] = false;
      8     location["position_5"] = false;
      9     location["position_6"] = false;
     10     location["position_7"] = false;
     11     var offset1,offset2,offset3,
     12         offset4,offset5,offset6,offset7; //各个锚点的距离顶部的偏移
     13     offset1 = $("[name = position_1]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     14     offset2 = $("[name = position_2]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     15     offset3 = $("[name = position_3]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     16     offset4 = $("[name = position_4]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     17     offset5 = $("[name = position_5]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     18     offset6 = $("[name = position_6]").offset().top - 2 * parseInt($(".header_top").css("height").toString().split("px")[0]);
     19     offset7 = offset6 + parseInt($("#section_6").css("height").toString().split("px")[0]) / 2;//最后部分高度不够,不能按部就班,否则滚动条到不到指定位置就倒底了
     20     function initi () {
     21         for(var i in location)
     22             location[i] = false;
     23     }
     24     // debugger;
     25     $(window).scroll(function () {
     26         // debugger;
     27         var Scroll = $(document).scrollTop();
     28         $("[class = 'active']").removeAttr("class");
     29         if(Scroll < offset2 && Scroll >= offset1) {
     30             initi();
     31             $("[href =#position_1]").parent().attr("class", "active");
     32             location["position_1"] = true;
     33         }
     34         else if(Scroll < offset3 && Scroll >= offset2) {
     35             initi();
     36             $("[href =#position_2]").parent().attr("class","active");
     37             location["position_2"] = true;
     38         }
     39         else if(Scroll < offset4 && Scroll >= offset3){
     40             initi();
     41             $("[href =#position_3]").parent().attr("class","active");
     42             location["position_3"] = true;
     43         }
     44         else if(Scroll < offset5 && Scroll >= offset4) {
     45             initi();
     46             $("[href =#position_4]").parent().attr("class", "active");
     47             location["position_4"] = true;
     48         }
     49         else if(Scroll < offset6 && Scroll >= offset5) {
     50             initi();
     51             $("[href =#position_5]").parent().attr("class", "active");
     52             location["position_5"] = true;
     53         }
     54         else if(Scroll < offset7 && Scroll >= offset6){
     55             initi();
     56             $("[href =#position_6]").parent().attr("class", "active");
     57             location["position_6"] = true;
     58         }
     59         else
     60         {
     61             initi();
     62             $("[href =#position_7]").parent().attr("class", "active");
     63             location["position_7"] = true;
     64         }
     65     });
     66     $("a").on("click",function (e) {
     67         var event = e || window.event;
     68         var son = $(event.target);
     69         var father = son.parent();
     70         var Name = father.prop("tagName");
     71         if(Name == "LI") {
     72             var act = $("[class ='active']").children("a").attr("href").toString().split("#")[1];
     73             location[act] = false;
     74             $("[class ='active']").removeAttr("class");
     75             var pos = son.attr("href").toString().split("#")[1];
     76             location[pos] = true;
     77             $root.animate({
     78                 scrollTop: $("[name="" + pos + ""]").offset().top
     79             }, 1000);
     80             father.attr("class", "active");
     81             return false;
     82         }
     83         else if(Name == "A")
     84         {
     85             var flag = false,pri,real,next,total = 0;
     86             var temp,num_next = 0,num_pri = 0;
     87             for(var i in location)
     88             {
     89                 if(location[i])
     90                 {
     91                     flag = true;
     92                     break;
     93                 }
     94             }
     95             if(flag == false) {
     96                 location["position_1"] = true;
     97                 window.location.href = "#position_1";
     98             }
     99             for (i in location) {
    100                 total++;
    101                 if (location[i]) {
    102                     temp = i.split("_");
    103                     num_next = parseInt(temp[1]) + 1;
    104                     num_pri = parseInt(temp[1]) - 1;
    105                     if(total == 1)
    106                     {
    107                         pri = real = i;
    108                         next = temp[0] + "_" + num_next;
    109                     }
    110                     else if(total == 7)
    111                     {
    112                         next = real = i;
    113                         pri = temp[0] + "_" + num_pri;
    114                     }
    115                     else
    116                     {
    117                         next = temp[0] + "_" + num_next;
    118                         real = i;
    119                         pri = temp[0] + "_" + num_pri;
    120                     }
    121                     break;
    122                 }
    123             }
    124             if(son.hasClass("prev"))
    125             {
    126                 location[real] = false;
    127                 location[pri] = true;
    128                 $root.animate({
    129                     scrollTop: $("[name="" + pri + ""]").offset().top
    130                 }, 1000);
    131                 $("[class ='active']").removeAttr("class");
    132                 $("[href="#" + pri + ""]").parent().attr("class","active");
    133                 return false;
    134             }
    135             else if(son.hasClass("next"))
    136             {
    137                 location[real] = false;
    138                 location[next] = true;
    139                 $root.animate({
    140                     scrollTop: $("[name="" + next + ""]").offset().top
    141                 }, 1000);
    142                 $("[class='active']").removeAttr("class");
    143                 $("[href="#" + next + ""]").parent().attr("class","active");
    144                 return false;
    145             }
    146             else
    147                 return false;
    148 
    149         }
    150     });
    151 });

    在实习的时候做页面出现的几个问题

    1.图片自适应浏览器窗口完全显示

    •     查看图片的长宽 设置所在图片div的padding-top为高/宽 比如,一张图片是 2000*1333 那么设置所在div padding-top:66.65%;
    •     在div里面放空的block标签强行撑开 然后在div中设置background: url("../picture/bg.jpg") no-repeat;background-size: cover;

    2.滚动监视的时候发生的锚点偏移问题,过了锚点,还没触发了事件

      这种情况是因为头部设置了一个固定位置的div,从而导致这个div就不占空间了,他下面的元素就填充他的位置

    解决不占空间的方法:1.设置margin-top 2.设置一个空div

    解决锚点偏移的方法:设置暗锚来填充形成的固定位置的div

    1 <div id="section_2" class="container">
    2         <a name="position_2" class="target-fix"></a>
    3         <h1>信息15-1</h1><br/><br/><br/>
    4         <p>To be or not to be,that's a question.<br/><br/>
    5            决心不过是记忆的奴隶 它会根据你的记忆随意更改(When sorrows come, they come not single spies, - but in battalions.)<br/><br/>
    6         </p>
    7     </div>
    1 .target-fix
    2 {
    3     display: block;
    4     position: relative;
    5     top: -65px;
    6 }

    比如说我的固定位置的div高度是65px,那么我这个暗锚就上移65px,然后我就只用跳转到这个锚点位置就行<li><a href="#position_2">信息15-1</a></li>

    原理:虽然元素被fixed在最上面了,可是页面计算section_2的页边距报读的时候还是把fixed的高度算上去了,所以点击锚点链接的时候,偏移的距离是section_2页边距的高度加上fixed的高度( section_2.offset().top + nav.height)所以设置暗锚向上偏移65px;

    因为写的是静态页面 写的很随意把页面写死了 css文件里出现的多余的样式是因为我直接把实习的工程文件截取抽过来,所以代码就很难看了~~~~~~想要看看效果的就自行查看百度云链接:https://pan.baidu.com/s/1kWDK8rP 密码:6de2 

  • 相关阅读:
    [转]对Lucene PhraseQuery的slop的理解
    Best jQuery Plugins of 2010
    15 jQuery Plugins To Create A User Friendly Tooltip
    Lucene:基于Java的全文检索引擎简介
    9 Powerful jQuery File Upload Plugins
    Coding Best Practices Using DateTime in the .NET Framework
    Best Image Croppers ready to use for web developers
    28 jQuery Zoom Plugins Creating Stunning Image Effect
    VS2005 + VSS2005 实现团队开发、源代码管理、版本控制(转)
    禁止状态栏显示超链
  • 原文地址:https://www.cnblogs.com/cc-xiao5/p/8317534.html
Copyright © 2011-2022 走看看