简体   繁体   English

修复了导航栏淡入/淡出问题

[英]Fixed Navigation Bar fade-in/out issues

I've been struggling for a few hours now trying all kind of things to get this working, but I've failed thus far to get it to work the way I want it to work. 我一直在努力几个小时,尝试各种事情来使它工作,但是到目前为止,我一直未能使它按照我希望的方式工作。

Basically what I want to accomplish is to have the fixedbar to fade if the scroll position is more than the position of the phonebox element and have it fade out if the position is less than the phonebox element's position. 基本上,我想完成的工作是,如果滚动位置大于phonebox元素的位置,则使固定栏消失,如果滚动位置大于phonebox元素的位置,则使固定栏消失。

I used ScrollTop to enter the amount of pixels from the top at which the fade in and fadeout should happen at, but that only seems to work for my own screen (my macbook for example has a different height and therefore messes up the location where the bar fades in and out). 我使用ScrollTop从顶部开始输入应该发生淡入和淡出的像素数量,但这似乎只对我自己的屏幕有效(例如,我的Macbook具有不同的高度,因此弄乱了条淡入和淡出)。

I was hoping to get some help on how to make the javascript use the location of the phonebox element instead of a fixed amount of pixels. 我希望获得一些有关如何使javascript使用phonebox元素的位置而不是固定数量的像素的帮助。

Thanks in advance! 提前致谢!

 $(document).ready(function() { $('#navigation a, #fixedbar a').on('click', function(e) { e.preventDefault(); }); $(window).on('scroll', function() { var scrolltop = $(this).scrollTop(); if (scrolltop >= 967) { $('#fixedbar').fadeIn(250); } else if (scrolltop <= 967) { $('#fixedbar').fadeOut(250); } }); }); 
  #fixedbar { display: none; position: fixed; top: 0; width: 100%; height: 80px; background: rgba(0, 0, 0, 0.75); } #fixednav { display: block; width: 710px; margin: 0 auto; padding: 0px 25px; } #fixednav li a { display: block; float: left; font-size: 1.75em; font-weight: bold; color: #96aed8; line-height: 80px; text-decoration: none; padding: 0px 8px; margin-right: 6px; -webkit-transition: all 0.2s linear; -moz-transition: all 0.2s linear; transition: all 0.2s linear; } #fixednav li a:hover { color: #fff; background: rgba(0, 0, 0, 0.3); } 
 <div id="fixedbar"></div> <section id="header"> <div class="inner"> <a id="slide1" href="#"></a> <div id="slide2"></div> <h1>text</h1> <p>text</p> <ul class="actions"> <li><a href="#three" class="button">text</a></li> </ul> </div> </section> <div class="phonebox"></div> 

Had help from a online gaming community friend, took about 2 hours but we figured it out. 在一个在线游戏社区朋友的帮助下,大约花了2个小时,但我们弄清楚了。 Below is the script that did the trick for me if anyone else runs into the same problem. 如果有人遇到相同的问题,下面的脚本可以帮我解决问题。 :) :)

  $(document).ready(function(){ $('#navigation a, #fixedbar1 a').on('click', function(e) { e.preventDefault(); }); $(window).on('scroll',function() { var header = $('#header'); var offset = $('#header').position().top + $('#header').outerHeight(true); var scrolltop = $(this).scrollTop(); if(scrolltop >= offset) { $('#fixedbar1').fadeIn(250); } else if(scrolltop <= offset) { $('#fixedbar1').fadeOut(250); } }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM