Unveil captivating HTML5/YouTube video as you scroll down the page

I am looking to create a parallax effect where a video is revealed on scroll. The video, currently embedded from YouTube but could also be in mp4/HTML5 format, should only appear once the user scrolls and text content moves to the top. Users should then have the option to press play on the video. If you have any other or better solution for achieving this effect, I would appreciate any suggestions!

EDIT: Here is the effect I am trying to achieve - perhaps there is a ScrollMagic solution available..?

Pen: https://codepen.io/anon/pen/wjMwqE

.container {
  width: 1200px;
  height: 600px;
  margin: 0 auto;
  background: lightgrey;
  padding: 100px;
}

.video-container {
  width: 800px;
  background-color: lightblue;
  margin: 100px auto;
  position: relative;
}
.text-container {
  width: 100%;
  height: 100%;
  background-color: rgba(234, 654, 123, .3);
  position: absolute;
  top: 0;
  left: 0;
}
.text-container-content {
    text-align: center;
    margin: 150px auto;
}
<div class="container">
  <div class="video-container">
    <iframe width="800" height="415" src="https://www.youtube.com/embed/HECa3bAFAYk" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    <div class="text-container">
      <div class="text-container-content">
        <h3>Title here</h3>
        <p>Subtitle here</p>
        <button>Button</button>
      </div>
    </div>
  </div>
</div>

(The "duplicate" answer does not have similar video example)

Answer №1

Take a moment to explore the concept demonstrated in the sample below. It's not as complex as it may seem at first glance.

The basic idea is to retrieve the height of the .overlay element and then, as the user scrolls, monitor the scroll position using the scrollTop() function.

scrollTop(): Retrieves the current vertical position of the scroll bar for the first matched element or sets the vertical position of the scroll bar for all matched elements.

Once the scrollTop() value equals the overlayHeight, display the button.

Continuously adjust the marginTop: scrollTop() until the previous condition holds true. This ensures that the .show-when-visible element remains visible instead of staying at the top of the document.


Please note that the code snippet below serves as a basic demonstration of how you can achieve your desired outcome. You can enhance it by adding event handlers to the buttonShowWhenVisible, which could trigger actions like opening a popup or displaying an iframe with the relevant video content:

buttonShowWhenVisible.click(function() {
    // Implement the logic to show the video
});

View the following snippet in full-page mode

$(document).ready(function() {
  
  var win = $(window); // Window
  var content = $(".content") // jQuery element for Content
  var overlay = $(".overlay"); // jQuery element for Overlay
  var buttonShowWhenVisible = $(".show-when-visible"); // The button that fades in
  var showAfterScroll = $(".show-after-scroll"); // Fades in on scroll
  
  var overlayHeight, scrollTop, stopMargin, opacityOut, opacityIn;
  
  win.scroll(function(e) {
    
    scrollTop = win.scrollTop();
    overlayHeight = overlay.outerHeight(); 
    stopMargin = false;
    opacityOut = (1 - scrollTop / overlayHeight);
    opacityIn = (scrollTop / overlayHeight);
    
    if(opacityOut > 0.00)
      overlay.css("opacity", opacityOut);
    
    if(opacityIn < 1)
      showAfterScroll.css("opacity", opacityIn);
    
    if(scrollTop >= overlayHeight) {
      stopMargin = true;
      buttonShowWhenVisible.fadeIn();
    } else {
      buttonShowWhenVisible.fadeOut();
    }
    
    if(!stopMargin) {
      content.css({
        marginTop: scrollTop
      });
    }
    
  });
  
});
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,700');

* {
  box-sizing: border-box;
}

body {
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
}

h1 span {
  color: orange;
}

.content {
  min-height: 2000px;
}

.overlay {
  background-color: rgba(0, 0, 0, .8);
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  color: #fff;
  padding: 40px 0;
  text-align: center;
  z-index: 9999;
}

.show-after-scroll {
  text-align: center;
  padding: 60px 0;
  opacity: 0;
}

.btn-lg {
  background-color: orange;
  color: #fff;
  font-size: 12px;
  padding: 20px 80px;
  border-radius: 40px;
  border: none;
}

.show-when-visible {
  display: none;
}

