Visual Studio CodeでHTML・CSS・javascriptのコーディングをしています。
コーディングしたHTMLをブラウザで開いたところ、javascriptが読み込まれず困っています。
VS Code内でLive Serverのプラグインを使いブラウザに表示させる分には問題なく表示されますが、
HTMLのファイルを右クリックし「プログラムから開く」でブラウザで開くと、javascriptが反映されなくなってしまうのです。
HTMLとCSSは毎回正しく開けており、javascriptだけうまく読み込めないことが多いようです。
リロードを繰り返すと1割くらいの確率で読み込むことがあります。
どんな問題が考えられるでしょうか?
ご助力いただけると非常に助かります。
以下に実際のコードを示します。
おおまかに言うと、画面が読み込まれたら#splashの部分を表示し、一定時間後にフェードアウトさせることで、メインコンテンツが表示されるという風にしています。
そのほか、グローバルメニューが順番に下からスライドしながらふわっと表示されるようにしています。
HTML
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,700;1,700&display=swap" rel="stylesheet"> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/style.css"> <title>my portfolio...</title> </head> <body> <!-- 読み込み前 --> <div id="splash"> <div class="hide-top-pictures"> <img class="pic01 hidden" src="image/top cage.jpg" alt="ケージの天井" width="350px" height="233px"> <img class="pic02 hidden" src="image/top flower.jpg" alt="花単体" width="253px" height="380px"> <img class="pic03 hidden" src="image/top sky.jpg" alt="空" width="300px" height="200px"> <img class="pic04 hidden" src="image/top mini building.jpg" alt="家" width="400px" height="266px"> <img class="pic05 hidden" src="image/top flowers.jpg" alt="花たち" width="280px" height="280px"> </div> </div> <div id="container"> <!-- =============================== header ここから ================================= --> <header class="header"> <div class="wrapper"> <img class="logo" src="image/logo.png" alt="メインロゴ" width="150px" height="150px"> <nav class="global-nav"> <ul> <li class="global-item hidden"><a href="index.html" class="global-link">Top</a></li> <li class="global-item hidden"><a href="gallery.html" class="global-link">Gallery</a></li> <li class="global-item hidden"><a href="about_me.html" class="global-link">About me</a></li> <li class="global-item hidden"><a href="contact.html" class="global-link">Contact</a></li> </ul> </nav> </div> </header> <!-- =============================== main ここから ================================= --> <main class="main"> <!-- タイトル --> <div class="descriptions"> <h1 class="site-title textRandomAnime">my portfolio...</h1> <p class="explanation">all of my works are here</p> <!-- <div class="white-title"></div> --> </div> <!-- 写真 --> <div class="top-pictures"> <img class="pic01" src="image/top cage.jpg" alt="ケージの天井" width="350px" height="233px"> <img class="pic02" src="image/top flower.jpg" alt="花単体" width="253px" height="380px"> <img class="pic03" src="image/top sky.jpg" alt="空" width="300px" height="200px"> <img class="pic04" src="image/top mini building.jpg" alt="家" width="400px" height="266px"> <img class="pic05" src="image/top flowers.jpg" alt="花たち" width="280px" height="280px"> </div> </main> </div> <!-- =============================== script ここから ================================= --> <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="js/script.js"></script> </body> </html>
CSS
@charset "UTF-8"; html{ font-size: 62.5%; } *, *::before, *::after{ box-sizing: border-box; } body{ color: #666; font-size: 1.6rem; font-family: 'Cormorant Garamond', serif; } .main{ width: 100%; } /* =============================== jQuery共通 ================================= */ .hidden{ opacity: 0; transform : translateY(50px); /* transform: translateY(20px); */ transition: all 1s; } .fadeup{ transform: translateY(0); opacity: 0.8; } /* =============================== 画面読み込み前処理 ================================= */ #splash { position: fixed; width: 100%; height: 100%; z-index: 998; background:#fff; } #container{ width:100%; height: 100vh; } /* =============================== header ここから ================================= */ .wrapper{ display: flex; justify-content: space-between; width: 100%; padding: 0px 70px; } .global-nav{ padding-top: 55px; } .global-item{ display: inline-block; margin-left: 20px; list-style: none; } .global-link{ text-decoration: none; font-size: 2.5rem; color: #666; transition: 0.4s; padding-bottom: 3px; } .logo{ margin-top: 40px; opacity: 0.8; } .global-item a::after{ content: ''; display: block; width: 0; margin: 6px auto 0; border-bottom: 1px solid #666; transition: width 0.1s ease-in-out; } .global-item a:hover::after{ width: 100%; opacity: 0.5; } .global-item a:hover{ opacity: 0.5; } /* =============================== index ここから ================================= */ /* タイトル */ .descriptions{ position: relative; top: 170px; left: 200px; } .site-title{ font-size: 10rem; font-weight: bold; z-index: 999; margin-bottom: 30px; letter-spacing: 1px; } .explanation{ font-size: 3rem; } /* 写真 */ .top-pictures{ opacity: 0.5; } .pic01{ position: absolute; top: 180px; left: 300px; } .pic02{ position: absolute; top: 90px; left: 800px; } .pic03{ position: absolute; top: 310px; left: 1200px; } .pic04{ position: absolute; top: 570px; left: 900px; } .pic05{ position: absolute; top: 620px; left: 480px; }
JS
$(function(){ $(window).on('load',function(){ $('#splash .hide-top-pictures img').each(function(index){ let $this = $(this); delayTime = index; setTimeout(function(){ $this.addClass('fadeup'); }, 300 * delayTime); }); $('.global-nav .global-item').each(function(index){ let $this = $(this); delayTime = index; setTimeout(function(){ $this.addClass('fadeup'); }, 1600 + (300 * delayTime)); }); $("#splash").delay(1500).fadeOut(1000); }); });
よろしくお願いいたします。
まだ回答がついていません
会員登録して回答してみよう