【问题标题】:Get video to playback in direction mouse moves让视频在鼠标移动的方向上播放
【发布时间】:2016-10-12 07:19:53
【问题描述】:

当鼠标在 x 轴上移动时,我正在尝试使用一些 Javascript 来触发视频播放,但无法让它工作。

我已经包含了an example 我想要实现的目标,以及使之成为可能的代码。

var video = document.getElementById('video');

var x = window.innerWidth / 2;
var y = 0;

var loaded = false;

document.onclick = function(e) {
    window.parent.postMessage('feature:click', '*');
};

// function elementAtMousePosition() {
//     return document.elementFromPoint(x, y);
// }

// document.addEventListener('click', function(event) {
//     var newEvent = new Event(event.type);
//     elementAtMousePosition().dispatchEvent(newEvent);
// });

document.onmousemove = function(vent) {
    event = event || window.event;
    x = event.clientX;
    y = event.clientY;

    if (loaded) {
        throttledSeek();
    }
};

var throttle = function(delay, callback) {
    var previousCall = new Date().getTime();
    return function() {
        var time = new Date().getTime();
        if ((time - previousCall) >= delay) {
            previousCall = time;
            callback.apply(null, arguments);
        }
    };
};

var seek = function() {
    var spins = 3;

    var pos = (x - (window.innerWidth / spins * 0.5)) / (window.innerWidth / spins);

    pos -= Math.floor(pos);

    video.currentTime = pos * video.duration;
};

var throttledSeek = throttle(1000 / 16, seek);

var onload = function() {
    coverVid(video, video.videoWidth, video.videoHeight);

    loaded = true;

    video.style.opacity = 1;

    window.onresize();

    seek();
};

video.load();

video.addEventListener("canplaythrough", function() {
  this.play();
  this.pause();

  onload();
});

Link to Codepen

我尝试使用 jQuery 来做类似的事情,但是 .mousemove() 在 Chrome 中的触发频率不够高,无法实现这一目标。想知道我错过了什么/遗漏了什么/完全愚蠢,这使得我在上面提供的代码和示例打勾。任何建议、建设性批评或指点都将不胜感激!

【问题讨论】:

    标签: javascript jquery css html html5-video


    【解决方案1】:

    我已经分叉了你的代码笔,我不知道你的搜索需要如何工作,但是现在当我移动鼠标时,视频搜索。

    这里:http://codepen.io/anon/pen/KgRjow

    稍微修改了你的脚本:

    (function() {
       // your page initialization code here
       // the DOM will be available here
        var video = document.getElementById('deko_vid');
    
        var x = window.innerWidth / 2;
        var y = 0;
    
        var loaded = false;
    
        document.onclick = function(e) {
            window.parent.postMessage('feature:click', '*');
        };
    
        // function elementAtMousePosition() {
        //     return document.elementFromPoint(x, y);
        // }
    
        // document.addEventListener('click', function(event) {
        //     var newEvent = new Event(event.type);
        //     elementAtMousePosition().dispatchEvent(newEvent);
        // });
    
        document.onmousemove = function(vent) {
            event = event || window.event;
            x = event.clientX;
            y = event.clientY;
    
            if (loaded) {
                throttledSeek();
            }
        };
    
    
        var seek = function() {
            var spins = 3;
    
            var pos = (x - (window.innerWidth / spins * 0.5)) / (window.innerWidth / spins);
    
            pos -= Math.floor(pos);
    
            video.currentTime = pos * video.duration;
        };
    
        var throttle = function(delay, callback) {
            var previousCall = new Date().getTime();
            return function() {
                var time = new Date().getTime();
                if ((time - previousCall) >= delay) {
                    previousCall = time;
                    callback.apply(null, arguments);
                }
            };
        };
    
        var throttledSeek = throttle(1000 / 16, seek);
    
        function onload() {
          loaded = true;
        };
    
        video.load();
    
        video.addEventListener("canplaythrough", function() {
          this.play();
          this.pause();
    
          onload();
        });
    
    })();
    

    请提及我还修改了您的 HTML...我将视频源的 ID 属性移到了实际的视频元素中。

    【讨论】:

    • 太棒了!基本解决了我的问题,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2018-07-31
    相关资源
    最近更新 更多