{"id":1894,"date":"2025-05-27T02:48:29","date_gmt":"2025-05-27T02:48:29","guid":{"rendered":"https:\/\/satokotadesign.com\/blog\/?p=1894"},"modified":"2025-05-27T02:48:29","modified_gmt":"2025-05-27T02:48:29","slug":"js_random_img","status":"publish","type":"post","link":"https:\/\/satokotadesign.com\/blog\/js_random_img\/","title":{"rendered":"JavaScript\u3067\u80cc\u666f\u753b\u50cf\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u5207\u308a\u66ff\u3048\u308b\u65b9\u6cd5\uff5c\u521d\u5fc3\u8005\u5411\u3051\u306b\u4e01\u5be7\u89e3\u8aac\uff01"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\uff01\u9759\u5ca1\u770c\u6d5c\u677e\u5e02\u3067WEB\u30c7\u30b6\u30a4\u30ca\u30fc\u3092\u3057\u3066\u3044\u307e\u3059\u5c0f\u7027\u3067\u3059\u3002<br \/>\nWeb\u30b5\u30a4\u30c8\u306b\u3061\u3087\u3063\u3068\u3057\u305f\u300c\u5909\u5316\u300d\u3084\u300c\u904a\u3073\u5fc3\u300d\u3092\u52a0\u3048\u305f\u3044\u3068\u304d\u3001\u771f\u3063\u5148\u306b\u601d\u3044\u6d6e\u304b\u3076\u306e\u304c<span class=\"sc_marker-animation\">\u201c\u80cc\u666f\u753b\u50cf\u306e\u6f14\u51fa\u201d<\/span>\u3067\u3059\u3002<br \/>\n\u7279\u306b\u3001<span class=\"sc_marker-animation\">\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3073\u306b\u80cc\u666f\u304c\u30e9\u30f3\u30c0\u30e0\u306b\u5909\u308f\u3063\u305f\u3089<\/span>\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u5370\u8c61\u3082\u30ac\u30e9\u30c3\u3068\u5909\u308f\u308a\u307e\u3059\u3088\u306d\u3002<\/p>\n<p>\u300c\u304a\u3001\u3055\u3063\u304d\u3068\u9055\u3046\u30c7\u30b6\u30a4\u30f3\u3060\uff01\u300d<br \/>\n\u300c\u4eca\u65e5\u306f\u3069\u3093\u306a\u30d3\u30b8\u30e5\u30a2\u30eb\u306a\u3093\u3060\u308d\u3046\uff1f\u300d<\/p>\n<p>\u305d\u3093\u306a\u3061\u3087\u3063\u3068\u3057\u305f<span class=\"sc_marker-animation\">\u201c\u4ed5\u639b\u3051\u201d<\/span>\u304c\u3001\u30e6\u30fc\u30b6\u30fc\u3068\u306e\u8ddd\u96e2\u3092\u3050\u3063\u3068\u7e2e\u3081\u3066\u304f\u308c\u308b\u3093\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001JavaScript\u3060\u3051\u3067\u80cc\u666f\u753b\u50cf\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u5207\u308a\u66ff\u3048\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4e0d\u8981\u3002\u8ab0\u3067\u3082\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u306e\u3067\u3001Web\u521d\u5fc3\u8005\u306e\u65b9\u3082\u305c\u3072\u30c1\u30e3\u30ec\u30f3\u30b8\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n<h2 class=\"sc_heading solid blue\"><span class=\"sc_title\">\u57fa\u672c\u306e\u8003\u3048\u65b9\uff1a\u753b\u50cf\u306e\u30ea\u30b9\u30c8\u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b1\u3064\u3092\u9078\u3076<\/span><\/h2>\n<p>\u3084\u308b\u3053\u3068\u306f\u3068\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<div class=\"sc_designlist ul fa_check blue\">\n<ul>\n<li>\u8907\u6570\u306e\u80cc\u666f\u753b\u50cfURL\u3092JavaScript\u3067\u7528\u610f\u3059\u308b<\/li>\n<li>\u305d\u306e\u4e2d\u304b\u3089\u30e9\u30f3\u30c0\u30e0\u30671\u679a\u3092\u9078\u3076<\/li>\n<li>\u9078\u3070\u308c\u305f\u753b\u50cf\u3092\u80cc\u666f\u306b\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<\/div>\n<p>HTML\u3068CSS\u306f\u6700\u4f4e\u9650\u3067OK\u3002JavaScript\u3060\u3051\u3067\u5b8c\u7d50\u3057\u307e\u3059\u3002<\/p>\n<h2 class=\"sc_heading solid blue\"><span class=\"sc_title\">\u5b9f\u88c5\u30b3\u30fc\u30c9\uff08HTML\uff0bCSS\uff0bJavaScript\uff09<\/span><\/h2>\n<p>\u300cReturn\u300d\u30dc\u30bf\u30f3\u3067\u4f55\u5ea6\u3082\u30ea\u30ed\u30fc\u30c9\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p class=\"codepen\" data-height=\"300\" data-default-tab=\"html,result\" data-slug-hash=\"vEOEgag\" data-pen-title=\"Untitled\" data-user=\"ekwwawpb-the-styleful\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\">\n  <span>See the Pen <a href=\"https:\/\/codepen.io\/ekwwawpb-the-styleful\/pen\/vEOEgag\"><br \/>\n  Untitled<\/a> by \u5c0f\u7027\u8ce2 (<a href=\"https:\/\/codepen.io\/ekwwawpb-the-styleful\">@ekwwawpb-the-styleful<\/a>)<br \/>\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/span>\n<\/p>\n<p><script async src=\"https:\/\/public.codepenassets.com\/embed\/index.js\"><\/script><\/p>\n<h3 class=\"sc_heading bborder b blue\"><span class=\"sc_title\">HTML<\/span><\/h3>\n<p>\u80cc\u666f\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u8981\u7d20\u306b <span class=\"fontbackground\" style=\"background:;\">id<\/span> \u3092\u4ed8\u3051\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\" ><code class=\"language-markup\">&lt;body id=&quot;hero&quot;&gt;\r\n  &lt;h1&gt;\u3088\u3046\u3053\u305d\uff01&lt;\/h1&gt;\r\n&lt;\/body&gt;<\/code><\/pre>\n<h3 class=\"sc_heading bborder b blue\"><span class=\"sc_title\">CSS<\/span><\/h3>\n<p>#hero \u306b\u80cc\u666f\u753b\u50cf\u306e\u5171\u901a\u8a2d\u5b9a\u3092\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\" ><code class=\"language-css\">#hero {\r\n  background-size: cover;\r\n  background-position: center;\r\n  background-repeat: no-repeat;\r\n  height: 100vh;\r\n  color: white;\r\n  text-align: center;\r\n  padding-top: 20%;\r\n}<\/code><\/pre>\n<h3 class=\"sc_heading bborder b blue\"><span class=\"sc_title\">JavaScript<\/span><\/h3>\n<p>\u753b\u50cfURL\u3092\u914d\u5217\u3067\u7528\u610f\u3057\u3001\u30e9\u30f3\u30c0\u30e0\u306b1\u679a\u9078\u3093\u3067\u80cc\u666f\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\" ><code class=\"language-js\">document.addEventListener(&quot;DOMContentLoaded&quot;, function () {\r\n  const images = [\r\n    &quot;https:\/\/images.unsplash.com\/photo-1497290756760-23ac55edf36f?q=80&amp;w=1974&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.1.0&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&quot;,\r\n    &quot;https:\/\/images.unsplash.com\/photo-1742836531212-c55044926fbd?q=80&amp;w=2070&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.1.0&amp;ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&quot;,\r\n    &quot;https:\/\/images.unsplash.com\/photo-1747863498866-e88b7ef50d3e?q=80&amp;w=1970&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.1.0&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&quot;,\r\n    &quot;https:\/\/plus.unsplash.com\/premium_photo-1745616638280-305b6a90a25d?q=80&amp;w=1995&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.1.0&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&quot;\r\n  ];\r\n\r\n  const randomIndex = Math.floor(Math.random() * images.length);\r\n  const selectedImage = images[randomIndex];\r\n\r\n  const hero = document.getElementById(&quot;hero&quot;);\r\n  hero.style.backgroundImage = `url(&#039;${selectedImage}&#039;)`;\r\n});<\/code><\/pre>\n<h2 class=\"sc_heading solid blue\"><span class=\"sc_title\">\u88dc\u8db3\uff1a\u753b\u50cf\u3092\u8efd\u91cf\u306b\uff06\u8996\u8a8d\u6027\u306b\u914d\u616e\u3057\u3088\u3046<\/span><\/h2>\n<p>\u30e9\u30f3\u30c0\u30e0\u3068\u306f\u3044\u3048\u3001\u80cc\u666f\u753b\u50cf\u306f\u30b5\u30a4\u30c8\u306e\u201c\u9854\u201d\u3002\u6b21\u306e\u30dd\u30a4\u30f3\u30c8\u306b\u6c17\u3092\u914d\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u4f7f\u3044\u3084\u3059\u304f\u3001\u7f8e\u3057\u3044\u6f14\u51fa\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"sc_designlist ul fa_check blue\">\n<ul>\n<li><span class=\"sc_marker-animation\">\u753b\u50cf\u30b5\u30a4\u30ba\u306f\u5727\u7e2e\u3057\u3066\u8efd\u91cf\u306b<\/span>\uff08WebP\u306a\u3069\u3082\u304a\u3059\u3059\u3081\uff09<\/li>\n<li><span class=\"sc_marker-animation\">\u30c6\u30ad\u30b9\u30c8\u304c\u8aad\u307f\u306b\u304f\u304f\u306a\u3089\u306a\u3044\u3088\u3046\u30d5\u30a3\u30eb\u30bf\u30fc\u3092\u304b\u3051\u308b<\/span>\uff08\u6697\u3081\u306e\u30ec\u30a4\u30e4\u30fc\u3084\u30b0\u30e9\u30c7\u30fc\u30b7\u30e7\u30f3\uff09<\/li>\n<li><span class=\"sc_marker-animation\">\u753b\u9762\u6bd4\u7387\u306b\u5408\u308f\u305b\u3066\u69cb\u56f3\u304c\u5d29\u308c\u306a\u3044\u3088\u3046\u306b `background-size` \u3084 `position` \u3092\u8abf\u6574<\/span><\/li>\n<\/ul>\n<\/div>\n<h2 class=\"sc_heading solid blue\"><span class=\"sc_title\">\u5fdc\u7528\u30a2\u30a4\u30c7\u30a2\uff1a\u6642\u9593\u5e2f\u3084\u66dc\u65e5\u3067\u51fa\u3057\u5206\u3051\u3082<\/span><\/h2>\n<p>\u4eca\u56de\u306e\u30b3\u30fc\u30c9\u3092\u5c11\u3057\u5fdc\u7528\u3059\u308c\u3070\u3001\u300c\u671d\u306f\u660e\u308b\u3044\u753b\u50cf\u300d\u300c\u591c\u306f\u30b7\u30c3\u30af\u306a\u753b\u50cf\u300d\u300c\u9031\u672b\u306f\u7279\u5225\u30d0\u30fc\u30b8\u30e7\u30f3\u300d\u306a\u3069\u3001\u3055\u3089\u306b\u5de5\u592b\u3057\u305f\u80cc\u666f\u6f14\u51fa\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"line-numbers\" ><code class=\"language-js\">const now = new Date();\r\nconst hour = now.getHours();\r\nlet selectedImage;\r\n\r\nif (hour &lt; 12) {\r\n  selectedImage = &quot;images\/morning.jpg&quot;;\r\n} else if (hour &lt; 18) {\r\n  selectedImage = &quot;images\/afternoon.jpg&quot;;\r\n} else {\r\n  selectedImage = &quot;images\/night.jpg&quot;;\r\n}\r\ndocument.getElementById(&quot;hero&quot;).style.backgroundImage = `url(&#039;${selectedImage}&#039;)`;<\/code><\/pre>\n<h2 class=\"sc_heading solid blue\"><span class=\"sc_title\">\u307e\u3068\u3081<\/span><\/h2>\n<p><span class=\"sc_marker-animation\">Web\u30b5\u30a4\u30c8\u306b\u304a\u3051\u308b\u7b2c\u4e00\u5370\u8c61\u3092\u5de6\u53f3\u3059\u308b\u201c\u80cc\u666f\u753b\u50cf\u201d\u3002<\/span><\/p>\n<p>\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3073\u306b\u5909\u5316\u304c\u3042\u308b\u3068\u3001\u8a2a\u554f\u8005\u3082\u30ef\u30af\u30ef\u30af\u3057\u307e\u3059\u3088\u306d\u3002<\/p>\n<p>\u4eca\u56de\u306f\u300c\u753b\u50cf\u914d\u5217\uff0b\u30e9\u30f3\u30c0\u30e0\u9078\u629e\uff0b\u80cc\u666f\u6307\u5b9a\u300d\u3068\u3044\u3046\u30b7\u30f3\u30d7\u30eb\u306a\u69cb\u6210\u3067\u5b9f\u88c5\u3057\u307e\u3057\u305f\u304c\u3001\u7d44\u307f\u5408\u308f\u305b\u6b21\u7b2c\u3067\u8868\u73fe\u306f\u7121\u9650\u5927\u3067\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u3057\u305f\u3072\u3068\u5de5\u592b\u304c\u3001<span class=\"sc_marker-animation\">\u300c\u304a\u3001\u306a\u3093\u304b\u3044\u3044\u306a\u3053\u306e\u30b5\u30a4\u30c8\u300d<\/span>\u3068\u3044\u3046\u5370\u8c61\u306b\u3064\u306a\u304c\u308b\u3053\u3068\u3082\u3002<br \/>\n\u305c\u3072\u3001\u3042\u306a\u305f\u306e\u30b5\u30a4\u30c8\u306b\u3082\u53d6\u308a\u5165\u308c\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\uff01<\/p>\n<hr class=\"diver-hr style-solid hr-content-text is-align-center\" style=\"color:#ccc;height:1px;background-image:linear-gradient(to right, #ccc, #ccc);\">\n<p>&nbsp;<\/p>\n<p><figure id=\"attachment_475\" aria-describedby=\"caption-attachment-475\" style=\"width: 1500px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/satokotadesign.com\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/satokotadesign.com\/blog\/wp-content\/uploads\/2022\/10\/bnr.jpg\" alt=\"\u9759\u5ca1\u770c\u6d5c\u677e\u5e02\u3067\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u5236\u4f5c\u306a\u3089satokotadesign\" width=\"1500\" height=\"500\" class=\"size-full wp-image-475\" srcset=\"https:\/\/satokotadesign.com\/blog\/wp-content\/uploads\/2022\/10\/bnr.jpg 1500w, https:\/\/satokotadesign.com\/blog\/wp-content\/uploads\/2022\/10\/bnr-500x167.jpg 500w, https:\/\/satokotadesign.com\/blog\/wp-content\/uploads\/2022\/10\/bnr-1024x341.jpg 1024w, https:\/\/satokotadesign.com\/blog\/wp-content\/uploads\/2022\/10\/bnr-768x256.jpg 768w\" sizes=\"auto, (max-width: 1500px) 100vw, 1500px\" \/><\/a><figcaption id=\"caption-attachment-475\" class=\"wp-caption-text\"><a href=\"https:\/\/satokotadesign.com\/contact\/\" target=\"_blank\" rel=\"noopener\">\u9759\u5ca1\u770c\u6d5c\u677e\u5e02\u3067\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u5236\u4f5c\u306a\u3089satokotadesign<\/a><\/figcaption><\/figure><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\uff01\u9759\u5ca1\u770c\u6d5c\u677e\u5e02\u3067WEB\u30c7\u30b6\u30a4\u30ca\u30fc\u3092\u3057\u3066\u3044\u307e\u3059\u5c0f\u7027\u3067\u3059\u3002 Web\u30b5\u30a4\u30c8\u306b\u3061\u3087\u3063\u3068\u3057\u305f\u300c\u5909\u5316\u300d\u3084\u300c\u904a\u3073\u5fc3\u300d\u3092\u52a0\u3048\u305f\u3044\u3068\u304d\u3001\u771f\u3063\u5148\u306b\u601d\u3044\u6d6e\u304b\u3076\u306e\u304c\u201c\u80cc\u666f\u753b\u50cf\u306e\u6f14\u51fa\u201d\u3067\u3059\u3002 \u7279\u306b\u3001\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3073\u306b\u80cc\u666f\u304c\u30e9\u30f3\u30c0\u30e0\u306b\u5909\u308f\u3063\u305f\u3089\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u5370\u2026<\/p>\n","protected":false},"author":1,"featured_media":1900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[8],"class_list":["post-1894","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/posts\/1894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/comments?post=1894"}],"version-history":[{"count":5,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/posts\/1894\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/posts\/1894\/revisions\/1899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/media\/1900"}],"wp:attachment":[{"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/media?parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/categories?post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/satokotadesign.com\/blog\/wp-json\/wp\/v2\/tags?post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}