简体   繁体   English

使用jQuery / javascript和scrollify查看某些部分时显示div

[英]Display div when certain sections are in view with jquery / javascript and scrollify

I have a website page with four sections which are 100% width and height, and I'm using scrollify so the browser window always snaps to a section. 我有一个网站页面,其中包含四个部分,分别是100%宽度和高度,并且我使用的是scrollify,因此浏览器窗口始终会对齐到一个部分。

  1. I have a fixed header that I want to display only while either of the middle two sections ( #b and #c ) are in view. 我有一个固定的标头,只希望在中间两个部分( #b#c )中的任何一个显示时显示。

  2. I'd like for this header to fade in and fade out. 我想让此标题淡入淡出。 It should be gone by the time section #a or #d is snapped to. 它应该在#a或#d节被捕捉到时消失。

I'm a novice at writing Javascript, so a fully functional solution which works with my code snippet would be much appreciated. 我是编写Javascript的新手,因此非常适合与我的代码段配合使用的功能齐全的解决方案。

 $(function() { $.scrollify({ section: "section", easing: "swing", scrollSpeed: 500, offset: 0, scrollbars: true, before: function() {}, after: function() {}, afterResize: function() {} }); }); 
 body { width: 100%; margin: 0; } #header { background: rgba(0, 0, 0, 0.8); z-index: 1; width: 100%; height: 50px; position: fixed; top: 0; left: 0; } section { width: 100%; height: 100vh; } #a { background: #aaa; } #b { background: #bbb; } #c { background: #ccc; } #d { background: #ddd; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/scrollify/0.1.11/jquery.scrollify.min.js"> </script> <div id="header"> </div> <section id="a"> </section> <section id="b"> </section> <section id="c"> </section> <section id="d"> </section> 

You can use before event of scrollify. 您可以before scrollify事件before使用。 In this event you should get a current slide with the method $.scrollify.current(); 在这种情况下,您应该使用$.scrollify.current();方法获取当前幻灯片$.scrollify.current(); You can find all available methods here ! 您可以在这里找到所有可用的方法! And depending on your slide you can hide or show your header. 根据您的幻灯片,您可以隐藏或显示标题。 Simply use if (){} else {} construction. 只需使用if (){} else {}构造即可。 Example: 例:

if (currentSlide != 1 || currentSlide != 3) {
 header.show();
} else {
 header.hide();
}

or just add some class to header 或者只是将一些类添加到标题

if (currentSlide != 1 || currentSlide != 3) {
 header.addClass('is-shown');
} else {
 header.removeClass('is-shown');
}

https://jsfiddle.net/2Lo92L6s/ https://jsfiddle.net/2Lo92L6s/

You can solve it by jquery.you have to capture section load event and check the section id.if its is #a or #d you have to use: 您可以通过jquery来解决它。您必须捕获部分加载事件并检查部分ID。如果它是#a或#d,则必须使用:

if(id == "a" or id == "b")
{
   $("#header").hide();
}
else{
   $("#header").show();
}

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

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