0

我编写了一个脚本,在顶部栏中有一个幻灯片(品牌)和一个位于滑块(或横幅)上方和下方的菜单。当他们向后滚动时,它只是品牌和菜单......没有滑块或横幅。

它可以工作,但有一个我似乎无法修复的错误。

该页面有 4 个主要元素,但 SLIDER 或 BANNER 是选项元素(并不总是存在):

<div id="branding">BRANDING</div>
<div id="header">
    <div id="slider">SLIDER OR BANNER</div>
    <div id="menu">MENU</div>
</div>
<div id="content">CONTENT</div>

到目前为止,这是我的脚本:

     var sticky_navigation = function () {

         $lH = ($('#branding').length) ? $('#branding').height() : 0,
         $sH = ($('#slider').length) ? $('#slider').height() : 0,
         $bH = ($('#banner').length) ? $('#banner').height() : 0,
         $mH = ($('#menu').length) ? $('#menu').height() : 0;

         var $content = $('#content'), // Main content area
             $branding = $('#branding'),
             $header = $('#header'),
             $menu = $('#menu');

         // HEADER
         if ($(window).scrollTop() > $lH) {
             $header.css({
                 marginTop: $lH + "px"
             });
             if ($branding.css('position').toString() != "fixed") {
                 $branding.css({
                     position: "fixed",
                     top: "-" + $lH + "px",
                     left: 0,
                     zIndex: 500,
                 }).animate({
                     top: 0
                 }, 700);
             }
         } else {
             $branding.css({
                 position: "relative",
                 marginTop: "0px",
             });
             $header.css({
                 marginTop: "0px"
             });
         }

         // MENU
         if ($(window).scrollTop() > ($bH + $sH + $mH)) {
             $branding.css({
                 boxShadow: "none",
             });
             $header.css({
                 marginTop: ($lH + $mH) + "px"
             });
             if ($menu.css('position').toString() != "fixed") {
                 $menu.css({
                     position: "fixed",
                     top: "-" + $lH + "px",
                     left: 0,
                     zIndex: 490,
                 }).animate({
                     top: $lH
                 }, 700);
             }
         } else {
             $menu.css({
                 position: "relative",
                 marginTop: "0px",
                 top: 0,
             });
             if ($('#branding').length || $('#slider').length) {
                 $branding.css({
                     boxShadow: "0 0 16px rgba(0, 0, 0, 0.5)",
                 })
             }
         }
     };
     // run our function on load
     sticky_navigation();

     // and run it again every time you scroll
     $(window).scroll(function () {
         sticky_navigation();
     });

     // and run it again every time you resize
     $(window).resize(function () {
         sticky_navigation();
     });

这是一个 jsfiddle,因为我现在拥有它...查看错误快速上下滚动..您应该看到菜单低于应有的位置。

http://jsfiddle.net/hC423/1/

在此处输入图像描述

非常感谢您对此的任何帮助。如果有更好的方法可以做到这一点,我愿意接受建议。

C

4

1 回答 1

-1

http://jsfiddle.net/hC423/2/

用户滚动后添加了 250 毫秒的延迟。

如果 (!/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {

 var sticky_navigation = function () {

     setTimeout(function // ###### ADDED ######

     $lH = ($('#branding').length) ? $('#branding').height() : 0,
     $sH = ($('#slider').length) ? $('#slider').height() : 0,
     $bH = ($('#banner').length) ? $('#banner').height() : 0,
     $mH = ($('#menu').length) ? $('#menu').height() : 0;

     var $content = $('#content'), // Main content area
         $branding = $('#branding'),
         $header = $('#header'),
         $menu = $('#menu');

     // HEADER
     if ($(window).scrollTop() > $lH) {
         $header.css({
             marginTop: $lH + "px"
         });
         if ($branding.css('position').toString() != "fixed") {
             $branding.css({
                 position: "fixed",
                 top: "-" + $lH + "px",
                 left: 0,
                 zIndex: 500,
             }).animate({
                 top: 0
             }, 700);
         }
     } else {
         $branding.css({
             position: "relative",
             marginTop: "0px",
         });
         $header.css({
             marginTop: "0px"
         });
     }

     // MENU
     if ($(window).scrollTop() > ($bH + $sH + $mH)) {
         $branding.css({
             boxShadow: "none",
         });
         $header.css({
             marginTop: ($lH + $mH) + "px"
         });
         if ($menu.css('position').toString() != "fixed") {
             $menu.css({
                 position: "fixed",
                 top: "-" + $lH + "px",
                 left: 0,
                 zIndex: 490,
             }).animate({
                 top: $lH
             }, 700);
         }
     } else {
         $menu.css({
             position: "relative",
             marginTop: "0px",
             top: 0,
         });
         if ($('#branding').length || $('#slider').length) {
             $branding.css({
                 boxShadow: "0 0 16px rgba(0, 0, 0, 0.5)",
             })
         }
     }
   } ,250); // ###### ADDED ######
 };
 // run our function on load
 sticky_navigation();

 // and run it again every time you scroll
 $(window).scroll(function () {
     sticky_navigation();
 });

 // and run it again every time you resize
 $(window).resize(function () {
     sticky_navigation();
 });

};

于 2013-11-11T08:32:07.820 回答