Show a div when specific portions of the page are visible using jQuery, JavaScript, and Scrollify

My website page is divided into four sections, each with a width and height of 100%. I am using scrollify to ensure that the browser window snaps to each section.

  1. I need a fixed header to be displayed only when either section #b or #c is in view.

  2. The header should fade in and out, disappearing once section #a or #d comes into focus.

I am new to JavaScript and would greatly appreciate a fully functional solution that works with my existing code snippet.

$(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>

Answer №1

If you want to control the visibility of your header based on scrollify's current slide, you can utilize the before event. Within this event, you can retrieve the current slide using the method $.scrollify.current();. For a list of all available methods, check out this link. Based on the current slide, you can choose to hide or show your header by employing an if(){} else{} statement.

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

An alternative approach is to add a specific class to the header depending on the slide:

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

Check out this demo link for a practical example.

Answer №2

One possible solution is to utilize jQuery for resolving this issue. You need to listen for the section load event and then validate the section ID. If the ID matches #a or #d, you can implement the following logic:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating select option values without reloading the page using a modalHere is an efficient method for updating

Is there a way we can update the select tag option values without having to reload the page after submitting data in the bootstrap modal? Currently, we are facing an issue where the submitted values do not show up in the option tag until the page is reload ...

Encounter Issue: "Describe" function not recognized. This error occurred during the activation of Mocha Test

https://i.sstatic.net/WBSm6.png Upon my installation of mocha, I encountered an issue while running a test using a command, resulting in the error message "describe is not a function." ...

Ways to stop NVDA from reading out the entire link text - Strategies for preventing NVDA from announcing

I am facing an issue with a piece of code within a link. The problem arises when I use NVDA to focus on the element, as the screen reader reads out all the content in the link at once. For instance, here is the code snippet: <a href="example.html" ...

Executing `console.log()` in Windows 8 using JavaScript/Visual Studio 2012

Whenever I utilize console.log("Outputting some text") in JavaScript within Visual Studio 2012 for Windows 8 metro development, where exactly is the text being directed to? Which location should I look at to see it displayed? Despite having the "Output" pa ...

Encountering the error code 'ERR_EMPTY_RESPONSE' while utilizing an AJAX-powered live search feature

My website features a live AJAX search bar that retrieves records from a MySQL database. However, when users repeatedly conduct searches by modifying the search criteria, some web browsers display an error message stating 'ERR_EMPTY_RESPONSE'. ...

Utilize Ajax to invoke a function simultaneously with another Ajax call that includes preventDefault to submit the data

I am currently implementing two AJAX calls within my form. The first call is used to dynamically update the options in the second select element based on the value selected in the first select element. This call reaches out to a PHP page to process the dat ...

Fetching AJAX post data within a controller method in .NET MVC

Using jquery to post some data to a controller's method results in successful posting as shown below. POST http://localhost:16161/VisualObjectPairing/SaveParameters HTTP/1.1 Host: localhost:16161 Connection: keep-alive Content-Length: 146 Accept: ...

Tips on smoothly transitioning an object along a path in an HTML5 Canvas and ensuring that the previous object's residue does not linger behind

Is there a way to smoothly move an object over a line in HTML5 without leaving old objects behind? I need help achieving a seamless movement of a ball across a line. var canvas = document.getElementById('myCanvas'); var context = canvas.getCon ...

Troubleshooting: The Jquery each loop is malfunctioning

I am new to using jquery and I have encountered a problem in my project. I am attempting to iterate through all the links within the #rate_box and attach a click event to them. This click event is supposed to send data to an external php script, then remov ...

Can you please explain the significance of this error for me?

I'm attempting to showcase specific elements from an array in my code. <?php $data = array( 1000 => array( "id" => 1000, "number" => 2380, "name" => "CS2", "instructor" => "Chen", "rating" => null ), 1001 => a ...

Tips for incorporating HTML page snapshot into Facebook sharing post

Currently, my Ruby on Rails app is utilizing the Facebook API for sharing posts. I am wondering if it's feasible to incorporate a snippet of HTML code into the shared post. As of now, only text and a link to the page are being included in the post, bu ...

Verify the existence of a random entry in the database prior to insertion

After creating a code generator that provides various types of codes, I want to simplify it. When a user submits a registration form, certain information is collected and a random code is generated for them. However, I need this random code to be unique to ...

Can jQuery be used to change the CSS of a webpage at a global level, not just targeting specific elements?

Can jQuery be used to update a page's CSS globally, not just for specific elements? For example, let's say I have the following CSS rule in my main stylesheet: .page.left { top: -100%; } and during runtime, I want to change it to top: -50% ...

Recursive functions that request input from the user

Currently in the process of developing a fun little script to help me organize and rate the movies in my personal collection. Among my list are a number of different movie titles that desperately need to be organized. The plan is to implement a merge-sort- ...

Steps to create a dynamic Datatable using Jquery

Can someone please help me create a dynamic table using Jquery DataTables? I specifically need the table to be of type Server-side processing, where all the columns are generated in the same Ajax request along with the results. DataColumns = "" //JSON res ...

Force the page to refresh if the user navigates back using the browser's back button

Similar Question: Cross-browser onload event and the Back button I am looking for a way to automatically refresh a view or page when a user navigates back to it using the browser's back button. Any suggestions? ...

Struggling with pagination when dealing with numerous identical HTML elements within a single file?

I am in the process of creating a web app for a small CTF that will be held at my school. I have designed a webpage to display the problems, but currently they are all lined up in a long row. This is how it's set up: <body class="main-body"&g ...

Error in Angular 2: The requested URL https://imgur-apiv3.p.mashape.com/ cannot be loaded. The use of a wildcard '*' is not allowed in the 'Access-Control-Allow-Origin' header

I'm attempting to upload an image using the imgur API at: . However, I keep encountering this error: XMLHttpRequest cannot load . A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credential ...

``There is a bit of a visual clutter in my sidebar

Help! I've noticed a strange issue where text is overlapping in my sidebar. Check out the image here. I can't seem to figure out why this is happening. Strangely, the overlap disappears when I hover over the item. Here's the HTML for my sid ...

Out of nowhere, the functionality for printing labels in html/css has stopped

For over 3 years, a website has been successfully printing labels for shoes. However, the functionality suddenly broke two days ago, specifically the labels or page-break-after feature stopped working correctly. Despite searching online, I was unable to d ...