onclick html5,javascript - html5 onClick toggled by clicking controls - Stack Overflow

You can handle the onclick event of the video element with a function that accepts an event argument. This event argument will be populated with a lot of data about the mouse click, including its X/Y position in the layer (which should be the video tag)

From there you can trigger your play/pause event only when the click is in certain areas of the video. I've included an example below where we handle clicks everywhere in the video except the bottom 50 pixels.

document.getElementById("videoElement").onclick = function(ev){

var vid = document.getElementById("videoElement");

var heightOfControls = 50;

// You'll have to figure out a good height to use for your unclickable region where the controls are.

// I used 50 pixels as an example.

var areaAboveControls = vid.height - heightOfControls;

// the layerY attribute of the event lets us know where the mouse was within the topmost layer when the click occurred.

// Using this we can find out where we are in the video and react accordingly.

// Remember that 0 is at the top of the screen on the Y axis, so we need to use greater than to find out if it's BELOW

// our area above the controls.

if(ev.layerY > areaAboveControls)

{

alert("Clicked controls!");

}

else

{

alert("Did not click controls");

// Raise play/pause event from here since the controls won't handle the event and we can safely toggle play/pause.

}

};

With a little bit of experimentation you should be able to find a nice value for heightOfControls that gives you the behavior you're looking for.

Hope this helps!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值