• 使用scrollIntoView方法解决抛锚定位时网页往上跳的问题 2016.10.17


    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
      <head>
        <title>nav测试</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style type="text/css">
          *{margin:0; padding:0}
          body{width:960px; height:2000px; margin:0 auto; border: 1px dotted #432432;}
          ul,li{list-style-type: none;}
          a{text-decoration: none;}
          .nav{border:1px solid #000; 
             height:30px; 
             z-index:9999; 
            position:fixed ; 
            top:0px;
            _position:absolute;
            _top:expression(documentElement.scrollTop + "px");
          }
          .nav ul li{
            float:left;
            font-size: 16px;
            line-height: 30px;
            padding:0px 63px;
          }
          .nav ul li:hover{
            background: #23ded3;
          }
          #main{
            height:1000px; 
            border:1px solid #f00;
            margin-top:30px;
          }
          #div1{
            height:400px;
            border: 1px solid #ccc;
          }
           #div2{
            height:400px;
            border: 1px solid #ccc;
          }
           #div3{
            height:400px;
            border: 1px solid #ccc;
          }
        </style>
      </head>
      <body>
        <div id="headr">
          <div class="nav">
            <ul>
              <li><a>首页</a></li>
              <li><a onclick="onc()">你好</a></li>
              <li><a>很好</a></li>
              <li><a>他好</a></li>
              <li><a>真的</a></li>
              <li><a>哦哦</a></li>
            </ul>
          </div>
        </div>
        <div id ="main" style="960px; height: auto;">
          <div id="div1">
            <p>我是div1的内容</p>
          </div>
          <div id="div2">
            <p>我是div2的内容</p>
          </div>
          <div id="div3">
            <p>我是div3的内容</p>
          </div>
        </div>
        <div id ="footr"></div>
      </body>
      <script type="text/javascript">
        var dHeight = document.documentElement.clientHeight;
        var div1 = document.getElementById("div1");
        var div2 = document.getElementById("div2");
        var div3 = document.getElementById("div3");
        div1.style.height = dHeight - 30 + "px";        //通过一个js动态的来确定每个div的高度,还可以通过循环来实现,这里就不加了,各位自己可尝试
        div2.style.height = dHeight -30 + "px";
        div3.style.height = dHeight -30 + "px";
          var li = document.getElementsByTagName("li");
          li[0].onclick = function(){
            div1.scrollIntoView(false);          //这里不能给true不然会将会与导航条重叠
          }
          li[1].onclick = function(){
            div2.scrollIntoView(false);
          }
          li[2].onclick = function(){
            div3.scrollIntoView(false);
          }
      </script>
    </html>

     摘自:http://www.tuicool.com/articles/j6zuIbn

  • 相关阅读:
    百度统计图标消失了?
    C#中的变量类型var
    弗恩15693桌面读写器在win8下的测试
    JSON格式标准:http://www.ietf.org/rfc/rfc4627.txt?number=4627
    PHP中文乱码(转自百度文库)(记住:不管怎么样,用navicat创建数据库时选择utf8编码.)
    html2pdf:生成pdf应用
    win2003系统下apache、php、mysql安装以及虚拟主机和目录权限设置(转载)
    (转载)java环境变量配置
    (转)smarty里使用php函数
    (转载)php 知道某年中第几天计算出日期年月日
  • 原文地址:https://www.cnblogs.com/relstart/p/5971205.html
Copyright © 2020-2023  润新知