zoukankan      html  css  js  c++  java
  • 整合常用功能的JS小组件库-v1.0

    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);
  • 相关阅读:
    近期文章与教程和其他情况说明
    Python从入门到精通--课程目录
    第四天:创建型模式--原型模式
    python api链接数据库
    Delphi 实现简易语音发音(基于TTS方式)
    delphi下运行vbscript脚本
    使用PaxScript为Delphi应用增加对脚本的支持
    delphi与javascript互通
    奇技淫巧之Delphi和JavaScript互通
    在delphi中执行javascript代码
  • 原文地址:https://www.cnblogs.com/chenzeyongjsj/p/6560344.html
Copyright © 2011-2022 走看看