HTML, CSS, JavaScript 网络电影播放器制作指南 - 播放预告片,显示海报,全方位控制
以下是基本的 HTML 结构和 CSS 样式,用于创建一个客户端的网络电影播放器:
<!DOCTYPE html>
<html>
<head>
<title>Movie Player</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
font-size: 14px;
}
#player {
width: 100%;
height: 500px;
background-color: #000;
position: relative;
}
#video {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
#poster {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#controls {
width: 100%;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
bottom: 0;
left: 0;
z-index: 2;
display: flex;
align-items: center;
}
#play-pause {
width: 50px;
height: 50px;
background-color: rgba(255, 255, 255, 0.5);
margin-right: 10px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
#play-pause:hover {
background-color: rgba(255, 255, 255, 0.7);
}
#play-pause i {
font-size: 20px;
}
#volume {
width: 100px;
height: 10px;
background-color: rgba(255, 255, 255, 0.5);
margin-right: 10px;
position: relative;
display: flex;
align-items: center;
}
#volume input[type='range'] {
-webkit-appearance: none;
background-color: transparent;
width: 100%;
height: 2px;
margin: 0;
padding: 0;
cursor: pointer;
}
#volume input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
background-color: #fff;
border-radius: 100%;
width: 10px;
height: 10px;
margin-top: -4px;
cursor: pointer;
}
#volume input[type='range']::-moz-range-thumb {
background-color: #fff;
border-radius: 100%;
width: 10px;
height: 10px;
cursor: pointer;
}
#fullscreen {
width: 50px;
height: 50px;
background-color: rgba(255, 255, 255, 0.5);
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
#fullscreen:hover {
background-color: rgba(255, 255, 255, 0.7);
}
#fullscreen i {
font-size: 20px;
}
</style>
</head>
<body>
<div id='player'>
<video id='video' poster='path/to/poster.jpg'>
<source src='path/to/trailer.mp4' type='video/mp4'>
<source src='path/to/trailer.webm' type='video/webm'>
<source src='path/to/trailer.ogv' type='video/ogg'>
</video>
<div id='poster'></div>
<div id='controls'>
<div id='play-pause'>
<i class='fa fa-play'></i>
</div>
<div id='volume'>
<input type='range' min='0' max='1' step='0.1' value='1'>
</div>
<div id='fullscreen'>
<i class='fa fa-expand'></i>
</div>
</div>
</div>
<script src='path/to/jquery.js'></script>
<script>
$(document).ready(function() {
// Hide the poster when the video is playing
$('#video').on('play', function() {
$('#poster').hide();
});
// Show the poster when the video is paused
$('#video').on('pause', function() {
$('#poster').show();
});
// Play/Pause button
$('#play-pause').click(function() {
if ($('#video').get(0).paused) {
$('#video').get(0).play();
$(this).html('<i class='fa fa-pause'></i>');
} else {
$('#video').get(0).pause();
$(this).html('<i class='fa fa-play'></i>');
}
});
// Volume control
$('#volume input[type='range']').on('input', function() {
$('#video').get(0).volume = $(this).val();
});
// Fullscreen button
$('#fullscreen').click(function() {
if ($('#video').get(0).requestFullscreen) {
$('#video').get(0).requestFullscreen();
} else if ($('#video').get(0).webkitRequestFullscreen) {
$('#video').get(0).webkitRequestFullscreen();
} else if ($('#video').get(0).mozRequestFullScreen) {
$('#video').get(0).mozRequestFullScreen();
}
});
});
</script>
</body>
</html>
以上代码是一个基本的电影播放器模板,其中包含以下组件:
- 一个 video 标签,用于播放电影预告片
- 一个 div 标签,用于显示电影海报
- 一个控制栏,包含以下组件:
- 一个播放/暂停按钮
- 一个音量控制条
- 一个全屏按钮
可以通过自定义 CSS 样式来调整播放器的外观和布局。
在 JavaScript 中,可以使用 jQuery 来处理播放器的交互,包括:
- 当视频播放时隐藏海报
- 当视频暂停时显示海报
- 响应播放/暂停按钮点击事件
- 响应音量控制条变化事件
- 响应全屏按钮点击事件
可以根据需要对 JavaScript 代码进行扩展,以实现更多功能,例如:
- 进度条
- 倍速播放
- 字幕
- 播放列表
- 等等
原文地址: https://www.cveoy.top/t/topic/lVhd 著作权归作者所有。请勿转载和采集!