Move youtube if the screen scroll is detected
Move youtube if the screen scroll is detected
by Adi Jaya
HTML
<div class="main">
<div class="content">Scroll this page</div>
<div class="content">Scroll this page</div>
<div class="youtube-container" id="demo">
<button class="close" onclick="alert('alert demo')">×</button>
<iframe
src="https://www.youtube.com/embed/6VnPYD0TKHQ"
frameborder="0"
allowfullscreen>
</iframe>
</div>
<div class="content">Scroll this page</div>
<div class="content">Scroll this page</div>
<div class="content">Scroll this page</div>
<div class="content">Scroll this page</div>
<div class="content">Scroll this page</div>
</div>
CSS
.content {
height: 300px;
border: 1px solid silver;
margin: 1em;
padding: 15px;
background: beige;
}
.youtube-container {
border: 1px solid #949494;
padding: 15px;
margin: 1em;
position: relative;
overflow: auto;
height: 350px;
background: #636161;
box-sizing: border-box;
}
.youtube-container iframe {
position: absolute;
width: 100%;
height: 100%;
border: 0;
display: block;
left: 0;
right: 0;
bottom: 0;
}
.youtube-container.active {
position: fixed;
bottom: 15px;
right: 15px;
height: 230px;
width: 300px;
margin: 0;
}
.close {
position: absolute;
z-index: 1000;
top: 0;
right: 0;
font-size: 1.5em;
background: #2196F3;
border: 1px solid #0074a9;
border-radius: 5px;
display: none;
zoom: 1;
line-height: 1;
cursor: pointer;
color: #fff;
}
.youtube-container.active .close {
display: inline-block;
}
JavaScript
var $demo = document.getElementById("demo");
var $target = $demo.innerHeight || $demo.clientHeight ;
var $height = [ $target * 3 ];
function tes123() {
if ( window.pageYOffset >= $height ) {
$demo.className = "youtube-container active";
} else {
$demo.className = "youtube-container";
}
}
window.onscroll = function(){
tes123();
}