微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当鼠标在定义的区域时控制视频播放

如何解决当鼠标在定义的区域时控制视频播放

我正在尝试根据鼠标在屏幕上的位置来控制视频播放。 我已将窗口的宽度分成 3 个区域 - 左、中和右。到目前为止,这工作正常。 当鼠标位于“左侧”时,我想指定跳转到某个时间,中间和右侧相同。 我遇到的问题是,每次鼠标移动视频播放重新启动时,我希望它仅在从“左”变为“右”或“中心”时才改变。我已经创建了 current_pos 和 new_pos 变量,但不知道如何正确更新它们。

非常感谢!

PS 暂时省略了视频代码,只是试图让位置正常工作。

var viewport_width = document.documentElement.clientWidth;
var viewport_height = document.documentElement.clientHeight;
var current_pos;
var new_pos;

function getMousePos(e) {
    return {x:e.clientX,y:e.clientY};
}
document.onmousemove=function(e) {
  
    var mousecoords = getMousePos(e);
  
  //left

  if (mousecoords.x < viewport_width/3){
    
    current_pos = 'left';
 
    
  //centre

  }else if (mousecoords.x > viewport_width/3 && mousecoords.x < viewport_width*.66){
    current_pos = 'centre';

  //right 

  }else {
    current_pos = 'right';
  }
    
    console.log(current_pos);
};


解决方法

每次尝试更改时都需要检查 current_pos 是否已更改。

var viewport_width = document.documentElement.clientWidth;
var viewport_height = document.documentElement.clientHeight;
var current_pos;

function getMousePos(e) {
  return {
    x: e.clientX,y: e.clientY
  };
}
document.onmousemove = function(e) {
  var mousecoords = getMousePos(e);

  //left
  if (mousecoords.x < viewport_width / 3) {
    if(current_pos != 'left') {
      current_pos = 'left';
      // Play your video from the desired point
    }
  //centre
  } else if (mousecoords.x > viewport_width / 3 && mousecoords.x < viewport_width * .66) {
    if(current_pos != 'centre') {
      current_pos = 'centre';
      // Play your video from the desired point
    }
  //right 
  } else {
    if(current_pos != 'right') {
      current_pos = 'right';
      // Play your video from the desired point
    }
  }
  console.log(current_pos);
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。