zoukankan      html  css  js  c++  java
  • 导航栏滚动

    在项目,遇到导航栏滚动,滚到到窗口顶部悬停;滚动导航栏的产品,对应的导航栏选中。

    html:

    <div id="nav" >

            <a class="xyifu"   data-id="yifu" @click="goscrol">衣服</a>

            <a class="xxiezi"   data-id="xiezi" @click="goscrol">鞋子</a>

            <a  class="xmaozidata-id="maozi" @click="goscrol">帽子</a>

    </div>

    <div id="yifu">衣服列表</div>

    <div id="xiezi">鞋子列表</div>

    <div id="maozi">帽子列表</div>

    js:

    //点击对应的导航栏,跳转到对应商品列表

    goscrol($event) {

          var id = $event.target.dataset.id;

          var $scrollHeight = $("#" + id).offset().top;

          $("html, body").animate(

            {

              scrollTop: $scrollHeight

            },

            500

          );

    },

    window.onload = function() {

          var arr = [

            { id: "yifu", height: $("#yifu").height() },

            { id: "xiezi", height: $("#xiezi").height() },

            { id: "maozi", height: $("#maozi").height() }

          ];

          var box = document.getElementById("nav");

          var topmargin = box.offsetTop;

      

      //不同设备,安卓,iphone

          var userAgent = navigator.userAgent.toLowerCase();

          var isIphone = userAgent.match(/iphone os/i) == "iphone os";

          var isAndroid = userAgent.match(/android/i) == 'android';

          $(window).on("scroll", function() {

        //滚动页面,让导航栏悬浮在窗口顶部

            var scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

        if(isIphone){

          scroll >= topmargin - 50 ? $("#nav").addClass("postplayIOS") : $("#nav").removeClass("postplayIOS");

        }else{

          scroll >= topmargin - 50 ? $("#nav").addClass("postplayAndroid") : $("#nav").removeClass("postplayAndroid");

        }

         //当滚动时,导航栏没到窗口顶部,移除选中样式

          if (document.getElementById(arr[0].id).offsetTop - $(window).scrollTop() > 90 ) {

                  $(".x" + arr[0].id).removeClass("redfont");

                }

        //当滚动到对应产品列表时,导航栏添加对应选中样式

             for (var i = 0; i < arr.length; i++) {

                var nowbox = document.getElementById(arr[i].id);

                if (nowbox.offsetTop - $(window).scrollTop() < 90) {

                  $(".x" + arr[i].id).addClass("redfont").siblings().removeClass("redfont");

                }

             }

          });

    };

    CSS:

    .postplayIOS {

      position: sticky;

      position: -webkit-sticky;

      left: 0;

      top: 0;

    }

    .postplayAndroid {

      position: fixed;

      left: 0;

      top: 0;

    }

    #nav .redfont {

      border-bottom: 2px solid #f9f100;

    }

  • 相关阅读:
    Java Web(三) 会话机制,Cookie和Session详解(转载)
    如何转载别人的文章
    C#操作Word的+ CKEditor 輸出成Word文件(包含圖案上傳)
    vs2013 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”。是否缺少程序集引用?
    CKEditor与CKFinder学习--自定义界面及按钮事件捕获
    JS获取CkEditor在线编辑的内容
    <%#Eval() %>的常用方法
    ASP.NET 中 ContentType 类型
    富文本编辑器 CKeditor 配置使用+上传图片
    VS2013 蛋疼的“AJAX Control Toolkit”安装过程
  • 原文地址:https://www.cnblogs.com/Super-scarlett/p/10000067.html
Copyright © 2011-2022 走看看