.overlay p {
  max-width: 600px;
  font-size: 44px;
  margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
  <div class="overlay">
    <h1>Studio<span>.</span></h1>
    <p>This is some long big text saying hello</p>
    <br/><br/><br/>
    <button class="btn-lg">REQUEST EARLY ACCESS</button>
  </div>
  <div class="show-after-scroll">
    <p>There will a button appear when you scroll down, try it out!</p>
    <button class="btn-lg show-when-visible">BONJOUR</button>
  </div>
</div>

You can experiment with the snippet here: https://codepen.io/richardmauritz/pen/QrKvBQ?editors=0010

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

Chrome causing the vanishing act of floated iframes

I encountered an unexpected issue on my website recently. For the past three years, everything was working smoothly until I updated Chrome to version 60. Now, whenever I try to load iframes from Archive.org, they momentarily appear and then vanish instantl ...

"Creating a semi-circular arch with CSS encompassing an entire

I am looking to create a unique design with a half arch surrounding a full circle, similar to the image below. Can anyone provide guidance on how to achieve this effect using CSS? I have successfully created the circle using the code below, but I am uncert ...

Any tips on animating my SVG when I hover my mouse over it with CSS or JavaScript?

Having trouble getting the gray outline to fill when hovering over it. The method I'm currently using isn't working on any browser. Any suggestions? You can find the HTML with the inline SVG file below: CSS is in a separate file, containing cla ...

Customize the website's CSS for optimal viewing on an iPad

I have a website with two CSS files: one for viewing the site on a desktop screen and the other for viewing it on an iPad screen. Despite having the same HTML code, is there a way to detect the iPad device and force it to use the appropriate CSS file? Tha ...

What are some strategies for implementing a basic search function using arrays in JavaScript?

Is my approach to creating a search function using arrays instead of a database correct? <script type="text/javascript> $(document).ready(function() { $(document).on('submit', '#sample', function() { var inputs = docu ...

struggling to implement captcha verification with jQuery validation

I've been working on real-time captcha validation using jQuery Validation version 1.11.1. The captcha value is stored in a session variable called security_code. I'm utilizing the remote method of jQuery for this purpose, which is my first time u ...

Implement a dropdown menu for filtering, but it is currently not functioning as expected

When I select a city_name, my goal is for the graph to only display information pertaining to that particular city. In the params section of my code, I have included filtering options using a selection menu in Vega-Lite. However, despite selecting Brisba ...

An empty row in ng-option continues to be visible within the select menu

Hello, I am facing an issue with a select menu in AngularJS. Here is the relevant code: $scope.GetAllSnimateljiBaseInfo = function () { $http.get(serviceBase + "GetAllSnimateljiBaseInfo", { timeout: 6000 }) .success(function (response) { ...

Issue with deleting data from database using the script

I am facing an issue with deleting records using a button on the Index webpage. I tried using jQuery with AJAX, but it does not seem to be working correctly. Here is my controller method which works fine when called from a form generated by another page u ...

Adjusting the dimensions of images by using the percentage width and height relative to the body

On my website, I am displaying two images that are hosted on another company's server. To ensure these images adjust with the size of the browser window, I have set their width and height as a percentage relative to the body. However, the issue arise ...

Immediately set the width attribute of an HTML element using JavaScript

Can an element's width be dynamically set using the following code: <img id="backgroundImg" src="images/background.png" alt="" width="javascript:getScreenSize()['width']+px" height="1100px"/> In this code, getScreenSize() is a JavaSc ...

Can you guide me on implementing a transition animation through class toggling?

I am facing an issue where certain elements need to be highlighted or unhighlighted conditionally. To achieve this, I have assigned a class called highlight to the elements that require highlighting with a transition animation. The challenge arises when t ...

Create a hover effect for a list item that displays an image and text simultaneously

Is there a way to create an on-hover effect for my icon when hovering over a list item in my dropdown menu? I've shared a codepen link where you can see the issue I'm facing. Everything highlights except the image because I'm only changing ...

Is there a way to show a message when I hover over a different text?

I've been attempting to show a text when hovering over another text, but for some reason it's not working. I've followed all the steps from similar questions, but unfortunately, it still doesn't seem to be functioning correctly. Here i ...

Verify the current line in PHP

Below is the structure of my current table: <div class="container"> <div class="wrapper"> <table class="agenda"> <thead> <tr> <th> ...

I would love to hear your suggestions for a custom select element plugin in jQuery

After browsing multiple options online, I decided to turn to the expertise of the Stack Overflow community to ask for recommendations based on personal experience. I am specifically in search of a plugin that can substitute a select element with a custo ...

Highlight main title in jQuery table of contents

I have successfully created an automatic Table of Contents, but now I need to make the top heading appear in bold font. jQuery(document).ready(function(){ var ToC = "<nav role='navigation' class='table-of-contents vNav'>" + ...

What could be causing the issue with the alignment of text to the right in Bootstrap 4

Is there a way to align the text in the blue column div to the right like the red div? I've tried using text-right but it doesn't seem to work. Also, how can I maintain the spacing between letters like this: [I] | dashboard? https://i.sstatic.ne ...

Ways to Update/Overwrite Current Information in HTML Using Vue

I have experience creating templates for Vue components using JavaScript. Imagine I have HTML that is generated on the server side with a language called UTL: <table> <tr> <td>[% example_var; %]</td> <t ...

Having trouble aligning my CSS form correctly

The issue is with the alignment of the second row of textbox elements under the "Add Equipment" section on this page: While organizing the code in Dreamweaver, the elements line up correctly. However, when viewed in Google Chrome, they shift to the second ...