function Alex() {
//给予video.js的页面滚动到视频元素范围内自动播放/出范围暂停播放-----01
this.video_autoplay = function (box) {
var video_box = box.getElementsByTagName('video')[0];
//能否自动播放
var canAuto = true;
//元素距离顶部高度//上滚此距离元素完全出现
var top = box.offsetTop;
//元素高度
var height = parseFloat((window.getComputedStyle ? window.getComputedStyle(box, null) : null || box.currentStyle).height);
//网页可见区域高度
var _height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
//下滚此距离元素完全出现
var min_scroll_height = top - _height + height;
//下滚超过此距离元素完全消失
var max_scroll_height = top + height;
//上滚此距离元素完全出现
var up_max_height = max_scroll_height - height;
//上滚此距离元素完全消失
var up_min_height = top - _height;
//人为点击之后不再自动播放
box.addEventListener('click', function () {
canAuto = false;
});
//判断是上滚还是下滚
var scroll = function (fn) {
var beforeScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
fn = fn || function () {
};
window.addEventListener("scroll", function () {
var afterScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
delta = afterScrollTop - beforeScrollTop;
if (delta === 0) return false;
fn(delta > 0 ? "down" : "up");
beforeScrollTop = afterScrollTop;
}, false);
}
var video_scroll = function () {
//可以自动播放
scroll(function (direction) {
var _top = document.documentElement.scrollTop || document.body.scrollTop;
if ((direction == 'down') && (_top > min_scroll_height) && canAuto) {
video_box.play();
}
if ((direction == 'down') && (_top > max_scroll_height)) {
video_box.pause();
}
if ((direction == 'up') && (_top < up_max_height) && canAuto) {
video_box.play();
}
if ((direction == 'up') && (_top < up_min_height)) {
video_box.pause();
}
});
}
video_scroll();
};
//元素滚动到一定距离后固定在页面顶部------------------------------02
this.menu_fixed = function (menu_container) {
var _top = menu_container.offsetTop;
var scroll_div = function () {
var top = document.documentElement.scrollTop || document.body.scrollTop;
if (top > _top) {
menu_container.style.position = 'fixed';
menu_container.style.top = '0';
} else if (top < _top) {
menu_container.style.position = 'relative';
menu_container.style.top = '0';
}
}
scroll_div();
}
}
var alex = new Alex();

调用方法

var box = document.querySelector('.index_video');
alex.video_autoplay(box);

最新文章

  1. JavaScript并非“按值传递”
  2. Ceph源码解析:PG peering
  3. SQL Server -&gt;&gt; GROUPING SETS, CUBE, ROLLUP, GROUPING, GROUPING_ID
  4. multithread synchronization use mutex and semaphore
  5. php 利用第三方软件进行网页快照
  6. IoC容器Autofac之实例引入(一)
  7. freemarker报错之十四
  8. Flex和Java通信报错
  9. PHP MySQL 插入数据
  10. jquery.validate.js使用实例
  11. Luogu P2336 [SCOI2012]喵星球上的点名
  12. css3流动布局
  13. 数据预处理:独热编码(One-Hot Encoding)和 LabelEncoder标签编码
  14. luogu1600 [NOIp2016]天天爱跑步 (tarjanLca+dfs)
  15. Java httpClient 发送http请求
  16. 一道笔试题:给定编码规则,实现decode()方法
  17. matla互相关协方差的计算和理解
  18. VMware安装与VMware下安装CentOS系统
  19. DOM文档对象模型
  20. Powershell split file

热门文章

  1. 北京Uber优步司机奖励政策(12月21日)
  2. 成都Uber优步司机奖励政策(3月31日)
  3. php session存入redis
  4. cf#516A. Make a triangle!(三角形)
  5. Android Studio怎样创建App项目
  6. egrep及扩展正则
  7. eclipse格式化
  8. 使用JavaScript判断手机是处于横屏还是竖屏
  9. ntp服务:实现时间同步
  10. 正则表达式 和 re 模块