Concealing a division at the conclusion of a <section>

Is there a way to toggle the visibility of a div based on scrolling? When reaching section 2; The 'banner' div should appear at the start of section 2 and disappear when section 2 ends. But how can I determine when section 2 is finished in this case? It needs to stay fixed under the menubar.

This is not the actual source, but similar scripts and CSS settings. Since the real page is too large to include here.

$(window).scroll(function() {
    var scrollTop = $(window).scrollTop();
    var showbar = $('#sec2').height();
    var showbar = showbar -25;

    if ( scrollTop > showbar) { 
        $("#banner").fadeIn(700);
    }else{
        $("#banner").fadeOut(250);
    }
});
html, body {
  margin:0px;
  height:100%;
  width:100%;
  padding:0;
  scroll-behavior: smooth;
}

.container {
    position: absolute;
    width:100%;
    min-height:100%;
    height:100%;
    text-align: center;
}

.wrapper{
    position: relative;
    width:100%;
    max-width:1000px;
    height:100%;
}

section {
    display: block;
    color:white;
    height:100%;
    
    
}

#menubar{
  position: -webkit-sticky;
  position: sticky;
  top:0px;
  width:100%;
  background-color:black;
  
  height:25px;
  text-align: center;
  z-index:1000;
  
}

#menubar a {
  margin:20px 20px 20px 20px;
  text-decoration:none;
  color:white;
  font-weight:bold;
}

#menubar a:hover {
  color:yellow; 
}

#sec1 {
  background-color:red;
}

#sec2 {
  background-color:blue;
}

#sec3 {
  background-color:green;
}

#banner {
  display:none;
  z-index:1000;
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 25;
  height:35px;
  color:black;
  background-color:white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="wrapper">
    <div id="menubar">
      <a href="#sec1">Section 1</a>
      <a href="#sec2">Section 2</a>
      <a href="#sec3">Section 3</a>
    </div>
    <div id="banner">Show this div only in section 2</div>
    <section id="sec1"> bla  </section>
    <section id="sec2"> bla bla </section>
    <section id="sec3"> bla bla bla </section>
  </div>
</div>
    <script>
        function scrolled(o){
            //visible height + pixel scrolled = total height
            if(o.offsetHeight + o.scrollTop == o.scrollHeight){
                var x = document.getElementById("#banner");
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }}
    </script>

I'm hoping someone can provide some guidance.

Answer №1

I made a slight adjustment to the html structure by enclosing the section tags in a div:

<div class="sections">
    <section id="sec1"> bla  </section>
    <section id="sec2"> bla bla </section>
    <section id="sec3"> bla bla bla </section>
</div>

This modification allows the stickiness rule to apply to the height of all sections. Additionally, adjustments were made to the css styles.

$(window).scroll(function() {
    var scrollTop = $(window).scrollTop();
    var showbar = $('#sec2').height();
    var showbar_out = $('#sec3').height();
    var showbar = showbar /*-25*/;
    
 if ( scrollTop > showbar && scrollTop < showbar_out) { 
        $("#banner").fadeIn(700);
    }else{
        $("#banner").fadeOut(250);
    }       
    
    
});
html, body {
  margin:0px;
  height:100%;
  width:100%;
  padding:0;
  scroll-behavior: smooth;
}

.container {
    position: absolute;
    width:100%;
    min-height:100%;
    height:100%;
    text-align: center;
}

.wrapper{
    position: relative;
    width:100%;
    max-width:1000px;
    /*height:100%;*/
}

.sections {
    display: flex;
    flex-direction: column;
}

section {
    display: block;
    color:white;
    /*height:100%;*/
    height: 100vh;
}

#menubar{
  position: -webkit-sticky;
  position: sticky;
  top:0px;
  width:100%;
  background-color:black;
  
  height:25px;
  text-align: center;
  z-index:1000;
  
}

#menubar a {
  margin:20px 20px 20px 20px;
  text-decoration:none;
  color:white;
  font-weight:bold;
}

#menubar a:hover {
  color:yellow; 
}

#sec1 {
  background-color:red;
}

#sec2 {
  background-color:blue;
}

#sec3 {
  background-color:green;
}

#banner {
  display:none;
  z-index:1000;
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 25px;
  height:35px;
  color:black;
  background-color:white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="wrapper">
    <div id="menubar">
      <a href="#sec1">Section 1</a>
      <a href="#sec2">Section 2</a>
      <a href="#sec3">Section 3</a>
    </div>
    <div id="banner">Show this div only in section 2</div>
    <div class="sections">
    <section id="sec1"> bla  </section>
    <section id="sec2"> bla bla </section>
    <section style="height: 200vh" id="sec3"> bla bla bla </section>
    </div>
  </div>
</div>
    <script>
       /* function scrolled(o){
            //visible height + pixel scrolled = total height
            if(o.offsetHeight + o.scrollTop == o.scrollHeight){
                var x = document.getElementById("#banner");
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }}*/
    </script>

Answer №2

Implement a banner within a section and utilize the position:fixed property while using a clip-path technique to conceal it externally:

html,
body {
  margin: 0px;
  height: 100%;
  width: 100%;
  padding: 0;
  scroll-behavior: smooth;
}

.container {
  position: absolute;
  width: 100%;
  min-height: 100%;
  height: 100%;
  text-align: center;
}

.wrapper {
  position: relative;
  width: 100%;
  max-width: 1000px;
  height: 100%;
}

section {
  display: block;
  color: white;
  height: 100%;
}

#menubar {
  position: sticky;
  top: 0px;
  width: 100%;
  background-color: black;
  height: 25px;
  text-align: center;
  z-index: 1000;
}

#menubar a {
  margin: 20px 20px 20px 20px;
  text-decoration: none;
  color: white;
  font-weight: bold;
}

#menubar a:hover {
  color: yellow;
}

#sec1 {
  background-color: red;
}

#sec2 {
  background-color: blue;
  clip-path:inset(0); /* this will keep the banner inside sec2 */
}

#sec3 {
  background-color: green;
}

#banner {
  z-index: 1000;
  position: fixed;
  top: 25px;
  height: 35px;
  color: black;
  background-color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="wrapper">
    <div id="menubar">
      <a href="#sec1">Section 1</a>
      <a href="#sec2">Section 2</a>
      <a href="#sec3">Section 3</a>
    </div>
    <section id="sec1"> bla </section>
    <section id="sec2">
      <div id="banner">Show this div only in section 2</div>
      bla bla </section>
    <section id="sec3"> bla bla bla </section>
  </div>
</div>

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

The input range in ChromeVox is behaving incorrectly by skipping to the maximum value instead of following the correct step value

Attempting to get an input type="range" element to function correctly with ChromeVox has presented me with a challenge. Here's what I'm facing: When I press the right arrow key to move forward, it instantly jumps to the maximum value of 100 in ...

Is there a way to transform a regular timestamp into a BSON timestamp format?

I am currently utilizing the https://www.npmjs.com/package/mongodb package. Upon inserting my timestamp, mongodb converts it into BSON format (which is expected). The original timestamp value is 641912491, but in the database it appears as 1457904327000. ...

Differences between CSS property `transform` and `position: absolute`

Can you explain the distinction between position: absolute; top: 50%; left: 50%; and transform: translate(-50%, -50%); What are the advantages of one over the other? ...

The console is displaying an undefined error for _co.photo, but the code is functioning properly without any issues

I am facing an issue with an Angular component. When I create my component with a selector, it functions as expected: it executes the httpget and renders a photo with a title. However, I am receiving two errors in the console: ERROR TypeError: "_co.photo ...

Create a dynamic background for content output by checkboxes using jQuery

Is it possible to have each individual tag match the background color of its corresponding checkbox's data-color? Currently, all tags change color simultaneously. $("input").on("click", function(e) { var tagColor = $(this).attr("data-color"); ...

tips for accessing the useState value once it has been initialized

When using the state hook in my code, I have: const [features, setFeatures] = useState([]) const [medicalProblem, setMedicalProblem] = useState([]) The medicalProblem variable will be initially populated with a response from an API call: useEf ...

The HTML page is experiencing loading inconsistencies

I am just venturing into the realm of web development (although I am not new to coding), so please bear with me. For my project, I decided to use Django and integrated some free Bootstrap themes that I stumbled upon online. While working on my home page, ...

Can @media screen rules be included in a loop for optimization?

Having @media screen rules in my CSS file that appear as follows: @media screen and (min-width: calc((180px + 40px) * 1 - 40px)) { #main-form { width: calc((180px + 40px) * 1 - 40px); } #main-projects { width: calc((180px + 40px) * 1); } } ...

Scroll Magic not cooperating with Simple Tween local copy

Greetings! I am attempting to replicate this simple tween example using scrollmagic.js. It functions properly on jsfiddle. To confirm, I added scene.addIndicators() to observe the start, end, and trigger hook. It functions flawlessly. As displayed in the ...

The application of border-radius to a solitary side border forms a unique "crescent moon" shape rather than a traditional semi-circle

Is there a way to transform this shape into a regular semicircle, avoiding the appearance of a moon, and change the image into a circle rather than an ellipsis? http://jsfiddle.net/226tq1rb/1/ .image{ width:20%; border-radius:500%; border-rig ...

Retrieve the information enclosed by comments using an HTML DOM parser

Looking to extract the plain text content between two comments, but struggling to find a solution without relying on specific HTML tags in the structure. Parse between comments in Simple HTML Dom Wondering if it's achievable with HTML Dom Parser? ...

Tips for dynamically adjusting the color of link text within jQuery Mobile?

When using jQuery Mobile, links are styled according to the theme stylesheet. If you want to change this styling, you can use !important as follows: .glossaryLink{ color: white !important; background-color: #CC8DCC; } I am looking to dynamically cha ...

Getting console data in AngularJS can be achieved by using the console.log()

This log in the console is functioning correctly. Is there a way to retrieve this information for the HTML? ProductController.js $scope.selectedProduct = function(product) { console.log(product.post_title); console.log(product.ID); console.l ...

Encountering a Npm ERR! when deploying Angular 6 to Heroku due to missing Start script

I am experiencing an issue with my simple angular 6 app after deploying it to Heroku. When I check the logs using the command heroku logs, I encounter the following error: 2018-07-15T00:45:51.000000+00:00 app[api]: Build succeeded 2018-07-15T00:45:53.9012 ...

In Javascript, async functions automatically halt all ongoing "threads" when a new function begins

I have a dilemma with multiple async functions that can be called by the user at any point in time. It is crucial for me to ensure that all previously executed functions (and any potential "threads" they may have initiated) are terminated when a new functi ...

Creating a dynamic HTML select form with generated options - tips and tricks

My HTML form includes a script that automatically activates the default "option" (with hard-coded option values). In addition, I have a separate script that dynamically generates "options" based on specific column values in MySQL. The issue arises when s ...

Is it possible for browsers to preload URLs stored in JavaScript variables without manual intervention?

I am currently developing a website where I am utilizing jQuery to dynamically change background images throughout the site. There will be a significant number of images swapped out as users navigate through the pages. I am considering declaring variables ...

How can JavaScript be used to deactivate an HTML form element?

Within this form, there are two selection buttons that require client-side validation. The user must choose one of them, and once a selection is made, the other button should automatically be disabled. Below is the script I have implemented: <html> ...

Difficulty with info window within a for loop

I am currently working on implementing multiple info windows for markers. I found an example on Stack Overflow that I am trying to follow. Despite not encountering any errors in the console, the info windows do not appear as expected when clicking on the m ...

Arrange, Explore (Refine), Segment HTML Chart

Is there a way to efficiently sort, paginate, and search (based on a specific column) data in an HTML table? I've explored several JQuery plugins such as DataTable, TableSorter, Picnet Table Filter, but none seem to offer all three functionalities (se ...