页面滚动下拉时,元素变为fixed浮动js代码 以视频示例
javascript"><style>
video{width:100%;height:auto}
.div2,#float1{position:fixed;_position:absolute;
top:45px;right:0; z-index:250;}
button{float:right;display:block;margin:5px}
</style>
<section id="about-page">
<div id="float1"style="margin:0 auto;display:none">
<button onclick="showVideo()">显示视频</button>
</div>
<div id="float2"style="margin:0 auto;background:#000;">
<video id="myVideo" src="视频地址" controls autoplay></video>
<button onclick="pauseVideo()">关闭视频</button>
</div>
其它文章内容
</section>
<script>
//关闭开启视频
function pauseVideo() {
var video = document.getElementById("myVideo");
video.pause();
$('#float2').stop().fadeOut();
$('#float1').show();
}
function showVideo() {
var video = document.getElementById("myVideo");
video.play();
$('#float2').show();
$('#float1').stop().fadeOut();
}
// 滚动
(function(){
var oDiv=document.getElementById("float2");
//alert(document.getElementById("my-left-div").offsetWidth);
var H=0,iE6;
var Y=oDiv;
while(Y){H+=Y.offsetTop;Y=Y.offsetParent};
iE6=window.ActiveXObject&&!window.XMLHttpRequest;
if(!iE6){
window.onscroll=function()
{
var s=document.body.scrollTop||document.documentElement.scrollTop;
if(s>H){oDiv.className="div1 div2";
if(iE6){oDiv.style.top=(s-H)+"px";}}
else{oDiv.className="div1";
}
};
}
})();
</script>