前提・実現したいこと
「jQuery テクニカルノート 矢次悟郎」という参考書についてです。
複数画像をクロスフェードで切り替えるスライドショーの機能を実装中に以下のエラーメッセージが発生しました。エラーが出ているのはjavascriptです。
発生している問題・エラーメッセージ
エラーメッセージ function declaration should not be plyed in blocks.use a function expression or move the statement to the top of the outer function.
該当のソースコード
javascript
1function switchNext(){ 2 var setActive = pagination.find('.pnActive'), 3 pnIndex = pnPoint.index(setActive), 4 pnNum = pnIndex + 1; 5 6 if(pnCount === pnNum){ 7 pnFirst.click(); 8 } else { 9 setActive.next('a').click(); 10 } 11 } 12 13function switchPrev(){ 14 var setActive = pagination.find('.pnActive'), 15 pnIndex = pnPoint.index(this), 16 pnNum = pnIndex + 1; 17 if(1 === pnNum){ 18 pnLast.click(); 19 } else { 20 setActive.prev('a').click(); 21 } 22 } 23 24 btnNext.click(function(){switchNext();}); 25 btnPrev.click(function(){switchPrev();}); 26 }
html
1<!doctype html> 2<html> 3<head> 4<meta charset="utf-8"> 5<title>架空旅館サイト</title> 6<link rel="stylesheet" href="style.css"> 7<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Bitter:400;700"> 8<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script> 9<script src="js/script.js"></script> 10 11</head> 12 13<body> 14 15<header> 16 <div class="logo"> 17 <a href=""> 18 <h1>架空旅館サイト</h1> 19 </a> 20 </div> 21 22 <nav> 23 <ul> 24 <li></li> 25 <li><a href="">客室</a></li> 26 <li><a href="">お食事</a></li> 27 <li><a href="">船外体験</a></li> 28 <li><a href="">ブログ</a></li> 29 <li><a href="">アクセス</a></li> 30 </ul> 31 </nav> 32</header> 33 34<div class="slideshow"> 35 <ul> 36 <li><img src="img/img01.png" alt=""></li> 37 <li><img src="img/img02.png" alt=""></li> 38 <li><img src="img/img03.png" alt=""></li> 39 </ul> 40</div> 41 42 43</body> 44</html> 45 46 47
css
1 2.slideshow ul { 3 width: 100%; 4} 5 6.slideshow ul li { 7 top: 150px; 8 left: 0; 9 width: 100%; 10 display: none; 11 position: absolute; 12} 13 14.slideshow ul li img { 15 width: 100%; 16} 17 18@media only screen and (max-width: 1020px) { 19 .slideshow { 20 width: 100%; 21 } 22} 23 24.pagiNation { 25 bottom: 20px; 26 left: 0; 27 width: 100%; 28 height: 15px; 29 text-align: center; 30 position: absolute; 31 z-index: 110; 32} 33 34.pagiNation a { 35 margin: 0; 36 width: 15px; 37 height: 15px; 38 display: inline-block; 39 overflow: hidden; 40 background: #aaa; 41 border: #fff 2px solid; 42 border-radius: 50%; 43 transition: all 0.2s ease-in-out; 44} 45 46.pagiNation a:hover { 47 opacity: 0.6; 48} 49 50.pagiNation a.pnActive { 51 background: #000; 52} 53 54.pagiNation a.pnActive:hover { 55 opacity: 1; 56} 57 58.slideshow .btnPrev, 59.slideshow .btnNext { 60 margin-top: -25px; 61 top: 50%; 62 width: 50px; 63 height: 50px; 64 position: absolute; 65 z-index: 105; 66 transition: all 0.2s ease-in-out; 67} 68 69.slideshow .btnPrev { 70 left: 15px; 71 background: transparent url(../img/btnPrev.png) 72 no-repeat center center; 73} 74 75.slideshow .btnNext { 76 right: 15px; 77 background: transparent url(../img/btnNext.png) 78 no-repeat center center; 79} 80 81.slideshow .btnPrev:hover, 82.slideshow .btnNext:hover { 83 opacity: 0.6; 84} 85 86
試したこと
ネットでエラーメッセージを調べましたが、日本語の参考になるサイトがほとんどなく、私がJSについて初心者なので意味がいまいちわかりません。
参考書のサンプルコードも上記のコードと同じで、エラーがでています。jQueryも参考書とバージョンを合わせています。何かしらのバージョンの問題なのかもしれませんが、お助け下さい。
他にもエラーがあり、調べてみてif(1 == pnNum)をif(1 === pnNum)に直しました。
これで正しいか正直自身はありませんがエラーはなくなりました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
jquery-1.11.3.min.js
dreamweaver CC 2017を使用