Visual Studio CodeでHTML・CSS・javascriptのコーディングをしています。
コーディングしたHTMLをブラウザで開いたところ、javascriptが読み込まれず困っています。
VS Code内でLive Serverのプラグインを使いブラウザに表示させる分には問題なく表示されますが、
HTMLのファイルを右クリックし「プログラムから開く」でブラウザで開くと、javascriptが反映されなくなってしまうのです。
HTMLとCSSは毎回正しく開けており、javascriptだけうまく読み込めないことが多いようです。
リロードを繰り返すと1割くらいの確率で読み込むことがあります。
どんな問題が考えられるでしょうか?
ご助力いただけると非常に助かります。
以下に実際のコードを示します。
おおまかに言うと、画面が読み込まれたら#splashの部分を表示し、一定時間後にフェードアウトさせることで、メインコンテンツが表示されるという風にしています。
そのほか、グローバルメニューが順番に下からスライドしながらふわっと表示されるようにしています。
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <link rel="preconnect" href="https://fonts.googleapis.com"> 8 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> 9 <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,700;1,700&display=swap" rel="stylesheet"> 10 <link rel="stylesheet" href="css/reset.css"> 11 <link rel="stylesheet" href="css/style.css"> 12 <title>my portfolio...</title> 13</head> 14 15<body> 16 <!-- 読み込み前 --> 17 <div id="splash"> 18 <div class="hide-top-pictures"> 19 <img class="pic01 hidden" src="image/top cage.jpg" alt="ケージの天井" width="350px" height="233px"> 20 <img class="pic02 hidden" src="image/top flower.jpg" alt="花単体" width="253px" height="380px"> 21 <img class="pic03 hidden" src="image/top sky.jpg" alt="空" width="300px" height="200px"> 22 <img class="pic04 hidden" src="image/top mini building.jpg" alt="家" width="400px" height="266px"> 23 <img class="pic05 hidden" src="image/top flowers.jpg" alt="花たち" width="280px" height="280px"> 24 </div> 25 </div> 26 <div id="container"> 27 28 <!-- =============================== 29 header ここから 30 ================================= --> 31 <header class="header"> 32 <div class="wrapper"> 33 <img class="logo" src="image/logo.png" alt="メインロゴ" width="150px" height="150px"> 34 <nav class="global-nav"> 35 <ul> 36 <li class="global-item hidden"><a href="index.html" class="global-link">Top</a></li> 37 <li class="global-item hidden"><a href="gallery.html" class="global-link">Gallery</a></li> 38 <li class="global-item hidden"><a href="about_me.html" class="global-link">About me</a></li> 39 <li class="global-item hidden"><a href="contact.html" class="global-link">Contact</a></li> 40 </ul> 41 </nav> 42 </div> 43 </header> 44 45 46 <!-- =============================== 47 main ここから 48 ================================= --> 49 <main class="main"> 50 <!-- タイトル --> 51 <div class="descriptions"> 52 <h1 class="site-title textRandomAnime">my portfolio...</h1> 53 <p class="explanation">all of my works are here</p> 54 <!-- <div class="white-title"></div> --> 55 </div> 56 57 <!-- 写真 --> 58 <div class="top-pictures"> 59 <img class="pic01" src="image/top cage.jpg" alt="ケージの天井" width="350px" height="233px"> 60 <img class="pic02" src="image/top flower.jpg" alt="花単体" width="253px" height="380px"> 61 <img class="pic03" src="image/top sky.jpg" alt="空" width="300px" height="200px"> 62 <img class="pic04" src="image/top mini building.jpg" alt="家" width="400px" height="266px"> 63 <img class="pic05" src="image/top flowers.jpg" alt="花たち" width="280px" height="280px"> 64 </div> 65 </main> 66 </div> 67 68 69 <!-- =============================== 70 script ここから 71 ================================= --> 72 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> 73 <script src="js/script.js"></script> 74</body> 75</html>
CSS
1@charset "UTF-8"; 2 3html{ 4 font-size: 62.5%; 5} 6 7*, *::before, *::after{ 8 box-sizing: border-box; 9} 10 11body{ 12 color: #666; 13 font-size: 1.6rem; 14 font-family: 'Cormorant Garamond', serif; 15} 16 17.main{ 18 width: 100%; 19} 20 21/* =============================== 22 jQuery共通 23================================= */ 24.hidden{ 25 opacity: 0; 26 transform : translateY(50px); 27 /* transform: translateY(20px); */ 28 transition: all 1s; 29} 30.fadeup{ 31 transform: translateY(0); 32 opacity: 0.8; 33} 34 35/* =============================== 36 画面読み込み前処理 37================================= */ 38#splash { 39 position: fixed; 40 width: 100%; 41 height: 100%; 42 z-index: 998; 43 background:#fff; 44} 45#container{ 46 width:100%; 47 height: 100vh; 48} 49 50/* =============================== 51 header ここから 52================================= */ 53.wrapper{ 54 display: flex; 55 justify-content: space-between; 56 width: 100%; 57 padding: 0px 70px; 58} 59.global-nav{ 60 padding-top: 55px; 61} 62.global-item{ 63 display: inline-block; 64 margin-left: 20px; 65 list-style: none; 66} 67.global-link{ 68 text-decoration: none; 69 font-size: 2.5rem; 70 color: #666; 71 transition: 0.4s; 72 padding-bottom: 3px; 73} 74.logo{ 75 margin-top: 40px; 76 opacity: 0.8; 77} 78.global-item a::after{ 79 content: ''; 80 display: block; 81 width: 0; 82 margin: 6px auto 0; 83 border-bottom: 1px solid #666; 84 transition: width 0.1s ease-in-out; 85} 86.global-item a:hover::after{ 87 width: 100%; 88 opacity: 0.5; 89} 90.global-item a:hover{ 91 opacity: 0.5; 92} 93 94 95 96/* =============================== 97 index ここから 98================================= */ 99/* タイトル */ 100.descriptions{ 101 position: relative; 102 top: 170px; 103 left: 200px; 104} 105.site-title{ 106 font-size: 10rem; 107 font-weight: bold; 108 z-index: 999; 109 margin-bottom: 30px; 110 letter-spacing: 1px; 111} 112.explanation{ 113 font-size: 3rem; 114} 115 116 117/* 写真 */ 118.top-pictures{ 119 opacity: 0.5; 120} 121.pic01{ 122 position: absolute; 123 top: 180px; 124 left: 300px; 125} 126.pic02{ 127 position: absolute; 128 top: 90px; 129 left: 800px; 130} 131.pic03{ 132 position: absolute; 133 top: 310px; 134 left: 1200px; 135} 136.pic04{ 137 position: absolute; 138 top: 570px; 139 left: 900px; 140} 141.pic05{ 142 position: absolute; 143 top: 620px; 144 left: 480px; 145} 146
JS
1$(function(){ 2 3 $(window).on('load',function(){ 4 5 $('#splash .hide-top-pictures img').each(function(index){ 6 7 let $this = $(this); 8 delayTime = index; 9 10 setTimeout(function(){ 11 $this.addClass('fadeup'); 12 }, 300 * delayTime); 13 }); 14 15 16 $('.global-nav .global-item').each(function(index){ 17 18 let $this = $(this); 19 delayTime = index; 20 21 setTimeout(function(){ 22 $this.addClass('fadeup'); 23 }, 1600 + (300 * delayTime)); 24 }); 25 26 $("#splash").delay(1500).fadeOut(1000); 27 }); 28});
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー