Div sliding down from the top of the page

I have successfully created a pop up that appears when the user reaches the bottom of the page.

Now, I am looking to implement a similar concept but have the pop up appear from the TOP of the page, at a specific location on the page instead of just top or bottom (specifically within a certain div).

This is how I am currently triggering the pop up:

$(window).scroll(function() {
  if ($(window).scrollTop() + $(window).height() == $(document).height()) {
    $('#signup').addClass('show')
  } else {
    $('#signup').removeClass('show')
  }
});
$('#signup').on('click', '.close', function(e) {
  e.preventDefault();
  $('#signup').removeClass('show')
})
/* popup at end of page */
body {
  height: 1000px;
}

#signup {
  position: fixed;
  z-index:100;
  width: 100%;
  bottom: -50px;
  height: 50px;
  left: 0;
  background-color: green;
  transition: bottom .5s linear;
  color: white;
  font-size: 2em;
  text-align: center
}
#signup.show {
  bottom: 0;
}

html { height: 2000px; }
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="signup" class="signup">
  <div class="container">
    <p class="text-xlg text-center">
      Don't have an account yet? Get started here &nbsp;
      <a class="btn btn-white btn-outline" href="#">Free Trial</a> &nbsp;
      <a class="btn btn-white btn-outline" href="#">Contact Us</a>
    </p>
    <a href="#" class="close"><i class="fa fa-times text-white"></i></a>
  </div>
</div>

Therefore, I am seeking guidance on how to modify this method to make a pop-up descend from the TOP at a specific point on the page. The intention is to display a new navigation bar once the user reaches a particular section. **I do not want to use a sticky div. My aim is for it to remain hidden until triggered, like the example I provided for the pop-up.

Example:

<nav>
  Here is the static nav bar
</nav>
<div>
  Likely a banner in here
</div>
<div class="new-nav">
  Once scrolled to this point, new nav slides down from the top.
</div>

Answer №1

Take a look at the revised code snippet below, followed by an explanation of the modifications that were implemented.

$(window).scroll(function() {
  if ($(window).scrollTop() >= ($(document).height() / 4)) {
    $('#signup').addClass('show')
  } else {
    $('#signup').removeClass('show')
  }
});
$('#signup').on('click', '.close', function(e) {
  e.preventDefault();
  $('#signup').removeClass('show')
})
/* display pop-up at the end of the page */

body {
  height: 1000px;
}
/* creating a scrollbar for demonstration purposes */

#signup {
  position: fixed;
  z-index: 100;
  width: 100%;
  top: -60px;
  height: 50px;
  left: 0;
  background-color: green;
  transition: top .5s linear;
  color: white;
  font-size: 2em;
  text-align: center
}
#signup.show {
  top: 0;
}
html {
  height: 2000px;
}
/* creating a scrollbar for demonstration purposes */
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="signup" class="signup">
  <div class="container">
    <p class="text-xlg text-center">
      Don't have an account yet? Get started here &nbsp;
      <a class="btn btn-white btn-outline" href="#">Free Trial</a> &nbsp;
      <a class="btn btn-white btn-outline" href="#">Contact Us</a>
    </p>
    <a href="#" class="close"><i class="fa fa-times text-white"></i></a>
  </div>
</div>

The adjustments made to achieve this involved tweaking some CSS properties:

#signup {
  position: fixed;
  z-index: 100;
  width: 100%;
  top: -60px;
  height: 50px;
  left: 0;
  background-color: green;
  transition: top .5s linear;
  color: white;
  font-size: 2em;
  text-align: center
}
#signup.show {
  top: 0;
}

This altered the CSS rules to position the element at the top, with modified animations starting from the top.

The JavaScript portion was also updated as follows:

if ($(window).scrollTop() >= ($(document).height() / 4)) {

The condition now triggers the drop-down when the user has scrolled past one-fourth of the screen and maintains it unless they scroll back up above this threshold.

Answer №2

One way to determine if the scroll-y position is at or exceeds the top position of a specific element is by using the following code:

$(this).scrollTop() >= $('.new-nav').position().top

For Example

A jQuery plugin has been crafted for easier reuse of this feature.

(function($) {
  $.fn.onScrollTo = function(focusInFn, focusOutFn) {
    var $this = this;
    $(document).scroll(function() {
      var y = $(this).scrollTop();
      if (y >= $this.position().top) {
        if (focusInFn) focusInFn();
      } else {
        if (focusOutFn) focusOutFn();
      }
    });
  }
})(jQuery);

$('.new-nav').onScrollTo(function() {
  $('#signup').addClass('show');
}, function() {
  $('#signup').removeClass('show');
});

$('#signup').on('click', '.close', function(e) {
  e.preventDefault();
  $('#signup').removeClass('show');
})
.container { position: relative; }

/* create an example scrollbar */
#signup {
  position: fixed;
  z-index:100;
  width: 100%;
  height: 80px;
  top: -80px;
  left: 0;
  background-color: green;
  transition: top 0.67s linear;
  color: white;
  font-size: 2em;
  text-align: center
}
#signup.show {
  top: 0;
}
#signup .close { position: absolute; top: 0.25em; right: 0.125em; }
#signup p { margin-top: 0.125em; line-height: 1.25em; }

