HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
If the stylesheet you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<script src="https://www.youtube.com/iframe_api" defer></script>
<div class="mesBox">
<div id="mes"></div>
</div>
<main>
<div class="mb-y">
<div class="embed-youtube">
<div id="sample01" data-movieid="HKThn7Phxkk" class="ytPlayerReady">
<img src="https://img.youtube.com/vi/HKThn7Phxkk/mqdefault.jpg" alt="" width="640" height="390">
</div>
</div>
<div class="controller" data-playerid="sample01" style="display:none">
<p>
<span class="y_start">スタート</span> |
<span class="y_pause">一時停止</span>
</p>
</div>
</div>
<div class="mb-y">
<div class="embed-youtube">
<div id="sample02" data-movieid="jmhKf2nh2ZA" class="ytPlayerReady">
<img src="https://img.youtube.com/vi/jmhKf2nh2ZA/mqdefault.jpg" alt="" width="640" height="390">
</div>
</div>
<div class="controller" data-playerid="sample02" style="display:none">
<p>
<span class="y_start">スタート</span> |
<span class="y_pause">一時停止</span>
</p>
</div>
</div>
<div class="mb-y">
<div class="embed-youtube">
<div id="sample03" data-movieid="KoHltPzjs3U" class="ytPlayerReady">
<img src="https://img.youtube.com/vi/KoHltPzjs3U/mqdefault.jpg" alt="" width="640" height="390">
</div>
</div>
<div class="controller" data-playerid="sample03" style="display:none">
<p>
<span class="y_start">スタート</span> |
<span class="y_pause">一時停止</span>
</p>
</div>
</div>
<div class="mb-y">
<div class="embed-youtube">
<div id="sample04" data-movieid="4niQ-lCTTv4" class="ytPlayerReady">
<img src="https://img.youtube.com/vi/4niQ-lCTTv4/mqdefault.jpg" alt="" width="640" height="390">
</div>
</div>
<div class="controller" data-playerid="sample04" style="display:none">
<p>
<span class="y_start">スタート</span> |
<span class="y_pause">一時停止</span>
</p>
</div>
</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
html,
body {
/* width: 100%; */
box-sizing: border-box;
}
main {
width: 100%;
max-width: 780px;
margin: 0 auto 20px;
padding: 0 10px;
box-sizing: border-box;
}
p,
pre {
margin-bottom: 10px;
line-height: 1.6;
overflow: hidden;
}
.mesBox {
font-size: 80%;
background: #eee;
padding: 10px;
margin: 10px 0 0;
max-height: 25%;
left: 50%;
transform: translate(-50%, -0);
overflow: auto;
width: 760px;
max-width: 100%;
box-sizing: border-box;
position: fixed;
bottom: 0;
z-index: 9999;
}
.embed-youtube {
position: relative;
height: 0;
padding-top: 56.25%;
}
.embed-youtube iframe,
.embed-youtube img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.ytPlayerReady {
cursor: pointer;
}
.ytPlayerReady img {
border-radius: 5px;
}
.ytPlayerReady:before {
position: absolute;
content: "";
color: #fff;
text-align: center;
font-size: 22px;
font-weight: 700;
line-height: 180px;
background: rgba(0, 0, 0, .6);
border-radius: 5px;
top: 0;
left: 0;
width: 780px;
max-width: 100%;
height: 100%;
z-index: 10;
/* transition: all .3s; */
transition: .5s;
background-image: url(./images/yt_icon_mono_light.png);
background-repeat: no-repeat;
background-position: center;
background-size: 80px;
}
.ytPlayerReady:hover::before {
background-image: url(./images/yt_icon_rgb.png);
}
.controller {
margin: 10px auto;
text-align: center;
}
.controller .y_start,
.controller .y_pause {
cursor: pointer;
text-decoration: underline;
color: rgb(64, 31, 253);
}
iframe {
max-width: 100%;
}
.mb-y {
margin-bottom: 400px;
}
// 各プレーヤーの格納
var ytPlayer = [];
// プレーヤーのサイズ
var ytWidth = 640;
var ytHeight = 390;
var ua = navigator.userAgent;
if (ua.indexOf('iPhone') > 0 || ua.indexOf('iPad') > 0 || ua.indexOf('iPod') > 0) {
var os = "ios";
} else if (ua.indexOf('Android') > 0) {
var os = "android";
} else {
var os = "pc";
}
logmes("OS:" + os);
// iOSはlazyload的に処理
if (os == "ios") {
var thisOffset = [];
$(window).on('load', function() {
//要素の位置取得
$(".ytPlayerReady").each(function(i, elm) {
thisOffset[i] = [];
thisOffset[i]["playerId"] = $(elm).attr("id");
thisOffset[i]["movieId"] = $(elm).attr("data-movieid");
thisOffset[i]["ytWidth"] = ytWidth;
thisOffset[i]["ytHeight"] = ytHeight;
thisOffset[i]["height"] = $(elm).offset().top + $(elm).outerHeight();
thisOffset[i]["lazyStatus"] = false;
logmes("位置:" + $(elm).attr("id") + ":" + thisOffset[i]["height"]);
});
// console.log(thisOffset);
//スクロール開始前にも、ファーストビュー中に動画があったらYouTube起動処理
youtubeLazy(ytPlayer, thisOffset);
});
//lazyでのYouTube起動処理 スクロール中
$(window).scroll(function() {
youtubeLazy(ytPlayer, thisOffset);
});
}
function youtubeLazy(ytPlayer, thisOffset) {
var nowScrollTop;
for (var i = 0; i < thisOffset.length; i++) {
nowScrollTop = $(window).scrollTop() + $(window).height();
if (nowScrollTop > thisOffset[i]["height"] && !thisOffset[i]["lazyStatus"]) {
logmes("lazyload:" + thisOffset[i]["playerId"]);
$("#" + thisOffset[i]["playerId"]).removeClass("ytPlayerReady");
$("#" + thisOffset[i]["playerId"]).parent().parent().find(".controller").show();
youTubeIframeAPIReady(ytPlayer, thisOffset[i]["playerId"], thisOffset[i]["movieId"], thisOffset[i]["ytWidth"], thisOffset[i]["ytHeight"]);
thisOffset[i]["lazyStatus"] = true;
}
}
}
//画像クリックでのYouTube起動処理
$(".ytPlayerReady").click(function() {
var playerId = $(this).attr("id");
var movieId = $(this).attr("data-movieid");
$(this).removeClass("ytPlayerReady");
$(this).parent().parent().find(".controller").show();
// logmes("---");
logmes("起動クリック:" + playerId + ":" + movieId);
youTubeIframeAPIReady(ytPlayer, playerId, movieId, ytWidth, ytHeight);
});
$(".y_start").click(function() {
// logmes("---");
// logmes("■ボタン:" + playerId + ":playVideo");
var playerId = $(this).parents('.controller').data("playerid");
videoControl("playVideo", playerId);
// playVideo();
});
$(".y_pause").click(function() {
// logmes("---");
// logmes("■ボタン:" + playerId + ":pauseVideo");
var playerId = $(this).parents('.controller').data("playerid");
videoControl("pauseVideo", playerId);
});
// 各プレーヤーの埋め込み
function youTubeIframeAPIReady(ytPlayer, playerId, movieId, ytWidth, ytHeight) {
ytPlayer[playerId] = new YT.Player(playerId, {
width: ytWidth,
height: ytHeight,
videoId: movieId,
playerVars: {
rel: 0
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 各プレーヤー準備完了後の処理
function onPlayerReady(event) {
playerId = event.target.getIframe().id;
logmes(playerId + ":onPlayerReady");
if (os != "ios") {
// videoControl("mute", playerId);
videoControl("playVideo", playerId);
}
}
function onPlayerStateChange(event) {
playerId = event.target.getIframe().id;
// logmes("---");
logmes(playerId + ":onPlayerStateChange");
playerStateView(event, playerId);
}
function playerStateView(event, playerId) {
var ytStatus = ytPlayer[playerId].getPlayerState();
if (ytStatus == 1) {
logmes('ステータス:再生中');
} else if (ytStatus == 0) {
logmes('ステータス:終了');
} else if (ytStatus == 2) {
logmes('ステータス:一時停止中');
} else if (ytStatus == 3) {
logmes('ステータス:バッファリング中');
} else if (ytStatus == 5) {
logmes('ステータス:頭出し済み');
} else if (ytStatus == -1) {
logmes('ステータス:未開始');
} else {
logmes('ステータス:該当なし?ログ確認');
console.log(ytStatus);
}
}
function videoControl(action, playerId) {
// playVideo or pauseVideo or mute or unMute
logmes("操作:" + playerId + ":" + action);
var $playerWindow = $("#" + playerId)[0].contentWindow;
if ($playerWindow) {
$playerWindow.postMessage('{"event":"command","func":"' + action + '","args":""}', '*');
}
}
function logmes(log) {
if (log != "") {
var mes = $("#mes").html();
mes = mes + "<p>" + log + "</p>";
$("#mes").html(mes);
var scrollPoint = $('#mes')[0].scrollHeight;
// $(".mesBox").scrollTop(scrollPoint);
$('.mesBox').animate({
scrollTop: $('#mes')[0].scrollHeight
});
}
}
Also see: Tab Triggers