一楼代码
<style>
#papa { margin: 30px 0 0 calc(50% - 593px); width: 1024px; height: 640px; background: url('https://638183.freep.cn/638183/t24/1/purple.jpg') no-repeat center/cover; box-shadow: 3px 3px 20px #000; position: relative; z-index: 1000; }
#btnplay { position: absolute; left: 10px; top: 10px; width: 80px; height: 80px; transition: 5s; cursor: pointer; opacity: .9; animation: rot 6s linear infinite; }
#papa:fullscreen #btnplay { width: 120px; height: 120px; }
#btnFs { position: absolute; bottom: 10px; left: 48%; color: white; background: transparent; border: 2px solid white; border-radius: 6px; padding: 4px; transition: opacity 1s; cursor: pointer; opacity: 0; z-index: 10; }
#msg22 { margin: 40px; color: white; padding: 50px; }
@keyframes rot { to { transform: rotate(360deg); } }
</style>
<div id="papa">
<div id="msg22">Message Box</div>
<div id="btnFs">进入全屏</div>
<img id="btnplay" src="https://638183.freep.cn/638183/small/purple.png" alt="" title="测试按钮" />
</div>
<script>
let go = {x: true, y: true},
timerId = null,
movTimer = null,
fs = false,
rect = papa.getBoundingClientRect();
papa.addEventListener('mousemove', () => {
clearTimeout(timerId);
btnFs.style.opacity = '1';
timerId = setTimeout('btnFs.style.opacity = "0"', 3000);
});
document.addEventListener('fullscreenchange', () => {
if (document.fullscreenElement !== null) {
fs = true;
btnFs.innerText = '退出全屏';
} else {
fs = false;
btnFs.innerText = '进入全屏';
}
});
let correctPos = (son) => {
let left = parseInt(son.style.left), top = parseInt(son.style.top);
if(left < 0) left = 0;
if(top < 0) top = 0;
if(left + son.clientWidth > papa.clientWidth) left = papa.clientWidth - son.clientWidth;
if(top + son.clientHeight > papa.clientHeight) top = papa.clientHeight - son.clientHeight;
son.style.left = left + 'px';
son.style.top = top + 'px';
};
document.onmousemove = function(e) {
if(e.target.id === 'btnplay') return;
clearTimeout(movTimer);
let x, y;
movTimer = setTimeout(function() {
if(document.fullscreenElement && e.target.id.toUpperCase() === 'BTNFS') return;
x = document.fullscreenElement ? e.offsetX || e.layer.x : e.pageX - rect.x;
y = document.fullscreenElement ? e.offsetY || e.layer.y : e.pageY - rect.y;
let left = go.x === true ? x + 'px'span>: '', top = go.y === true ? y + 'px' : '';
btnplay.style.cssText += `left: ${left}; top: ${top};`;
msg22.innerText = `x: ${x} / left: ${left}\ny: ${y} / top: ${top}\npapa: ${papa.clientWidth} / ${papa.clientHeight}`;
correctPos(btnplay);
}, 400);
};
btnFs.onclick = () => fs ? document.exitFullscreen() : papa.requestFullscreen();
</script>
|