nav { text-align: center; font-size: 1.25em; margin: 0.25em; }
.banner { text-align: center; font-size: 2em; margin: 1em; }
.new-nav { height: 800px; padding: 0.5em; }
.text-white { color: #FFFFFF; }
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav>
  This is a static navigation bar
</nav>
<div class="banner">
  Possibly a banner goes here
</div>
<div class="new-nav">
  Upon reaching this point, a new navigation menu will slide down.
</div>

<div id="signup" class="signup">
  <div class="container">
    <p class="text-xlg text-center">
      Want to sign up? Begin here<br />
      <a class="btn btn-white btn-outline" href="#">Free Trial</a> &nbsp;
      <a class="btn btn-white btn-outline" href="#">Contact Us</a>
    </p>
    <a href="#" class="close"><i class="fa fa-times text-white"></i></a>
  </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

How is it possible for the output to be a string array when the variable was declared as a number in TypeScript?

Snippet: function sampleFunction(sample:string|number|string[]) { if(typeof sample == "string") { console.log("Sample is String " + sample); } else if(typeof sample == "number") { console.log("Sample is Number " + sampl ...

The issue of the drop-down menu not appearing persists when using HTML and CSS

ul#menu li ,ul.sub-menu li { list-style-type: none; float:left; } ul#menu li a ,ul.sub-menu li a { display: inline-block; width: 150px; height: 40px; text-decoration: none; line-height: 40px; text-align: center; color:rgb(235, 139, 13) ...

Unable to access model content in multiple AngularJS controllers

My question is clear and straightforward. Let me explain in detail: 1. I have created a module. var ang = angular.module('myApp', []); I have a controller named controller1, which includes the 'campaign' factory. //controllero ...

What is the best way to collaborate and distribute local npm packages within a shared repository across different teams?

Unique Scenario Imagine the structure of a folder as follows: /my-app /src /dist /some-library /src /dist package.json my-package.json Two npm packages are present: one for my-app and one for some-library. my-app relies on some-library. ...

Determine the frequency of form submissions

I am trying to find a way to detect if someone attempts to log in to a website multiple times unsuccessfully, and then redirect them to the forgot password page. I am a bit lost on where to start looking for a solution. My plan is to use jQuery and ajax ...

Connect the hover effect to both elements

In an attempt to connect a grid of links with a vertical menu, I am aiming for a hover effect that highlights both the grid item and the corresponding menu item simultaneously. Here is my current code snippet: /* Grid */ <div class="pos-content count1" ...

Steps to activate the parent list item when the child item is altered

As a newcomer to both ui router and angularjs, I'm encountering a specific issue: Within my header section, the following code is present: <li ng-class="{active: $state.includes('settings')}" id="header01"> <a ...

Trigger a refresh of the Angular app by clicking a button

Recently, I embarked on developing a single-page application that allows users to input data in a text box and navigate to another page. While designing the second page, I aimed to incorporate a home button that would not only return me to the initial view ...

Is there a way to execute a Node 6 npm package within a Node 5.6.0 environment?

I am currently utilizing a tool called easy-sauce to conduct cross-browser JavaScript tests. Essentially, my package.json file references this tool for the test command: { "scripts": { "test": "easy-sauce" } } Everything runs smoothly when I exec ...

Using node-native to authenticate in MongoDB is a surefire way to ensure the

I'm currently facing an issue while attempting to save a document in MongoDB within my Nodejitsu/MongoHQ application. Everything works perfectly locally, but the MongoHQ database requires authentication and it fails even with the correct user/password ...

Generate the Xpath for the mentioned href element to use with Selenium Webdriver

I need help creating the Xpath for a specific href element using Selenium webdriver with only IE browser. The HTML code I am working with is as follows: I am looking to find the Xpath for: . Can someone assist in generating the correct Xpath expression ...

React 16 Component Size Attribute (size="test") is being eliminated

Is anyone else encountering a similar issue where the size attribute of a React component is getting removed? For example, `ReactDOM.render( <h1 size="hello">Hello, world!</h1>, document.getElementById("root") );` You ...

Discovering all instances of a particular name in JSON using incremented values: a guide

I am looking to automatically detect every occurrence of a specific name in my JSON data. { "servergenre": "Classic Rock", "servergenre2": "pop", "servergenre3": "rock", "servergenre4": "jazz", "servergenre5": "80s", "serverurl": "http://www.n ...

Bootstrap UI Tab presents an obstacle for Google Map functionality

When utilizing the Bootstrap Tabset to include a Bootstrap slider, Google Map, and other pages, I encountered an issue where the slider functions perfectly but the Google Map does not work as expected. Interestingly, the map works perfectly in street view ...

Instructions for integrating AJAX into a PHP script

I am looking to incorporate AJAX into my PHP file so that when I delete an item from a list, the data automatically reloads and the list is updated with the new information. I have created a list of all my data along with a delete button. Below is the PH ...

Navigating through AngularJS routes leads to an unexpected directory being added

Assigned to an AngularJS project after the departure of a team member, I found myself navigating unfamiliar territory as a Python/Java developer. Despite its outdated AngularJS version 1.0.8, I aim to modernize the system once it's stable. An issue h ...

IE causes link_to :remote => :true to malfunction

I'm facing an issue with remote links in Internet Explorer, and it's crucial to resolve it promptly as the deadline is today. The problem arises when we utilize AJAX to initiate a remote call to an action and evaluate the JavaScript returned. Wh ...

Issues with the parseInt() Function

I'm having trouble figuring out why parseInt() isn't working correctly in my code, especially when passing numbers through to my function parameters. It keeps returning NaN in both my array and the function's return value. What I'm att ...

Instructions for inserting text into a donut chart created with Google, utilizing basic HTML, JavaScript, or jQuery syntax

Are there any attributes in Google Chart that I should know about? I am just starting to use Google Charts and would appreciate some guidance. Here is the code snippet I am working with: function drawChart() { var data = google.visualization.arrayTo ...

Generating Ionic components with HTML markup

When I use $http.get to fetch a JSON file, the Ionic view is not displaying the output correctly: loadNews() { return new Promise(resolve => { let header = {"Content-Type": "application/json"}; this.http.get('http://www.mywebs ...