慕课网的一个小课程,练习了一遍,不足之处,欢迎指正(照片在本地,大家可以着重看代码哈):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>爱淘气购物网-JQuery网页定位导航特效</title>
  <style>
  /*All tag*/
  html, body, div, ul, li, img, h1, h2 { margin: 0; padding: 0; }
  ul { list-style: none; }
  a { text-decoration: none; color: #333; }
  /*menu*/
  #menu { position: fixed; top: 100px; left: 50%; margin-left: 400px; }
  #menu > li > a { display: block; padding: 10px 15px; height: 30px; line-height: 30px; font-size: 14px; font-weight: bold; margin: 5px 0; }
  #menu > li > a:hover,
  #menu > li > a.current { color: #fff; background-color: #0088bb; }
  /*content*/
  #content { width: 800px; margin: 30px auto; }
  h1 { color: #0088bb; font-size: 30px; margin-bottom: 10px; }
  .item { border: 1px dotted #0088bb; padding: 20px; margin-bottom: 20px; }
  .item > h2 { font-size: 16px; padding-bottom: 5px; border-bottom: 2px solid #0088bb; margin-bottom: 10px; }
  .item-list > li { display: inline-block; margin-right: 10px; }
  .item-list > li > a > img { width: 230px; height: 230px; border: none; 
  /* IE6 hack*/
  * html, * html body { background-image: url(about:blank); background-attachment: fixed; }
  * html #menu { /*position: fixed*/ position: absolute; top: expression((e = document.documentElement.scrollTop "menu">
    <li><a class="current" href="#item1" rel="external nofollow" >1F&nbsp;男装</a></li>
    <li><a href="#item2" rel="external nofollow" >2F&nbsp;女装</a></li>
    <li><a href="#item3" rel="external nofollow" >3F&nbsp;美妆</a></li>
    <li><a href="#item4" rel="external nofollow" >4F&nbsp;数码</a></li>
    <li><a href="#item5" rel="external nofollow" >5F&nbsp;母婴</a></li>
  </ul>
  <div id="content">
    <h1>爱淘气购物网</h1>
    <div id="item1" class="item">
      <h2>1F&nbsp;男装</h2>
      <ul class="item-list">
        <li>
          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/1F.jpg">

 下面再看下JS代码实现:

<script>
  //定义getByClassName函数,让函数实现根据className获取对象并返回
  function getByClassName(obj, cls) {
    var elements = obj.getElementsByTagName("*");
    var result = [];
    for (var i = 0; i < elements.length; i++) {
      if (elements[i].className == cls) {
        result.push(elements[i]);
      }
    }
    return result;
  }
  //定义hasClass函数,确认对象是否包含对应的class
  function hasClass(obj, cls) {
    return obj.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
  }
  //定义removeClass函数,让函数实现删除对象的class 
  function removeClass(obj, cls) {
    if (hasClass(obj, cls)) {
      //remove
      var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
      obj.className = obj.className.replace(reg, "");
    }
  }
  //定义addClass函数,让函数实现给对象增加class 
  function addClass(obj, cls) {
    if (!hasClass(obj, cls)) {
      obj.className += " " + cls;
    }
  }
  //页面加载完毕执行以下函数
  window.onload = function() {
    //页面滚动时,执行以下函数
    window.onscroll = function() {
      var top = document.documentElement.scrollTop || document.body.scrollTop;
      var menus = document.getElementById("menu").getElementsByTagName("a");
      var items = getByClassName(document.getElementById("content"), "item");
      var currentId = "";
      for (var i = 0; i < items.length; i++) {
        var _item = items[i];
        var _itemTop = _item.offsetTop;
        if (top > _itemTop - 200) {
          currentId = _item.id;
        } else {
          break;
        }
      }
      //给正确的menu下的a元素class赋值current
      if (currentId) {
        for (var j = 0; j < menus.length; j++) {
          var _menu = menus[j];
          var _href = _menu.href.split("#");
          if (_href[_href.length - 1] != currentId) {
            removeClass(_menu, "current");
          } else {
            addClass(_menu, "current");
          }
        }
      }
    }
  }
  </script>

以上实例代码是小编给大家分享的JQuery和原生JavaScript实现网页定位导航特效,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之间网站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com