2

I have a video (currently it's embedded YT video, but could be a normal mp4/HTML5), which I would like to be revealed on scroll. So when scrolled, the text content should go into the top and then the video should be revealed and the user should be able to press Play - it's some kind of a parallax effect maybe, but I couldn't find anything similar... Any other/better solution is very welcome!

EDIT: This is the effect which I want to achieve - maybe there's some ScrollMagic solution..? https://www.invisionapp.com/studio

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)

Smithy
  • 807
  • 5
  • 17
  • 43
  • Possible duplicate of [Trigger event when user scroll to specific element - with jQuery](https://stackoverflow.com/questions/21561480/trigger-event-when-user-scroll-to-specific-element-with-jquery) – SilverSurfer Apr 23 '18 at 08:17
  • nothing with video there... – Smithy Apr 23 '18 at 08:40

1 Answers1

2

Just try to understand what I did in the below fiddle. Its not as hard as it might look like.

What you basicly do is just get the height of the .overlay element, then when user is scrolling keep track of the scroll position using the scrollTop() function.

scrollTop(): Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.

When the scrollTop() reaches the overlayHeight, show the button.

Also keep adding marginTop: scrollTop() until above sentence is true. So that the .show-when-visible element stays visible instead of sitting at the top of the document.


Note that the below snippet is a basic demo showing how you can achieve what you want. Ofcourse you can add event's to the buttonShowWhenVisible. Which the opens a popup / iframe with the corresponding video. ie:

buttonShowWhenVisible.click(function() {
    // code the show the video
});

Open below snippet in full page mode

$(document).ready(function() {
  
  var win = $(window); // Window
  var content = $(".content") // Content jquery element
  var overlay = $(".overlay"); // Overlay jquery element
  var buttonShowWhenVisible = $(".show-when-visible"); // This is the button which will fade in
  var showAfterScroll = $(".show-after-scroll"); // On scroll fade in
  
  var overlayHeight, scrollTop, stopMargin, opacityOut, opacityIn;
  
  win.scroll(function(e) {
    
    scrollTop = win.scrollTop();
    overlayHeight = overlay.outerHeight(); // The height of the overlay
    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();
    }
    
    // Keep adding margin on top so that the element stays in the view until it reached overlayheight
    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 play with the snippet here: https://codepen.io/richardmauritz/pen/QrKvBQ?editors=0010

Red
  • 6,599
  • 9
  • 43
  • 85
  • Thanks a lot for such a long answer, I will try to figure it out and apply it. Is there a way perhaps to modify your code so the text in the overlay will scroll off first, and when it reaches the top the dark semi-transparent gets faded out(opacity 0) - but in a way that won't block the video Controls(maybe getting a negative z-index after fading out)..? https://www.invisionapp.com/studio – Smithy Apr 26 '18 at 12:30
  • Similar, but fading on the way out and the way in if possible..? So, the text should be one overlay which goes off First while fading out. After it's almost on the way out, the dark semitransparent overlay should fade out too(and go off behind with z-index, so the user will be able to press play on the video). Sorry for being annoying, been trying to do this for days :D – Smithy Apr 26 '18 at 12:49
  • 1
    Alright, check it out once again. Is that the desired result? – Red Apr 26 '18 at 13:21
  • Great work, thank you so much! Sorry for repeating myself - but is it possible that the text fades out exactly the same as now - but that the semi-transparent background fades out separately, as the text slowly reaches the top..? So - exactly as here: https://www.invisionapp.com/studio If that's too much work, I will accept your answer nontheless - you deserved it mate – Smithy Apr 26 '18 at 13:26
  • 1
    Hehe ;), aight. Check it out once more. – Red Apr 26 '18 at 13:59
  • Not seeing the difference, but giving you the bounty anyway :) I will try to figure it out... Maybe I was not clear, I would like the text to fade out in the top, and the transparent background to fade out and back after the text is gone(so, as on the link invisionapp.com/studio). Don't wanna be annoying anymore :D Thanks a lot! – Smithy Apr 26 '18 at 14:04
  • (Awarded you, takes 21 hours it seems) – Smithy Apr 26 '18 at 14:04
  • 1
    Ok thanks. Yeay, I think I dont understand it clearly. My english isn't very pro aswell. – Red Apr 26 '18 at 14:10
  • do you know a way to control the margin reduce to be less than 100px, to avoid the slight glitch which happens with the hidden content, perhaps? Scroll is called many times, so some glitching is occuring, don't know is this possible to control @red – Smithy Apr 27 '18 at 12:06
  • 1
    I don't see any glitch in chrome. What I suggest you to do is to use the code as reference. But make it so, that it does what ever you want it to do. Be creative and use your imagination. You will get there in the end :) – Red Apr 27 '18 at 13:11
  • Yeah of course, junior here :D glitch occurs on the First mouse turn, text "there will a button appear when you scroll down...", moves a bit. Since I have more content in my page it is more obvious, especially in IE and Firefox. Gonna try to find some solution... Thanks again for your time and awesome skill :) – Smithy Apr 27 '18 at 13:18